1.25 <GSource>

Structure: <GSource>

The <GSource> struct is an opaque data type representing an event source.

Procedures

Method: source:add-child-source self child-source
Method: add-child-source self child-source

Adds child-source to self as a ‘polled’ source.

When self is added to a <GMainContext>, child-source will be automatically added with the same priority. When child-source is triggered, it will cause self to dispatch (in addition to calling its own callback), and when self is destroyed, it will destroy child-source as well.

The self will also still be dispatched if its own prepare/check functions indicate that it is ready.

If you don’t need child-source to do anything on its own when it triggers, you can call source-set-dummy-callback on it to set a callback that does nothing (except return true if appropriate).

The self will hold a reference on child-source while child-source is attached to it.

This API is only intended to be used by implementations of <GSource>. Do not call this API on a <GSource> that you did not create.

self

Passed: a <GSource>.

a source

child-source

Passed: a <GSource>.

a second source that self should ‘poll’

Method: source:add-poll self fd
Method: add-poll self fd

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

This is usually combined with GLib.Source.new to add an event source. The event source’s check function will typically test the revents field in the <GPollFD> struct and return true if events need to be processed.

This API is only intended to be used by implementations of <GSource>. Do not call this API on a <GSource> that you did not create.

Using this API forces the linear scanning of event sources on each main loop iteration. Newly-written event sources should try to use source:add-unix-fd instead of this API.

self

Passed: a <GSource>.

a source

fd

Passed: a <GPollFD>.

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

Method: source:add-unix-fd self fd events ⇒ %return
Method: add-unix-fd self fd events ⇒ %return

Monitors fd for the IO events in events.

The tag returned by this function can be used to remove or modify the monitoring of the fd using source:remove-unix-fd or source:modify-unix-fd.

It is not necessary to remove the file descriptor before destroying the source; it will be cleaned up automatically.

This API is only intended to be used by implementations of <GSource>. Do not call this API on a <GSource> that you did not create.

As the name suggests, this function is not available on Windows.

self

Passed: a <GSource>.

a source

fd

Passed: a 32-bit integer.

the file descriptor to monitor

events

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

an event mask

%return

Returned: a foreign pointer.

an opaque tag

Method: source:attach self [context] ⇒ %return
Method: attach self [context] ⇒ %return

Adds a <GSource> to a context so that it will be executed within that context.

Remove it by calling source:destroy.

This function is safe to call from any thread, regardless of which thread the context is running in.

self

Passed: a <GSource>.

a source

context

Passed, or left out: a <GMainContext>, or #f.

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

%return

Returned: an unsigned 32-bit integer.

the ID (greater than 0) for the source within the <GMainContext>

Method: source:destroy self
Method: destroy self

Removes a source from its <GMainContext>, if any, and marks it as destroyed.

The source cannot be subsequently added to another context. It is safe to call this on sources which have already been removed from their context.

This does not unref the <GSource>: if you still hold a reference, use source:unref to drop it.

This function is safe to call from any thread, regardless of which thread the <GMainContext> is running in.

If the source is currently attached to a <GMainContext>, destroying it will effectively unset the callback similar to calling source:set-callback. This can mean, that the data’s GDestroyNotify gets called right away.

self

Passed: a <GSource>.

a source

Method: source:dup-context self ⇒ %return
Method: dup-context self ⇒ %return

Gets a reference to the <GMainContext> with which the source is associated.

You can call this on a source that has been destroyed. You can always call this function on the source returned from main-current-source.

self

Passed: a <GSource>.

a source

%return

Returned: a <GMainContext>, or #f.

the <GMainContext> with which the source is associated, or NULL if the context has not yet been added to a source

Method: source:get-can-recurse? self ⇒ %return
Method: get-can-recurse? self ⇒ %return

Checks whether a source is allowed to be called recursively.

See source:set-can-recurse.

self

Passed: a <GSource>.

a source

%return

Returned: a boolean.

whether recursion is allowed

Method: source:get-context self ⇒ %return
Method: get-context self ⇒ %return

Gets the <GMainContext> with which the source is associated.

