pub struct Source { /* private fields */ }
Expand description
The GSource
struct is an opaque data type
representing an event source.
GLib type: Shared boxed type with reference counted clone semantics.
Implementations§
Source§impl 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 #GSource 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 GLib::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 #GMainContext with which the
source is associated, or None
if the context has not
yet been added to a source.
Sourcepub fn name(&self) -> Option<GString>
pub fn name(&self) -> Option<GString>
Gets a name for the source, used in debugging and profiling. The
name may be #NULL if it has never been set with GLib::Source::set_name()
.
§Returns
the name of the source
Sourcepub fn ready_time(&self) -> i64
pub fn ready_time(&self) -> i64
Gets the “ready time” of @self, as set by
GLib::Source::set_ready_time()
.
Any time before or equal to 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 #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.
§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 #GSource. Do not call this API on a #GSource that you did not create.
§child_source
a #GSource previously passed to
add_child_source()
.
Trait Implementations§
Source§impl HasParamSpec for Source
impl HasParamSpec for Source
Source§impl Ord for Source
impl Ord for Source
Source§impl PartialOrd for Source
impl PartialOrd for Source
Source§impl StaticType for Source
impl StaticType for Source
Source§fn static_type() -> Type
fn static_type() -> Type
Self
.impl Eq for Source
impl Send for Source
impl StructuralPartialEq for Source
impl Sync for Source
Auto Trait Implementations§
impl Freeze for Source
impl RefUnwindSafe for Source
impl Unpin for Source
impl UnwindSafe for Source
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
Source§impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
Source§impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
Source§impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
Source§impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
Source§impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
Source§impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
Source§impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
Source§impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
Source§impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
Source§impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
Source§impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for Twhere
T: GlibPtrDefault + FromGlibPtrNone<<T as GlibPtrDefault>::GlibType> + FromGlibPtrFull<<T as GlibPtrDefault>::GlibType>,
Source§impl<T> IntoClosureReturnValue for T
impl<T> IntoClosureReturnValue for T
fn into_closure_return_value(self) -> Option<Value>
Source§impl<T> PropertyGet for Twhere
T: HasParamSpec,
impl<T> PropertyGet for Twhere
T: HasParamSpec,
Source§impl<T> StaticTypeExt for Twhere
T: StaticType,
impl<T> StaticTypeExt for Twhere
T: StaticType,
Source§fn ensure_type()
fn ensure_type()
Source§impl<T> ToSendValue for T
impl<T> ToSendValue for T
Source§fn to_send_value(&self) -> SendValue
fn to_send_value(&self) -> SendValue
SendValue
clone of self
.