Struct glib::Source[][src]

pub struct Source(_);
Expand description

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

Implementations

Adds child_source to self as a “polled” source; when self is added to a MainContext, 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. (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 g_source_set_dummy_callback() on it to set a callback that does nothing (except return true if appropriate).

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 Source. Do not call this API on a Source that you did not create.

child_source

a second Source that self should “poll”

Removes a source from its MainContext, if any, and mark 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 Source: if you still hold a reference, use g_source_unref() to drop it.

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

Checks whether a source is allowed to be called recursively. see g_source_set_can_recurse().

Returns

whether recursion is allowed.

Gets the MainContext with which the source is associated.

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

Returns

the MainContext with which the source is associated, or None if the context has not yet been added to a source.

Gets a name for the source, used in debugging and profiling. The name may be None if it has never been set with g_source_set_name().

Returns

the name of the source

Gets the priority of a source.

Returns

the priority of the source

Gets the “ready time” of self, as set by g_source_set_ready_time().

Any time before the current monotonic time (including 0) is an indication that the source will fire immediately.

Returns

the monotonic ready time, -1 for “never”

Gets the time to be used when checking this source. The advantage of calling this function over calling 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 monotonic_time().

Returns

the monotonic time in microseconds

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.

⚠️ The following code is in C ⚠️

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.

⚠️ The following code is in C ⚠️

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 MainContext the Source 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.

Returns

true if the source has been destroyed

Detaches child_source from self and destroys it.

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

child_source

a Source previously passed to add_child_source().

Adds a Source to a context so that it will be executed within that context. Remove it by calling g_source_destroy().

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

context

a MainContext (if None, the default context will be used)

Returns

the ID (greater than 0) for the source within the MainContext.

Removes the source with the given ID from the default main context. You must use g_source_destroy() for sources added to a non-default main context.

The ID of a Source is given by g_source_get_id(), or will be returned by the functions g_source_attach(), g_idle_add(), g_idle_add_full(), g_timeout_add(), g_timeout_add_full(), g_child_watch_add(), g_child_watch_add_full(), g_io_add_watch(), and g_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 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.

tag

the ID of the source to remove.

Returns

For historical reasons, this function always returns true

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Returns the type identifier of Self.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Returns a SendValue clone of self.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.