Class (GI Struct)

GLib-2.0GLibMainContext

The GLib.MainContext struct is an opaque data type representing a set of sources to be handled in a main loop.

Index

Constructors

Properties

Methods

  • Adds a file descriptor to the set of file descriptors polled for this context.

    This will very seldom be used directly. Instead a typical event source will use g_source_add_unix_fd() instead.

    Parameters

    • fd: GLib.PollFD

      a GLib.PollFD structure holding information about a file descriptor to watch.

    • priority: number

      the priority for this file descriptor which should be the same as the priority used for GLib.Source.attach to ensure that the file descriptor is polled whenever the results may be needed.

    Returns void

  • Passes the results of polling back to the main loop.

    You should be careful to pass fds and its length n_fds as received from GLib.MainContext.query, as this functions relies on assumptions on how fds is filled.

    You must have successfully acquired the context with GLib.MainContext.acquire before you may call this function.

    Since 2.76 context can be NULL to use the global-default main context.

    Parameters

    Returns boolean

    true if some sources are ready to be dispatched, false otherwise

  • Dispatches all pending sources.

    You must have successfully acquired the context with GLib.MainContext.acquire before you may call this function.

    Since 2.76 context can be NULL to use the global-default main context.

    Returns void

  • Finds a source with the given source functions and user data.

    If multiple sources exist with the same source function and user data, the first one found will be returned.

    Parameters

    Returns GLib.Source

    the source, if one was found, otherwise NULL

  • Finds a GLib.Source given a pair of context and ID.

    It is a programmer error to attempt to look up a non-existent source.

    More specifically: source IDs can be reissued after a source has been destroyed and therefore it is never valid to use this function with a source ID which may have already been removed. An example is when scheduling an idle to run in another thread with GLib.idle_add: the idle may already have run and been removed by the time this function is called on its (now invalid) source ID. This source ID may have been reissued, leading to the operation being performed against the wrong source.

    Parameters

    Returns GLib.Source

    the source

  • Finds a source with the given user data for the callback.

    If multiple sources exist with the same user data, the first one found will be returned.

    Parameters

    • Optionaluser_data: any

      the user_data for the callback

    Returns GLib.Source

    the source, if one was found, otherwise NULL

  • Invokes a function in such a way that context is owned during the invocation of function.

    This function is the same as GLib.MainContext.invoke except that it lets you specify the priority in case function ends up being scheduled as an idle and also lets you give a GLib.DestroyNotify for data.

    The notify function should not assume that it is called from any particular thread or with any particular context acquired.

    Parameters

    • priority: number

      the priority at which to run function

    • _function: SourceFunc

      function to call

    • Optionalnotify: DestroyNotify

      a function to call when data is no longer in use

    Returns void

  • Determines whether this thread holds the (recursive) ownership of this GLib.MainContext.

    This is useful to know before waiting on another thread that may be blocking to get ownership of context.

    Returns boolean

    true if current thread is owner of context, false otherwise

  • Runs a single iteration for the given main loop.

    This involves checking to see if any event sources are ready to be processed, then if no events sources are ready and may_block is true, waiting for a source to become ready, then dispatching the highest priority events sources that are ready. Otherwise, if may_block is false, this function does not wait for sources to become ready, and only the highest priority sources which are already ready (if any) will be dispatched.

    Note that even when may_block is true, it is still possible for GLib.MainContext.iteration to return false, since the wait may be interrupted for other reasons than an event source becoming ready.

    Parameters

    • may_block: boolean

      whether the call may block

    Returns boolean

    true if events were dispatched, false otherwise

  • Checks if any sources have pending events for the given context.

    Returns boolean

    true if events are pending, false otherwise

  • Pops context off the thread-default context stack (verifying that it was on the top of the stack).

    Returns void

  • Prepares to poll sources within a main loop.

    The resulting information for polling is determined by calling GLib.MainContext.query.

    You must have successfully acquired the context with GLib.MainContext.acquire before you may call this function.

    Returns [boolean, number]

    true if some source is ready to be dispatched prior to polling, false otherwise

  • Acquires context and sets it as the thread-default context for the current thread. This will cause certain asynchronous operations (such as most Gio-based I/O) which are started in this thread to run under context and deliver their results to its main loop, rather than running under the global default main context in the main thread. Note that calling this function changes the context returned by GLib.MainContext.get_thread_default, not the one returned by GLib.MainContext.default, so it does not affect the context used by functions like GLib.idle_add.

    Normally you would call this function shortly after creating a new thread, passing it a GLib.MainContext which will be run by a GLib.MainLoop in that thread, to set a new default context for all async operations in that thread. In this case you may not need to ever call GLib.MainContext.pop_thread_default, assuming you want the new GLib.MainContext to be the default for the whole lifecycle of the thread.

    If you don’t have control over how the new thread was created (e.g. in the new thread isn’t newly created, or if the thread life cycle is managed by a GLib.ThreadPool), it is always suggested to wrap the logic that needs to use the new GLib.MainContext inside a GLib.MainContext.push_thread_default / GLib.MainContext.pop_thread_default pair, otherwise threads that are re-used will end up never explicitly releasing the GLib.MainContext reference they hold.

    In some cases you may want to schedule a single operation in a non-default context, or temporarily use a non-default context in the main thread. In that case, you can wrap the call to the asynchronous operation inside a GLib.MainContext.push_thread_default / GLib.MainContext.pop_thread_default pair, but it is up to you to ensure that no other asynchronous operations accidentally get started while the non-default context is active.

    Beware that libraries that predate this function may not correctly handle being used from a thread with a thread-default context. For example, see g_file_supports_thread_contexts().

    Returns void

  • Push main_context as the new thread-default main context for the current thread, using GLib.MainContext.push_thread_default, and return a new GLib.MainContextPusher. Pop with g_main_context_pusher_free(). Using GLib.MainContext.pop_thread_default on main_context while a GLib.MainContextPusher exists for it can lead to undefined behaviour.

    Using two GLib.MainContextPushers in the same scope is not allowed, as it leads to an undefined pop order.

    This is intended to be used with g_autoptr(). Note that g_autoptr() is only available when using GCC or clang, so the following example will only work with those compilers:

    typedef struct
    {
    ...
    GMainContext *context;
    ...
    } MyObject;

    static void
    my_object_do_stuff (MyObject *self)
    {
    g_autoptr(GMainContextPusher) pusher = g_main_context_pusher_new (self->context);

    // Code with main context as the thread default here

    if (cond)
    // No need to pop
    return;

    // Optionally early pop
    g_clear_pointer (&pusher, g_main_context_pusher_free);

    // Code with main context no longer the thread default here
    }

    Returns void

    a GLib.MainContextPusher

  • Determines information necessary to poll this main loop.

    You should be careful to pass the resulting fds array and its length n_fds as-is when calling GLib.MainContext.check, as this function relies on assumptions made when the array is filled.

    You must have successfully acquired the context with GLib.MainContext.acquire before you may call this function.

    Parameters

    • max_priority: number

      maximum priority source to check

    Returns [number, number, GLib.PollFD[]]

    the number of records actually stored in fds, or, if more than n_fds records need to be stored, the number of records that need to be stored

  • Tries to become the owner of the specified context, and waits on cond if another thread is the owner.

    This is the same as GLib.MainContext.acquire, but if another thread is the owner, atomically drop mutex and wait on cond until that owner releases ownership or until cond is signaled, then try again (once) to become the owner.

    Parameters

    • cond: Cond

      a condition variable

    • mutex: Mutex

      a mutex, currently held

    Returns boolean

    true if this thread is now the owner of context, false otherwise

  • Wake up context if it’s currently blocking in GLib.MainContext.iteration, causing it to stop blocking.

    The context could be blocking waiting for a source to become ready. Otherwise, if context is not currently blocking, this function causes the next invocation of GLib.MainContext.iteration to return without blocking.

    This API is useful for low-level control over GLib.MainContext; for example, integrating it with main loop implementations such as GLib.MainLoop.

    Another related use for this function is when implementing a main loop with a termination condition, computed from multiple threads:

      #define NUM_TASKS 10
    static gint tasks_remaining = NUM_TASKS; // (atomic)
    ...

    while (g_atomic_int_get (&tasks_remaining) != 0)
    g_main_context_iteration (NULL, TRUE);

    Then in a thread:

      perform_work ();

    if (g_atomic_int_dec_and_test (&tasks_remaining))
    g_main_context_wakeup (NULL);

    Returns void