You can call this on a source that has been destroyed, provided that the <GMainContext> it was attached to still exists (in which case it will return that <GMainContext>). In particular, you can always call this function on the source returned from main-current-source. But calling this function on a source whose <GMainContext> has been destroyed is an error.

If the associated <GMainContext> could be destroy concurrently from a different thread, then this function is not safe to call and source:dup-context should be used instead.

self

Passed: a <GSource>.

a source

%return

Returned: a <GMainContext>, or #f.

the main context with which the source is associated, or NULL if the context has not yet been added to a source

Method: source:get-id self ⇒ %return
Method: get-id self ⇒ %return

Returns the numeric ID for a particular source.

The ID of a source is a positive integer which is unique within a particular main loop context. The reverse mapping from ID to source is done by main-context:find-source-by-id.

You can only call this function while the source is associated to a <GMainContext> instance; calling this function before source:attach or after source:destroy yields undefined behavior. The ID returned is unique within the <GMainContext> instance passed to source:attach.

self

Passed: a <GSource>.

a source

%return

Returned: an unsigned 32-bit integer.

the ID (greater than 0) for the source

Method: source:get-name self ⇒ %return
Method: get-name self ⇒ %return

Gets a name for the source, used in debugging and profiling.

The name may be NULL if it has never been set with source:set-name.

self

Passed: a <GSource>.

a source

%return

Returned: a string, or #f.

the name of the source

Method: source:get-priority self ⇒ %return
Method: get-priority self ⇒ %return

Gets the priority of a source.

self

Passed: a <GSource>.

a source

%return

Returned: a 32-bit integer.

the priority of the source

Method: source:get-ready-time self ⇒ %return
Method: get-ready-time self ⇒ %return

Gets the ‘ready time’ of self, as set by source:set-ready-time.

Any time before or equal to the current monotonic time (including zero) is an indication that the source will fire immediately.

self

Passed: a <GSource>.

a source

%return

Returned: a 64-bit integer.

the monotonic ready time, -1 for ‘never’

Method: source:get-time self ⇒ %return
Method: get-time self ⇒ %return

Gets the time to be used when checking this source.

The advantage of calling this function over calling get-monotonic-time directly is that when checking multiple sources, GLib can cache a single value instead of having to repeatedly get the system monotonic time.

The time here is the system monotonic time, if available, or some other reasonable alternative otherwise. See get-monotonic-time.

self

Passed: a <GSource>.

a source

%return

Returned: a 64-bit integer.

the monotonic time in microseconds

Method: source:is-destroyed? self ⇒ %return
Method: is-destroyed? self ⇒ %return

Returns whether self has been destroyed.

This is important when you operate upon your objects from within idle handlers, but may have freed the object before the dispatch of your idle handler.

static gboolean
idle_callback (gpointer data)
{
  SomeWidget *self = data;
   
  g_mutex_lock (&self->idle_id_mutex);
  // do stuff with self
  self->idle_id = 0;
  g_mutex_unlock (&self->idle_id_mutex);
   
  return G_SOURCE_REMOVE;
}
 
static void
some_widget_do_stuff_later (SomeWidget *self)
{
  g_mutex_lock (&self->idle_id_mutex);
  self->idle_id = g_idle_add (idle_callback, self);
  g_mutex_unlock (&self->idle_id_mutex);
}
 
static void
some_widget_init (SomeWidget *self)
{
  g_mutex_init (&self->idle_id_mutex);

  // ...
}

