pub struct MainContext { /* private fields */ }
Expand description
Binding to the underlying GMainContext
type.
Implementations§
Source§impl MainContext
impl MainContext
Sourcepub fn new() -> MainContext
pub fn new() -> MainContext
Creates a new context to execute within.
Sourcepub fn default<F, R>(f: F) -> Rwhere
F: FnOnce(&MainContext) -> R,
pub fn default<F, R>(f: F) -> Rwhere
F: FnOnce(&MainContext) -> R,
Acquires a reference to this thread’s default 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.
Sourcepub fn thread_default<F, R>(f: F) -> R
pub fn thread_default<F, R>(f: F) -> R
Gets the thread-default MainContext
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 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 yield None
if you are running in the default thread.)
Sourcepub fn try_lock(&self) -> Option<LockedMainContext<'_>>
pub fn try_lock(&self) -> Option<LockedMainContext<'_>>
Attempts to become the owner of the specified context.
If some other thread is the owner of the context, returns Err
immediately. Ownership is properly recursive: the owner can require
ownership again.
If the context is successfully locked then a locked version is
returned, otherwise an Err
is returned with this context.
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 context .
Sourcepub fn wakeup(&self)
pub fn wakeup(&self)
If context 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.
Sourcepub fn push_thread_default(&self) -> PushThreadDefault<'_>
pub fn push_thread_default(&self) -> PushThreadDefault<'_>
Acquires context and sets it as the thread-default context for the current thread.
This will cause certain asynchronous operations (such as most gio-based
I/O) which are started in this thread to run under context and deliver
their results to its main loop, rather than running under the global
default context in the main thread. Note that calling this function
changes the context returned by thread_default
not the one returned
by default
.
Normally you would call this function shortly after creating a new
thread, passing it a MainContext
which will be run by a MainLoop
in
that thread, to set a new default context for all async operations in
that thread.
If you don’t have control over how the new thread was created (e.g. in
the new thread isn’t newly created, or if the thread life cycle is
managed by a ThreadPool
), it is always suggested to wrap the logic
that needs to use the new MainContext
inside push_thread_default
block.
otherwise threads that are re-used will end up never explicitly
releasing the MainContext
reference they hold.
In some cases you may want to schedule a single operation in a
non-default context, or temporarily use a non-default context in the
main thread. In that case, you can wrap the call to the asynchronous
operation inside a push_thread_default
block, but it is up to you to
ensure that no other asynchronous operations accidentally get started
while the non-default context is active.
This context will be popped from the default scope when the returned
PushThreadDefault
value goes out of scope.
Trait Implementations§
Source§impl Clone for MainContext
impl Clone for MainContext
Source§fn clone(&self) -> MainContext
fn clone(&self) -> MainContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more