pub struct MainContext { /* private fields */ }
Expand description
The GMainContext
struct is an opaque data
type representing a set of sources to be handled in a main loop.
GLib type: Shared boxed type with reference counted clone semantics.
Implementations§
Source§impl MainContext
impl MainContext
Sourcepub fn as_ptr(&self) -> *mut GMainContext
pub fn as_ptr(&self) -> *mut GMainContext
Return the inner pointer to the underlying C value.
Sourcepub unsafe fn from_glib_ptr_borrow(ptr: &*mut GMainContext) -> &Self
pub unsafe fn from_glib_ptr_borrow(ptr: &*mut GMainContext) -> &Self
Borrows the underlying C value.
Source§impl MainContext
impl MainContext
Sourcepub fn new() -> MainContext
pub fn new() -> MainContext
Sourcepub fn with_flags(flags: MainContextFlags) -> MainContext
Available on crate feature v2_72
only.
pub fn with_flags(flags: MainContextFlags) -> MainContext
v2_72
only.Creates a new MainContext
structure.
§flags
a bitwise-OR combination of #GMainContextFlags flags that can only be set at creation time.
§Returns
the new #GMainContext
Sourcepub fn is_owner(&self) -> bool
pub fn is_owner(&self) -> bool
Determines whether this thread holds the (recursive)
ownership of this MainContext
. This is useful to
know before waiting on another thread that may be
blocking to get ownership of @self.
§Returns
true
if current thread is owner of @self.
Sourcepub fn iteration(&self, may_block: bool) -> bool
pub fn iteration(&self, may_block: bool) -> bool
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
sources are not waited to become ready, only those highest priority
events sources will be dispatched (if any), that are ready at this
given moment without further waiting.
Note that even when @may_block is true
, it is still possible for
iteration()
to return false
, since the wait may
be interrupted for other reasons than an event source becoming ready.
§may_block
whether the call may block.
§Returns
true
if events were dispatched.
Sourcepub fn wakeup(&self)
pub fn wakeup(&self)
If @self is currently blocking in iteration()
waiting for a source to become ready, cause it to stop blocking
and return. Otherwise, cause the next invocation of
iteration()
to return without blocking.
This API is useful for low-level control over MainContext
; for
example, integrating it with main loop implementations such as
MainLoop
.
Another related use for this function is when implementing a main loop with a termination condition, computed from multiple threads:
⚠️ The following code is in C ⚠️
#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:
⚠️ The following code is in C ⚠️
perform_work();
if (g_atomic_int_dec_and_test (&tasks_remaining))
g_main_context_wakeup (NULL);
Sourcepub fn default() -> MainContext
pub fn default() -> MainContext
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
thread_default()
.
§Returns
the global-default main context.
Sourcepub fn thread_default() -> Option<MainContext>
pub fn thread_default() -> Option<MainContext>
Gets the thread-default #GMainContext 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
ref_thread_default()
to get a
MainContext
to add their Source
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 None
if you are running in the default thread.)
If you need to hold a reference on the context, use
ref_thread_default()
instead.
§Returns
the thread-default #GMainContext, or
None
if the thread-default context is the global-default main context.
Sourcepub fn ref_thread_default() -> MainContext
pub fn ref_thread_default() -> MainContext
Gets the thread-default MainContext
for this thread, as with
thread_default()
, but also adds a reference to
it with GLib::MainContext::ref()
. In addition, unlike
thread_default()
, if the thread-default context
is the global-default context, this will return that
MainContext
(with a ref added to it) rather than returning
None
.
§Returns
the thread-default #GMainContext. Unref
with GLib::MainContext::unref()
when you are done with it.
Source§impl MainContext
impl MainContext
Sourcepub fn prepare(&self) -> (bool, i32)
pub fn prepare(&self) -> (bool, i32)
Prepares to poll sources within a main loop. The resulting information
for polling is determined by calling GLib::MainContext::query()
.
You must have successfully acquired the context with
acquire()
before you may call this function.
§Returns
true
if some source is ready to be dispatched
prior to polling.
§priority
location to store priority of highest priority source already ready.
Sourcepub fn find_source_by_id(&self, source_id: &SourceId) -> Option<Source>
pub fn find_source_by_id(&self, source_id: &SourceId) -> Option<Source>
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.
§source_id
the source ID, as returned by GLib::Source::get_id()
.
§Returns
the #GSource
Sourcepub fn invoke<F>(&self, func: F)
pub fn invoke<F>(&self, func: F)
Invokes func
on the main context.
If the current thread is the owner of the main context or the main context currently has no
owner then func
will be called directly from inside this function. If this behaviour is
not desired and func
should always be called asynchronously then use MainContext::spawn
glib::idle_add
instead.
Invokes a function in such a way that @self is owned during the
invocation of @function.
If @self is None
then the global-default main context — as
returned by default()
— is used.
If @self is owned by the current thread, @function is called
directly. Otherwise, if @self is the thread-default main context
of the current thread and acquire()
succeeds, then
@function is called and [release()
][Self::release()] is called
afterwards.
In any other case, an idle source is created to call @function and
that source is attached to @self (presumably to be run in another
thread). The idle source is attached with GLib::PRIORITY_DEFAULT
priority. If you want a different priority, use
[invoke_full()
][Self::invoke_full()].
Note that, as with normal idle functions, @function should probably
return false
. If it returns true
, it will be continuously run in a
loop (and may prevent this call from returning).
§function
function to call
Sourcepub fn invoke_with_priority<F>(&self, priority: Priority, func: F)
pub fn invoke_with_priority<F>(&self, priority: Priority, func: F)
Invokes func
on the main context with the given priority.
If the current thread is the owner of the main context or the main context currently has no
owner then func
will be called directly from inside this function. If this behaviour is
not desired and func
should always be called asynchronously then use MainContext::spawn
glib::idle_add
instead.
Sourcepub fn invoke_local<F>(&self, func: F)where
F: FnOnce() + 'static,
pub fn invoke_local<F>(&self, func: F)where
F: FnOnce() + 'static,
Invokes func
on the main context.
Different to invoke()
, this does not require func
to be
Send
but can only be called from the thread that owns the main context.
This function panics if called from a different thread than the one that owns the main context.
Note that this effectively means that func
is called directly from inside this function
or otherwise panics immediately. If this behaviour is not desired and func
should always
be called asynchronously then use MainContext::spawn_local
glib::idle_add_local
instead.
Sourcepub fn invoke_local_with_priority<F>(&self, _priority: Priority, func: F)where
F: FnOnce() + 'static,
pub fn invoke_local_with_priority<F>(&self, _priority: Priority, func: F)where
F: FnOnce() + 'static,
Invokes func
on the main context with the given priority.
Different to invoke_with_priority()
, this does not require func
to be
Send
but can only be called from the thread that owns the main context.
This function panics if called from a different thread than the one that owns the main context.
Note that this effectively means that func
is called directly from inside this function
or otherwise panics immediately. If this behaviour is not desired and func
should always
be called asynchronously then use MainContext::spawn_local
glib::idle_add_local
instead.
Sourcepub fn with_thread_default<R, F: FnOnce() -> R + Sized>(
&self,
func: F,
) -> Result<R, BoolError>
pub fn with_thread_default<R, F: FnOnce() -> R + Sized>( &self, func: F, ) -> Result<R, BoolError>
Call closure with the main context configured as the thread default one.
The thread default main context is changed in a panic-safe manner before calling func
and
released again afterwards regardless of whether closure panicked or not.
This will fail if the main context is owned already by another thread.
Sourcepub fn acquire(&self) -> Result<MainContextAcquireGuard<'_>, BoolError>
pub fn acquire(&self) -> Result<MainContextAcquireGuard<'_>, BoolError>
Acquire ownership of the main context.
Ownership will automatically be released again once the returned acquire guard is dropped.
This will fail if the main context is owned already by another thread.
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 [release()
][Self::release()]
is called as many times as acquire()
.
You must be the owner of a context before you
can call prepare()
, GLib::MainContext::query()
,
GLib::MainContext::check()
, dispatch()
,
[release()
][Self::release()].
Since 2.76 @self can be None
to use the global-default
main context.
§Returns
true
if the operation succeeded, and
this thread is now the owner of @self.
Source§impl MainContext
impl MainContext
Sourcepub fn spawn<R: Send + 'static, F: Future<Output = R> + Send + 'static>(
&self,
f: F,
) -> JoinHandle<R> ⓘ
pub fn spawn<R: Send + 'static, F: Future<Output = R> + Send + 'static>( &self, f: F, ) -> JoinHandle<R> ⓘ
Spawn a new infallible Future
on the main context.
This can be called from any thread and will execute the future from the thread
where main context is running, e.g. via a MainLoop
.
Sourcepub fn spawn_local<R: 'static, F: Future<Output = R> + 'static>(
&self,
f: F,
) -> JoinHandle<R> ⓘ
pub fn spawn_local<R: 'static, F: Future<Output = R> + 'static>( &self, f: F, ) -> JoinHandle<R> ⓘ
Spawn a new infallible Future
on the main context.
The given Future
does not have to be Send
.
This can be called only from the thread where the main context is running, e.g.
from any other Future
that is executed on this main context, or after calling
with_thread_default
or acquire
on the main context.
Sourcepub fn spawn_with_priority<R: Send + 'static, F: Future<Output = R> + Send + 'static>(
&self,
priority: Priority,
f: F,
) -> JoinHandle<R> ⓘ
pub fn spawn_with_priority<R: Send + 'static, F: Future<Output = R> + Send + 'static>( &self, priority: Priority, f: F, ) -> JoinHandle<R> ⓘ
Spawn a new infallible Future
on the main context, with a non-default priority.
This can be called from any thread and will execute the future from the thread
where main context is running, e.g. via a MainLoop
.
Sourcepub fn spawn_local_with_priority<R: 'static, F: Future<Output = R> + 'static>(
&self,
priority: Priority,
f: F,
) -> JoinHandle<R> ⓘ
pub fn spawn_local_with_priority<R: 'static, F: Future<Output = R> + 'static>( &self, priority: Priority, f: F, ) -> JoinHandle<R> ⓘ
Spawn a new infallible Future
on the main context, with a non-default priority.
The given Future
does not have to be Send
.
This can be called only from the thread where the main context is running, e.g.
from any other Future
that is executed on this main context, or after calling
with_thread_default
or acquire
on the main context.
Sourcepub fn spawn_from_within<R: Send + 'static, F: Future<Output = R> + 'static>(
&self,
func: impl FnOnce() -> F + Send + 'static,
) -> SpawnWithinJoinHandle<R> ⓘ
pub fn spawn_from_within<R: Send + 'static, F: Future<Output = R> + 'static>( &self, func: impl FnOnce() -> F + Send + 'static, ) -> SpawnWithinJoinHandle<R> ⓘ
Spawn a new infallible Future
on the main context from inside the main context.
The given Future
does not have to be Send
but the closure to spawn it has to be.
This can be called only from any thread.
Sourcepub fn spawn_from_within_with_priority<R: Send + 'static, F: Future<Output = R> + 'static>(
&self,
priority: Priority,
func: impl FnOnce() -> F + Send + 'static,
) -> SpawnWithinJoinHandle<R> ⓘ
pub fn spawn_from_within_with_priority<R: Send + 'static, F: Future<Output = R> + 'static>( &self, priority: Priority, func: impl FnOnce() -> F + Send + 'static, ) -> SpawnWithinJoinHandle<R> ⓘ
Spawn a new infallible Future
on the main context from inside the main context.
The given Future
does not have to be Send
but the closure to spawn it has to be.
This can be called only from any thread.
Sourcepub fn block_on<F: Future>(&self, f: F) -> F::Output
pub fn block_on<F: Future>(&self, f: F) -> F::Output
Runs a new, infallible Future
on the main context and block until it finished, returning
the result of the Future
.
The given Future
does not have to be Send
or 'static
.
This must only be called if no MainLoop
or anything else is running on this specific main
context.
Trait Implementations§
Source§impl Clone for MainContext
impl Clone for MainContext
Source§impl Debug for MainContext
impl Debug for MainContext
Source§impl Default for MainContext
impl Default for MainContext
Source§impl From<MainContext> for Value
impl From<MainContext> for Value
Source§fn from(s: MainContext) -> Self
fn from(s: MainContext) -> Self
Source§impl HasParamSpec for MainContext
impl HasParamSpec for MainContext
type ParamSpec = ParamSpecBoxed
Source§type SetValue = MainContext
type SetValue = MainContext
type BuilderFn = fn(_: &str) -> ParamSpecBoxedBuilder<'_, MainContext>
fn param_spec_builder() -> Self::BuilderFn
Source§impl Hash for MainContext
impl Hash for MainContext
Source§impl LocalSpawn for MainContext
impl LocalSpawn for MainContext
Source§impl Ord for MainContext
impl Ord for MainContext
Source§fn cmp(&self, other: &MainContext) -> Ordering
fn cmp(&self, other: &MainContext) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for MainContext
impl PartialEq for MainContext
Source§impl PartialOrd for MainContext
impl PartialOrd for MainContext
Source§impl Spawn for MainContext
impl Spawn for MainContext
Source§impl StaticType for MainContext
impl StaticType for MainContext
Source§fn static_type() -> Type
fn static_type() -> Type
Self
.impl Eq for MainContext
impl Send for MainContext
impl StructuralPartialEq for MainContext
impl Sync for MainContext
Auto Trait Implementations§
impl Freeze for MainContext
impl RefUnwindSafe for MainContext
impl Unpin for MainContext
impl UnwindSafe for MainContext
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
.