<GMainContext>

<GMainContext>

Description

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

Functions

acquire?

(define-values (%return) (main-context:acquire? self))

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 g_main_context_release() is called as many times as g_main_context_acquire().

You must be the owner of a context before you can call g_main_context_prepare(), g_main_context_query(), g_main_context_check(), g_main_context_dispatch().

Parameters

context

a GMainContext

Passed as self

add-poll

(define-values () (main-context:add-poll self fd 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.

Parameters

context

a GMainContext (or NULL for the default context)

Passed as self

fd

a GPollFD structure holding information about a file descriptor to watch.

Passed as fd

priority

the priority for this file descriptor which should be the same as the priority used for g_source_attach() to ensure that the file descriptor is polled whenever the results may be needed.

Passed as priority

check?

(define-values (%return) (main-context:check? self max-priority fds))

Passes the results of polling back to the main loop.

You must have successfully acquired the context with g_main_context_acquire() before you may call this function.

Parameters

context

a GMainContext

Passed as self

max_priority

the maximum numerical priority of sources to check

Passed as max-priority

fds

array of GPollFD's that was passed to the last call to g_main_context_query()

Passed as fds

n_fds

return value of g_main_context_query()

Inferred from fds

dispatch

(define-values () (main-context:dispatch self))

Dispatches all pending sources.

You must have successfully acquired the context with g_main_context_acquire() before you may call this function.

Parameters

context

a GMainContext

Passed as self

find-source-by-id

(define-values (%return) (main-context:find-source-by-id self source-id))

Finds a GSource 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 g_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

context

a GMainContext (if NULL, the default context will be used)

Passed as self

source_id

the source ID, as returned by g_source_get_id().

Passed as source-id

find-source-by-user-data

(define-values
  (%return)
  (main-context:find-source-by-user-data self 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.

Parameters

context

a GMainContext

Passed as self

user_data

the user_data for the callback.

Passed as user-data

invoke-full

(define-values
  ()
  (main-context:invoke-full self priority function data notify))

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

This function is the same as g_main_context_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 GDestroyNotify for data.

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

Parameters

context

a GMainContext, or NULL

Passed as self

priority

the priority at which to run function

Passed as priority

function

function to call

Passed as function

data

data to pass to function

Passed as data

notify

a function to call when data is no longer in use, or NULL.

Passed as notify

is-owner?

(define-values (%return) (main-context:is-owner? self))

Determines whether this thread holds the (recursive) ownership of this GMainContext. This is useful to know before waiting on another thread that may be blocking to get ownership of context.

Parameters

context

a GMainContext

Passed as self

iteration?

(define-values (%return) (main-context:iteration? self 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 sources are not waited to become ready, only those highest priority events sources will be dispatched (if any), that are ready at this given moment without further waiting.

Note that even when may_block is TRUE, it is still possible for g_main_context_iteration() to return FALSE, since the wait may be interrupted for other reasons than an event source becoming ready.

Parameters

context

a GMainContext (if NULL, the default context will be used)

Passed as self

may_block

whether the call may block.

Passed as may-block

pending?

(define-values (%return) (main-context:pending? self))

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

Parameters

context

a GMainContext (if NULL, the default context will be used)

Passed as self

pop-thread-default

(define-values () (main-context:pop-thread-default self))

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

Parameters

context

a GMainContext object, or NULL

Passed as self

prepare

(define-values (%return priority) (main-context:prepare self))

Prepares to poll sources within a main loop. The resulting information for polling is determined by calling g_main_context_query ().

You must have successfully acquired the context with g_main_context_acquire() before you may call this function.

Parameters

context

a GMainContext

Passed as self

priority

location to store priority of highest priority source already ready.

Passed as priority

push-thread-default

(define-values () (main-context:push-thread-default self))

Acquires context and sets it as the thread-default context for the current thread. This will cause certain asynchronous operations (such as most [gio][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 context in the main thread. Note that calling this function changes the context returned by g_main_context_get_thread_default(), not the one returned by g_main_context_default(), so it does not affect the context used by functions like g_idle_add().

Normally you would call this function shortly after creating a new thread, passing it a GMainContext which will be run by a GMainLoop 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 g_main_context_pop_thread_default(), assuming you want the new GMainContext 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 GMainContext inside a g_main_context_push_thread_default() / g_main_context_pop_thread_default() pair, otherwise threads that are re-used will end up never explicitly releasing the GMainContext 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 g_main_context_push_thread_default() / g_main_context_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. Eg, see g_file_supports_thread_contexts().

Parameters

context

a GMainContext, or NULL for the global default context

Passed as self

query!

(define-values
  (%return timeout- fds)
  (main-context:query! self max-priority fds))

Determines information necessary to poll this main loop.

You must have successfully acquired the context with g_main_context_acquire() before you may call this function.

Parameters

context

a GMainContext

Passed as self

max_priority

maximum priority source to check

Passed as max-priority

timeout_

location to store timeout to be used in polling

Passed as timeout-

fds

location to store GPollFD records that need to be polled.

Passed as fds

n_fds

length of fds.

Inferred from fds

ref

(define-values (%return) (main-context:ref self))

Increases the reference count on a GMainContext object by one.

Parameters

context

a GMainContext

Passed as self

release

(define-values () (main-context:release self))

Releases ownership of a context previously acquired by this thread with g_main_context_acquire(). If the context was acquired multiple times, the ownership will be released only when g_main_context_release() is called as many times as it was acquired.

Parameters

context

a GMainContext

Passed as self

remove-poll

(define-values () (main-context:remove-poll self fd))

Removes file descriptor from the set of file descriptors to be polled for a particular context.

Parameters

context

a GMainContext

Passed as self

fd

a GPollFD descriptor previously added with g_main_context_add_poll()

Passed as fd

unref

(define-values () (main-context:unref self))

Decreases the reference count on a GMainContext object by one. If the result is zero, free the context and free all associated memory.

Parameters

context

a GMainContext

Passed as self

wakeup

(define-values () (main-context:wakeup self))

If context is currently blocking in g_main_context_iteration() waiting for a source to become ready, cause it to stop blocking and return. Otherwise, cause the next invocation of g_main_context_iteration() to return without blocking.

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

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 volatile gint tasks_remaining = NUM_TASKS;
  ...
 
  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);

Parameters

context

a GMainContext

Passed as self

main-context:new

(define-values (%return) (main-context:new))

Undocumented

main-context:default

(define-values (%return) (main-context:default))

Returns the global default main context. This is the main context used for main loop functions when a main loop is not explicitly specified, and corresponds to the "main" main loop. See also g_main_context_get_thread_default().

main-context:get-thread-default

(define-values (%return) (main-context:get-thread-default))

Gets the thread-default GMainContext for this thread. Asynchronous operations that want to be able to be run in contexts other than the default one should call this method or g_main_context_ref_thread_default() to get a GMainContext to add their GSources to. (Note that even in single-threaded programs applications may sometimes want to temporarily push a non-default context, so it is not safe to assume that this will always return NULL if you are running in the default thread.)

If you need to hold a reference on the context, use g_main_context_ref_thread_default() instead.

main-context:ref-thread-default

(define-values (%return) (main-context:ref-thread-default))

Gets the thread-default GMainContext for this thread, as with g_main_context_get_thread_default(), but also adds a reference to it with g_main_context_ref(). In addition, unlike g_main_context_get_thread_default(), if the thread-default context is the global default context, this will return that GMainContext (with a ref added to it) rather than returning NULL.