static void
some_widget_finalize (GObject *object)
{
  SomeWidget *self = SOME_WIDGET (object);
   
  if (self->idle_id)
    g_source_remove (self->idle_id);
   
  g_mutex_clear (&self->idle_id_mutex);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

This will fail in a multi-threaded application if the widget is destroyed before the idle handler fires due to the use after free in the callback. A solution, to this particular problem, is to check to if the source has already been destroy within the callback.

static gboolean
idle_callback (gpointer data)
{
  SomeWidget *self = data;
  
  g_mutex_lock (&self->idle_id_mutex);
  if (!g_source_is_destroyed (g_main_current_source ()))
    {
      // do stuff with self
    }
  g_mutex_unlock (&self->idle_id_mutex);
  
  return FALSE;
}

Calls to this function from a thread other than the one acquired by the <GMainContext> the <GSource> is attached to are typically redundant, as the source could be destroyed immediately after this function returns. However, once a source is destroyed it cannot be un-destroyed, so this function can be used for opportunistic checks from any thread.

self

Passed: a <GSource>.

a source

%return

Returned: a boolean.

true if the source has been destroyed, false otherwise

Method: source:modify-unix-fd self tag new-events
Method: modify-unix-fd self tag new-events

Updates the event mask to watch for the file descriptor identified by tag.

The tag is the tag returned from source:add-unix-fd.

If you want to remove a file descriptor, don’t set its event mask to zero. Instead, call source:remove-unix-fd.

This API is only intended to be used by implementations of <GSource>. Do not call this API on a <GSource> that you did not create.

As the name suggests, this function is not available on Windows.

self

Passed: a <GSource>.

a source

tag

Passed: a foreign pointer, or a bytevector.

the tag from source:add-unix-fd

new-events

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

the new event mask to watch

Method: source:query-unix-fd self tag ⇒ %return
Method: query-unix-fd self tag ⇒ %return

Queries the events reported for the file descriptor corresponding to tag on self during the last poll.

The return value of this function is only defined when the function is called from the check or dispatch functions for self.

This API is only intended to be used by implementations of <GSource>. Do not call this API on a <GSource> that you did not create.

As the name suggests, this function is not available on Windows.

self

Passed: a <GSource>.

a source

tag

Passed: a foreign pointer, or a bytevector.

the tag from source:add-unix-fd

%return

Returned: a <GIOCondition>.

the conditions reported on the file descriptor

Method: source:ref self ⇒ %return
Method: ref self ⇒ %return

Increases the reference count on a source by one.

self

Passed: a <GSource>.

a source

%return

Returned: a <GSource>.

self

Procedure: source:remove-by-user-data? [user-data] ⇒ %return

Removes a source from the default main loop context given the user data for the callback.

If multiple sources exist with the same user data, only one will be destroyed.

user-data

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

the user_data for the callback

%return

Returned: a boolean.

true if a source was found and removed, false otherwise

Method: source:remove-child-source self child-source
Method: remove-child-source self child-source

Detaches child-source from self and destroys it.

This API is only intended to be used by implementations of <GSource>. Do not call this API on a <GSource> that you did not create.

self

Passed: a <GSource>.

a source

child-source

Passed: a <GSource>.

a source previously passed to source:add-child-source

Method: source:remove-poll self fd
Method: remove-poll self fd

Removes a file descriptor from the set of file descriptors polled for this source.

This API is only intended to be used by implementations of <GSource>. Do not call this API on a <GSource> that you did not create.

self

Passed: a <GSource>.

a source

fd

Passed: a <GPollFD>.

a <GPollFD> structure previously passed to source:add-poll

Method: source:remove-unix-fd self tag
Method: remove-unix-fd self tag

Reverses the effect of a previous call to source:add-unix-fd.

You only need to call this if you want to remove a file descriptor from being watched while keeping the same source around. In the normal case you will just want to destroy the source.

This API is only intended to be used by implementations of <GSource>. Do not call this API on a <GSource> that you did not create.

As the name suggests, this function is not available on Windows.

self

Passed: a <GSource>.

a source

tag

Passed: a foreign pointer, or a bytevector.

the tag from source:add-unix-fd

Procedure: source:remove? tag ⇒ %return

Removes the source with the given ID from the default main context.

You must use source:destroy for sources added to a non-default main context.

The ID of a <GSource> is given by source:get-id, or will be returned by the functions source:attach, idle-add, GLib.idle_add_full, timeout-add, GLib.timeout_add_full, child-watch-add, GLib.child_watch_add_full, io-add-watch, and GLib.io_add_watch_full.

It is a programmer error to attempt to remove 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.

tag

Passed: an unsigned 32-bit integer.

the ID of the source to remove.

%return

Returned: a boolean.

true if the source was found and removed, false otherwise

Method: source:set-callback self func
Method: set-callback self func

Sets the callback function for a source. The callback for a source is called from the source’s dispatch function.

The exact type of func depends on the type of source; ie. you should not count on func being called with data as its first parameter. Cast func with GLib.SOURCE_FUNC to avoid warnings about incompatible function types.

See main loop memory management for details on how to handle memory management of data.

Typically, you won’t use this function. Instead use functions specific to the type of source you are using, such as idle-add or timeout-add.

It is safe to call this function multiple times on a source which has already been attached to a context. The changes will take effect for the next time the source is dispatched after this call returns.

Note that source:destroy for a currently attached source has the effect of also unsetting the callback.

self

Passed: a <GSource>.

the source

func

Passed: a procedure.

a callback function

See GSourceFunc for how it is called.

data

Not passed: guile-gi supplies it.

the data to pass to callback 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: source:set-can-recurse self can-recurse
Method: set-can-recurse self can-recurse

Sets whether a source can be called recursively.

If can-recurse is true, then while the source is being dispatched then this source will be processed normally. Otherwise, all processing of this source is blocked until the dispatch function returns.

self

Passed: a <GSource>.

a source

can-recurse

Passed: a boolean.

whether recursion is allowed for this source

Method: source:set-name self name
Method: set-name self name

Sets a name for the source, used in debugging and profiling.

The name defaults to NULL.

The source name should describe in a human-readable way what the source does. For example, ‘X11 event queue’ or ‘GTK repaint idle handler’.

It is permitted to call this function multiple times, but is not recommended due to the potential performance impact. For example, one could change the name in the check function of a <GSourceFuncs> to include details like the event type in the source name.

Use caution if changing the name while another thread may be accessing it with source:get-name; that function does not copy the value, and changing the value will free it while the other thread may be attempting to use it.

Also see source:set-static-name.

self

Passed: a <GSource>.

a source

name

Passed: a string.

debug name for the source

Procedure: source:set-name-by-id tag name

Sets the name of a source using its ID.

This is a convenience utility to set source names from the return value of idle-add, timeout-add, etc.

It is a programmer error to attempt to set the name of 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.

tag

Passed: an unsigned 32-bit integer.

a source ID

name

Passed: a string.

debug name for the source

Method: source:set-priority self priority
Method: set-priority self priority

Sets the priority of a source.

While the main loop is being run, a source will be dispatched if it is ready to be dispatched and no sources at a higher (numerically smaller) priority are ready to be dispatched.

A child source always has the same priority as its parent. It is not permitted to change the priority of a source once it has been added as a child of another source.

self

Passed: a <GSource>.

a source

priority

Passed: a 32-bit integer.

the new priority

Method: source:set-ready-time self ready-time
Method: set-ready-time self ready-time

Sets a source to be dispatched when the given monotonic time is reached (or passed).

If the monotonic time is in the past (as it always will be if ready-time is 0) then the source will be dispatched immediately.

If ready-time is -1 then the source is never woken up on the basis of the passage of time.

Dispatching the source does not reset the ready time. You should do so yourself, from the source dispatch function.

Note that if you have a pair of sources where the ready time of one suggests that it will be delivered first but the priority for the other suggests that it would be delivered first, and the ready time for both sources is reached during the same main context iteration, then the order of dispatch is undefined.

It is a no-op to call this function on a <GSource> which has already been destroyed with source:destroy.

This API is only intended to be used by implementations of <GSource>. Do not call this API on a <GSource> that you did not create.

self

Passed: a <GSource>.

a source

ready-time

Passed: a 64-bit integer.

the monotonic time at which the source will be ready; 0 for ‘immediately’, -1 for ‘never’

Method: source:set-static-name self name
Method: set-static-name self name

A variant of source:set-name that does not duplicate the name, and can only be used with string literals.

self

Passed: a <GSource>.

a source

name

Passed: a string.

debug name for the source

Method: source:unref self
Method: unref self

Decreases the reference count of a source by one.

If the resulting reference count is zero the source and associated memory will be destroyed.

self

Passed: a <GSource>.

a source