Trait Semaphore

Source
pub trait Semaphore: Sized {
    // Required methods
    fn new(init: u32) -> Option<Self>;
    fn wait(&self);
    fn try_wait(&self) -> bool;
    fn wait_timeout(&self, timeout: Duration) -> bool;
    fn post(&self) -> bool;
    fn signal(&self);

    // Provided methods
    fn lock(&self) -> SemaphoreGuard<'_, Self> { ... }
    fn try_lock(&self) -> Option<SemaphoreGuard<'_, Self>> { ... }
}
Expand description

Describes Semaphore interface

This primitive provides access to single integer that can be decremented using signal and incremented using wait

Required Methods§

Source

fn new(init: u32) -> Option<Self>

Creates new instance, returning None on inability to do so.

init is initial value for the semaphore

Source

fn wait(&self)

Decrements self, returning immediately if it was signaled.

Otherwise awaits for signal

Source

fn try_wait(&self) -> bool

Attempts to decrement self, returning whether self was signaled or not.

Returns true if self was signaled

Returns false otherwise

Source

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

Attempts to decrement self within provided time, returning whether self was signaled or not.

Returns true if self was signaled within specified timeout

Returns false otherwise

Source

fn post(&self) -> bool

Increments self, returning whether another thread has been woken as result.

Source

fn signal(&self)

Increments self, waking any awaiting thread as result

Provided Methods§

Source

fn lock(&self) -> SemaphoreGuard<'_, Self>

Gets semaphore’s guard, which signal on drop.

Before guard is created, function will await for semaphore to get decremented.

Source

fn try_lock(&self) -> Option<SemaphoreGuard<'_, Self>>

Attempts to acquire semaphore’s guard, which signal on drop.

If semaphore cannot be decremented at the current moment, returns None

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§