Skip to main content

Condvar

Struct Condvar 

Source
pub struct Condvar(/* private fields */);
Expand description

Condition variable (for async)

For background information about of the semantics of a condition variable, see Wikipedia and/or std::sync::Condvar.

Unlike std’s condition variables, async-condvar-fair’s do not block the current thread, only the current async task. Also, multiple condition variables may be freely used with multiple different mutexes.

Like all condition variables, async-condvar-wait’s may generate spurious wakeups. After completing a wait, you must re-check the actual condition in your data structure, not simply assume that it must be ready for you.

Implementations§

Source§

impl Condvar

Source

pub fn new() -> Condvar

Source§

impl Condvar

Source

pub fn wait_baton<'c, G>(&'c self, guard: G) -> Waiter<'c, G>

Wait for someone to call notify_one or notify_all

Atomically unlocks the mutex corresponding to guard and starts waiting for notify.

This is a future producing (G, Option< Baton <'c>>).

G is a fresh guard for the mutex, which has been unlocked and relocked.

baton is a token representing an possible obligation to either perform the actions that the caller of notify_one is expecting, or re-notify the condvar. See Baton. baton will be None if the wakeup was the result of notify_all.

Source

pub async fn wait<'c, G>( &'c self, guard: G, ) -> <G as RelockMutexGuard>::JustGuard

Wait for a notification; caller must worry about cancellation

Like wait_baton but disposes of the baton right away on return.

§Beware task cancellation when using with notify_one and an async mutex

When wait, notify_one, and an async mutex, are combined, notifications can easily be lost: perhaps a task calling wait could be cancelled after being woken up by notify_one, but before doing the actual work. If that happens, probably no other task will be signaled and the work would go undone.

So when using notify_one, with an async mutex, it is probably best to use wait_baton.

async-condvar-fair does guarantee that notify_one will ensure that at least one wait call returns to its caller, (In particular, if the wait task is cancelled after being selected by notify_one, but before it manages to acquire the mutex, the condvar will be re-notified.)

But, in async code it is difficult and error-prone to try to avoid waiting. Any await might result in task cancellaton, and then if you’re not using wait_baton, the notification will be lost.

wait_baton avoids this problem. notify_all doesn’t suffer from it because everyone is woken up anyway. If you are using a sync mutex, there is no problem either, because you won’t be awaiting while processing (ie, while holding the mutex) anyway.

Source

pub async fn wait_no_relock<'c, G>(&'c self, guard: G) -> Option<Baton<'c>>

Wait for notification; caller will have to relock the mutex

Like wait_baton but does not relock the mutex.

This can be used with any mutex guard type, even one for which no impl of RelockMutexGuard is available.

wait_no_relock will first start waiting for notifications, and then drop guard (which, with a mutex guard, will unlock the mutex). When wait_no_relock completes, you will very probably want to acquire the mutex again: with wait_no_relock this must be done separately.

Be sure to dispose of the Option<Baton> exactly iff that is appropriate.

§Deadlock hazard

There is no type restricton on guard. It is important that you pass the ownership of the actual mutex guard. There is no way for the compiler to spot if you don’t. If (for example) you pass &mut MutexGuard, you will fail to unlock the mutex, usually resulting in deadlock.

Source§

impl Condvar

Source

pub fn notify_one(&self)

Notify a waiting task (aka “signal”)

If there are any tasks in wait_baton (or wait_no_relock), at least one of them will wake up and get Some(Baton) from wait_baton.

Likewise, if there are any tasks in wait, at least one of them will wake up and return. But if that task is cancelled after wait completss, the notification may be lost. See wait and Baton for a discussion of the interaction between task cancellation and notify_one.

Notifications do not “stack” or “count”. Calling notify_one several times might still wake up only one task.

Source§

impl Condvar

Source

pub fn notify_all(&self)

Notify all waiting tasks (aka “broadcast”)

Wake up all tasks currently in wait, wait_baton, and wait_no_relock,

Each the tasks in wait and wait_baton will start to try to reacquire the mutex; they will then (in general) take turns to return from wait/wait_baton with the mutex held.

All tasks will get None rather than Some( Baton ), from wait_baton or wait_no_relock - even possibly tasks which are in the process of waking up because of a previous call to notify_one.

Source§

impl Condvar

Source

pub fn make_baton<'c>(&'c self) -> Baton<'c>

Make a baton directly, without waiting

This may be useful in unusual situations.

If the returned Baton is simply dropped, this is the same as notify_one.

Trait Implementations§

Source§

impl Debug for Condvar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Condvar

Source§

fn default() -> Condvar

Returns the “default value” for a type. Read more

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> DebugExt<T> for T
where T: Debug,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<A> DynCastExt for A

Source§

fn dyn_cast<T>( self, ) -> Result<<A as DynCastExtHelper<T>>::Target, <A as DynCastExtHelper<T>>::Source>
where A: DynCastExtHelper<T>, T: ?Sized,

Use this to cast from one trait object type to another. Read more
Source§

fn dyn_upcast<T>(self) -> <A as DynCastExtAdvHelper<T, T>>::Target
where A: DynCastExtAdvHelper<T, T, Source = <A as DynCastExtAdvHelper<T, T>>::Target>, T: ?Sized,

Use this to upcast a trait to one of its supertraits. Read more
Source§

fn dyn_cast_adv<F, T>( self, ) -> Result<<A as DynCastExtAdvHelper<F, T>>::Target, <A as DynCastExtAdvHelper<F, T>>::Source>
where A: DynCastExtAdvHelper<F, T>, F: ?Sized, T: ?Sized,

Use this to cast from one trait object type to another. This method is more customizable than the dyn_cast method. Here you can also specify the “source” trait from which the cast is defined. This can for example allow using casts from a supertrait of the current trait object. Read more
Source§

fn dyn_cast_with_config<C>( self, ) -> Result<<A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Target, <A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Source>

Use this to cast from one trait object type to another. With this method the type parameter is a config type that uniquely specifies which cast should be preformed. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more