Struct tarantool::fiber::Cond

source ·
pub struct Cond { /* private fields */ }
Expand description

Conditional variable for cooperative multitasking (fibers).

A cond (short for “condition variable”) is a synchronization primitive that allow fibers to yield until some predicate is satisfied. Fiber conditions have two basic operations - wait() and signal(). cond.wait() suspends execution of fiber (i.e. yields) until cond.signal() is called.

Example:

use tarantool::fiber::Cond;
let cond = Cond::new();
cond.wait();

The job will hang because cond.wait() – will go to sleep until the condition variable changes.

// Call from another fiber:
cond.signal();

The waiting stopped, and the cond.wait() function returned true.

This example depended on the use of a global conditional variable with the arbitrary name cond. In real life, programmers would make sure to use different conditional variable names for different applications.

Unlike pthread_cond, Cond doesn’t require mutex/latch wrapping.

Implementations§

source§

impl Cond

  • call Cond::new() to create a named condition variable, which will be called cond for examples in this section.
  • call cond.wait() to make a fiber wait for a signal via a condition variable.
  • call cond.signal() to send a signal to wake up a single fiber that has executed cond.wait().
  • call cond.broadcast() to send a signal to all fibers that have executed cond.wait().
source

pub fn new() -> Self

Instantiate a new fiber cond object.

source

pub fn signal(&self)

Wake one fiber waiting for the cond. Does nothing if no one is waiting. Does not yield.

source

pub fn broadcast(&self)

Wake up all fibers waiting for the cond. Does not yield.

source

pub fn wait_timeout(&self, timeout: Duration) -> bool

Suspend the execution of the current fiber (i.e. yield) until Self::signal or Self::broadcast is called or a timeout is exceeded.

Like pthread_cond, Cond can issue spurious wake ups caused by explicit fiber::wakeup or fiber::cancel calls. Keep this in mind when designing your algorithms.

Returns:

source

pub fn wait_deadline(&self, deadline: Instant) -> bool

Suspend the execution of the current fiber (i.e. yield) until Self::signal or Self::broadcast is called or a deadline is reached.

Like pthread_cond, Cond can issue spurious wake ups caused by explicit fiber::wakeup or fiber::cancel calls. Keep this in mind when designing your algorithms.

This will call fiber::clock internally to compute the relative timeout.

Returns:

source

pub fn wait(&self) -> bool

Suspend the execution of the current fiber (i.e. yield) until Self::signal or Self::broadcast is called.

Like pthread_cond, Cond can issue spurious wake ups caused by explicit fiber::wakeup or fiber::cancel calls. Keep this in mind when designing your algorithms.

Returns:

  • true if cond was signalled or fiber was awoken by other means.
  • false if current fiber was cancelled (check fiber::is_cancelled).

Trait Implementations§

source§

impl Debug for Cond

source§

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

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

impl Default for Cond

source§

fn default() -> Self

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

impl Drop for Cond

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Cond

§

impl !Send for Cond

§

impl !Sync for Cond

§

impl Unpin for Cond

§

impl UnwindSafe for Cond

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> 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> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.