[14:7] extends: object
The GMainContext struct is an opaque data type representing a set of
sources to be handled in a main loop.
GMainContext (flags = null)
Creates a new [struct@GLib.MainContext] structure.
flags is a bitwise-OR combination of flags that can only be set at creation time.toNativeHandle (Source)
Normalizes a constructor argument into a raw pointer carrier. Accepts a raw NativeHandle, a raw NativeBuffer returned from
fn.call(...), another generated wrapper exposinghandle(), or null. Returns null when the argument carries no pointer.
Source is the raw handle, raw buffer, wrapper, or null.A raw pointer carrier or null when no pointer is present.getLib ()
Returns the opened native library for this generated wrapper.
The opened native library.handle ()
Returns the wrapped NativeHandle.
The wrapped NativeHandle.isNull ()
Returns true when the wrapped handle is null.
A bool.describe ()
Returns a small string for debugging generated wrappers.
A string.acquire ()
Tries to become the owner of the specified context. If some other thread is the owner of the context, returns false immediately. Ownership is properly recursive: the owner can require ownership again and will release ownership when [method@GLib.MainContext.release] is called as many times as [method@GLib.MainContext.acquire]. You must be the owner of a context before you can call [method@GLib.MainContext.prepare], [method@GLib.MainContext.query], [method@GLib.MainContext.check], [method@GLib.MainContext.dispatch], [method@GLib.MainContext.release]. Since 2.76 @context can be
NULLto use the global-default main context.
true if this thread is now the owner of @context, false otherwise.add_poll (fd, int priority)
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.
fd is a [struct@GLib.PollFD] structure holding information about a file descriptor to watch..priority is the priority for this file descriptor which should be the same as the priority used for [method@GLib.Source.attach] to ensure that the file descriptor is polled whenever the results may be needed..None.check (int max_priority, list fds)
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 [method@GLib.MainContext.query], as this functions relies on assumptions on how @fds is filled. You must have successfully acquired the context with [method@GLib.MainContext.acquire] before you may call this function. Since 2.76 @context can be
NULLto use the global-default main context.
max_priority is the maximum numerical priority of sources to check.fds is array of [struct@GLib.PollFD]s that was passed to the last call to [method@GLib.MainContext.query].n_fds is return value of [method@GLib.MainContext.query].true if some sources are ready to be dispatched, false otherwise.dispatch ()
Dispatches all pending sources. You must have successfully acquired the context with [method@GLib.MainContext.acquire] before you may call this function. Since 2.76 @context can be
NULLto use the global-default main context.
None.find_source_by_funcs_user_data (object funcs, user_data)
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.
funcs is the @source_funcs passed to [ctor@GLib.Source.new].user_data is the user data from the callback.the source, if one was found, otherwise NULL.find_source_by_id (int source_id)
Finds a [struct@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 [func@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.
source_id is the source ID, as returned by [method@GLib.Source.get_id].the source.find_source_by_user_data (user_data)
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.
user_data is the user_data for the callback.the source, if one was found, otherwise NULL.get_poll_func ()
Gets the poll function set by [method@GLib.MainContext.set_poll_func].
the poll function.invoke (data)
Invokes a function in such a way that @context is owned during the invocation of @function. If @context is
NULLthen the global-default main context — as returned by [func@GLib.MainContext.default] — is used. If @context is owned by the current thread, @function is called directly. Otherwise, if @context is the thread-default main context of the current thread and [method@GLib.MainContext.acquire] succeeds, then @function is called and [method@GLib.MainContext.release] is called afterwards. In any other case, an idle source is created to call @function and that source is attached to @context (presumably to be run in another thread). The idle source is attached with [const@GLib.PRIORITY_DEFAULT] priority. If you want a different priority, use [method@GLib.MainContext.invoke_full]. Note that, as with normal idle functions, @function should probably return [const@GLib.SOURCE_REMOVE]. If it returns [const@GLib.SOURCE_CONTINUE], it will be continuously run in a loop (and may prevent this call from returning).
function is function to call.data is data to pass to @function.None.invoke_full (int priority, object notify)
Invokes a function in such a way that @context is owned during the invocation of @function. This function is the same as [method@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 [callback@GLib.DestroyNotify] for @data. The @notify function should not assume that it is called from any particular thread or with any particular context acquired.
priority is the priority at which to run @function.function is function to call.data is data to pass to @function.notify is a function to call when @data is no longer in use.None.is_owner ()
Determines whether this thread holds the (recursive) ownership of this [struct@GLib.MainContext]. This is useful to know before waiting on another thread that may be blocking to get ownership of @context.
true if current thread is owner of @context, false otherwise.iteration (bool may_block)
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 [method@GLib.MainContext.iteration] to return false, since the wait may be interrupted for other reasons than an event source becoming ready.
may_block is whether the call may block.true if events were dispatched, false otherwise.pending ()
Checks if any sources have pending events for the given context.
true if events are pending, false otherwise.pop_thread_default ()
Pops @context off the thread-default context stack (verifying that it was on the top of the stack).
None.prepare ()
Prepares to poll sources within a main loop. The resulting information for polling is determined by calling [method@GLib.MainContext.query]. You must have successfully acquired the context with [method@GLib.MainContext.acquire] before you may call this function.
priority is location to store priority of highest priority source already ready.true if some source is ready to be dispatched prior to polling, false otherwise.push_thread_default ()
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 [func@GLib.MainContext.get_thread_default], not the one returned by [func@GLib.MainContext.default], so it does not affect the context used by functions like [func@GLib.idle_add]. Normally you would call this function shortly after creating a new thread, passing it a [struct@GLib.MainContext] which will be run by a [struct@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 [method@GLib.MainContext.pop_thread_default], assuming you want the new [struct@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 #GThreadPool), it is always suggested to wrap the logic that needs to use the new [struct@GLib.MainContext] inside a [method@GLib.MainContext.push_thread_default] / [method@GLib.MainContext.pop_thread_default] pair, otherwise threads that are re-used will end up never explicitly releasing the [struct@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 [method@GLib.MainContext.push_thread_default] / [method@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().
None.query (int max_priority, int n_fds)
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 [method@GLib.MainContext.check], as this function relies on assumptions made when the array is filled. You must have successfully acquired the context with [method@GLib.MainContext.acquire] before you may call this function.
max_priority is maximum priority source to check.timeout_ is location to store timeout to be used in polling.fds is location to store [struct@GLib.PollFD] records that need to be polled.n_fds is length of @fds.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.ref ()
Increases the reference count on a [struct@GLib.MainContext] object by one.
the @context that was passed in (since 2.6).release ()
Releases ownership of a context previously acquired by this thread with [method@GLib.MainContext.acquire]. If the context was acquired multiple times, the ownership will be released only when [method@GLib.MainContext.release] is called as many times as it was acquired. You must have successfully acquired the context with [method@GLib.MainContext.acquire] before you may call this function.
None.remove_poll (fd)
Removes file descriptor from the set of file descriptors to be polled for a particular context.
fd is a [struct@GLib.PollFD] descriptor previously added with [method@GLib.MainContext.add_poll].None.set_poll_func (object func)
Sets the function to use to handle polling of file descriptors. It will be used instead of the
poll()system call (or GLib’s replacement function, which is used wherepoll()isn’t available). This function could possibly be used to integrate the GLib event loop with an external event loop.
func is the function to call to poll all file descriptors.None.unref ()
Decreases the reference count on a [struct@GLib.MainContext] object by one. If the result is zero, free the context and free all associated memory.
None.wait (object cond, object mutex)
Tries to become the owner of the specified context, and waits on @cond if another thread is the owner. This is the same as [method@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.
cond is a condition variable.mutex is a mutex, currently held.true if this thread is now the owner of @context, false otherwise.wakeup ()
Wake up @context if it’s currently blocking in [method@GLib.MainContext.iteration], causing it to stop blocking. The
could be blocking waiting for a source to become ready. Otherwise, if @context is not currently blocking, this function causes the next invocation of [method@GLib.MainContext.iteration] to return without blocking. This API is useful for low-level control over [struct@GLib.MainContext]; for example, integrating it with main loop implementations such as [struct@GLib.MainLoop]. Another related use for this function is when implementing a main loop with a termination condition, computed from multiple threads: c #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: c perform_work (); if (g_atomic_int_dec_and_test (&tasks_remaining)) g_main_context_wakeup (NULL); None.