Struct MainContext

Source
pub struct MainContext { /* private fields */ }
Expand description

Binding to the underlying GMainContext type.

Implementations§

Source§

impl MainContext

Source

pub fn new() -> MainContext

Creates a new context to execute within.

Source

pub fn default<F, R>(f: F) -> R
where 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.

Source

pub fn thread_default<F, R>(f: F) -> R
where F: FnOnce(Option<&MainContext>) -> 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 Sources 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.)

Source

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.

Source

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 .

Source

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.

Source

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

Source§

fn clone(&self) -> MainContext

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Drop for MainContext

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for MainContext

Source§

impl Sync for MainContext

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.