#[repr(transparent)]pub struct Source { /* private fields */ }
Expand description
The GSource
struct is an opaque data type
representing an event source.
Implementations
sourceimpl Source
impl Source
sourcepub fn add_child_source(&self, child_source: &Source)
pub fn add_child_source(&self, child_source: &Source)
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”
pub fn destroy(&self)
sourcepub fn can_recurse(&self) -> bool
pub fn can_recurse(&self) -> bool
Checks whether a source is allowed to be called recursively.
see g_source_set_can_recurse()
.
Returns
whether recursion is allowed.
sourcepub fn context(&self) -> Option<MainContext>
pub fn context(&self) -> Option<MainContext>
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.
sourcepub fn ready_time(&self) -> i64
pub fn ready_time(&self) -> i64
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”
sourcepub fn time(&self) -> i64
pub fn time(&self) -> i64
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
sourcepub fn is_destroyed(&self) -> bool
pub fn is_destroyed(&self) -> bool
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
sourcepub fn remove_child_source(&self, child_source: &Source)
pub fn remove_child_source(&self, child_source: &Source)
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()
.
Trait Implementations
sourceimpl Ord for Source
impl Ord for Source
sourceimpl PartialOrd<Source> for Source
impl PartialOrd<Source> for Source
sourcefn partial_cmp(&self, other: &Source) -> Option<Ordering>
fn partial_cmp(&self, other: &Source) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl StaticType for Source
impl StaticType for Source
sourcefn static_type() -> Type
fn static_type() -> Type
Returns the type identifier of Self
.
impl Eq for Source
impl Send for Source
impl StructuralEq for Source
impl StructuralPartialEq for Source
impl Sync for Source
Auto Trait Implementations
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> StaticTypeExt for T where
T: StaticType,
impl<T> StaticTypeExt for T where
T: StaticType,
sourcefn ensure_type()
fn ensure_type()
Ensures that the type has been registered with the type system.
sourceimpl<T> ToClosureReturnValue for T where
T: ToValue,
impl<T> ToClosureReturnValue for T where
T: ToValue,
fn to_closure_return_value(&self) -> Option<Value>
sourceimpl<T> ToSendValue for T where
T: Send + ToValue + ?Sized,
impl<T> ToSendValue for T where
T: Send + ToValue + ?Sized,
sourcefn to_send_value(&self) -> SendValue
fn to_send_value(&self) -> SendValue
Returns a SendValue
clone of self
.