1.14 <GMainContext>

Structure: <GMainContext>

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

Procedures

Method: main-context:acquire? self ⇒ %return
Method: acquire? self ⇒ %return

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 main-context:release is called as many times as main-context:acquire?.

You must be the owner of a context before you can call main-context:prepare, main-context:query!, main-context:check?, main-context:dispatch, main-context:release.

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

self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)

%return

Returned: a boolean.

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

Method: main-context:add-poll self fd priority
Method: 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 source:add-unix-fd instead.

self

Passed: a <GMainContext>.

a main context (or NULL for the global-default main context)

fd

Passed: a <GPollFD>.

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

priority

Passed: a 32-bit integer.

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

Method: main-context:check? self max-priority fds ⇒ %return
Method: check? self max-priority fds ⇒ %return

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 main-context:query!, as this functions relies on assumptions on how fds is filled.

You must have successfully acquired the context with main-context:acquire? before you may call this function.

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

self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)

max-priority

Passed: a 32-bit integer.

the maximum numerical priority of sources to check

fds

Passed: a list or vector, each a <GPollFD>.

array of <GPollFD>s that was passed to the last call to main-context:query!

%return

Returned: a boolean.

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

n-fds

Not passed: guile-gi gives the length of fds.

return value of main-context:query!

Procedure: main-context:default ⇒ %return

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 main-context:get-thread-default.

%return

Returned: a <GMainContext>.

the global-default main context.

Method: main-context:dispatch self
Method: dispatch self

Dispatches all pending sources.

You must have successfully acquired the context with main-context:acquire? before you may call this function.

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

self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)

Method: main-context:find-source-by-id self source-id ⇒ %return
Method: find-source-by-id self source-id ⇒ %return

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 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.

self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)

source-id

Passed: an unsigned 32-bit integer.

the source ID, as returned by source:get-id

%return

Returned: a <GSource>.

the source

Method: main-context:find-source-by-user-data self [user-data] ⇒ %return
Method: find-source-by-user-data self [user-data] ⇒ %return

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.

self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)

user-data

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the user_data for the callback

%return

Returned: a <GSource>, or #f.

the source, if one was found, otherwise NULL

Procedure: main-context:get-thread-default ⇒ %return

Gets the thread-default main context 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 main-context:ref-thread-default to get a <GMainContext> to add their <GSource>s 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 main-context:ref-thread-default instead.

%return

Returned: a <GMainContext>, or #f.

the thread-default main context, or NULL if the thread-default context is the global-default main context

Method: main-context:invoke-full self priority function
Method: invoke-full self priority function

Invokes a function in such a way that self 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 GDestroyNotify for data.

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

self

Passed: a <GMainContext>.

a main context, or NULL for the global-default main context

priority

Passed: a 32-bit integer.

the priority at which to run function

function

Passed: a procedure.

function to call

See GSourceFunc for how it is called.

data

Not passed: guile-gi supplies it.

data to pass to function

notify

Not passed: guile-gi supplies it.

a function to call when data is no longer in use

See GDestroyNotify for how it is called.

Method: main-context:is-owner? self ⇒ %return
Method: is-owner? self ⇒ %return

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 self.

self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)

%return

Returned: a boolean.

true if current thread is owner of self, false otherwise

Method: main-context:iteration? self may-block ⇒ %return
Method: iteration? self may-block ⇒ %return

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 main-context:iteration? to return false, since the wait may be interrupted for other reasons than an event source becoming ready.

self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)

may-block

Passed: a boolean.

whether the call may block

%return

Returned: a boolean.

true if events were dispatched, false otherwise

Procedure: main-context:new ⇒ %return

Creates a new <GMainContext> structure.

%return

Returned: a <GMainContext>.

the new main context

Procedure: main-context:new-with-flags flags ⇒ %return

Creates a new <GMainContext> structure.

flags

Passed: a <%GMainContextFlags>, or a list of its symbols, or one of them.

a bitwise-OR combination of flags that can only be set at creation time

%return

Returned: a <GMainContext>.

the new main context

Method: main-context:pending? self ⇒ %return
Method: pending? self ⇒ %return

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

self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)

%return

Returned: a boolean.

true if events are pending, false otherwise

Method: main-context:pop-thread-default self
Method: pop-thread-default self

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

self

Passed: a <GMainContext>.

a main context, or NULL for the global-default main context

Method: main-context:prepare self ⇒ %return priority
Method: prepare self ⇒ %return priority

Prepares to poll sources within a main loop.

The resulting information for polling is determined by calling main-context:query!.

You must have successfully acquired the context with main-context:acquire? before you may call this function.

self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)

%return

Returned: a boolean.

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

priority

Returned: a 32-bit integer.

location to store priority of highest priority source already ready

Method: main-context:push-thread-default self
Method: push-thread-default self

Acquires self 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 self 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 main-context:get-thread-default, not the one returned by main-context:default, so it does not affect the context used by functions like 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 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 main-context:push-thread-default / 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 main-context:push-thread-default / 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. For example, see file:supports-thread-contexts?.

self

Passed: a <GMainContext>.

a main context, or NULL for the global-default main context

Method: main-context:pusher-new self ⇒ %return
Method: pusher-new self ⇒ %return

Push self as the new thread-default main context for the current thread, using main-context:push-thread-default, and return a new GLib.MainContextPusher. Pop with g_main_context_pusher_free(). Using main-context:pop-thread-default on self 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
}
self

Passed: a <GMainContext>.

a main context to push

%return

Returned: a foreign pointer.

a GMainContextPusher

Method: main-context:query! self max-priority fds ⇒ %return timeout- fds
Method: query! self max-priority fds ⇒ %return timeout- 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 main-context:check?, as this function relies on assumptions made when the array is filled.

You must have successfully acquired the context with main-context:acquire? before you may call this function.

self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)

max-priority

Passed: a 32-bit integer.

maximum priority source to check

fds

Passed: a list or vector, each a <GPollFD>. It is filled in, and returned: a vector, each a <GPollFD>.

location to store <GPollFD> records that need to be polled

%return

Returned: a 32-bit integer.

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

timeout-

Returned: a 32-bit integer.

location to store timeout to be used in polling

n-fds

Not returned: it is the length of fds.

length of fds

Method: main-context:ref self ⇒ %return
Method: ref self ⇒ %return

Increases the reference count on a <GMainContext> object by one.

self

Passed: a <GMainContext>.

a main context

%return

Returned: a <GMainContext>.

the self that was passed in (since 2.6)

Procedure: main-context:ref-thread-default ⇒ %return

Gets a reference to the thread-default <GMainContext> for this thread

This is the same as main-context:get-thread-default, but it also adds a reference to the returned main context with main-context:ref. In addition, unlike 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.

%return

Returned: a <GMainContext>.

the thread-default main context

Method: main-context:release self
Method: release self

Releases ownership of a context previously acquired by this thread with main-context:acquire?.

If the context was acquired multiple times, the ownership will be released only when main-context:release is called as many times as it was acquired.

You must have successfully acquired the context with main-context:acquire? before you may call this function.

self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)

Method: main-context:remove-poll self fd
Method: remove-poll self fd

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

self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)

fd

Passed: a <GPollFD>.

a <GPollFD> descriptor previously added with main-context:add-poll

Method: main-context:unref self
Method: 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.

self

Passed: a <GMainContext>.

a main context

Method: main-context:wakeup self
Method: wakeup self

Wake up self if it’s currently blocking in main-context:iteration?, causing it to stop blocking.

The self could be blocking waiting for a source to become ready. Otherwise, if self is not currently blocking, this function causes the next invocation of 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 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);
self

Passed: a <GMainContext>.

a main context (if NULL, the global-default main context will be used)