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§
Sourcefn new(init: u32) -> Option<Self>
fn new(init: u32) -> Option<Self>
Creates new instance, returning None on inability to do so.
init
is initial value for the semaphore
Sourcefn wait(&self)
fn wait(&self)
Decrements self, returning immediately if it was signaled.
Otherwise awaits for signal
Sourcefn try_wait(&self) -> bool
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
Sourcefn wait_timeout(&self, timeout: Duration) -> bool
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
Provided Methods§
Sourcefn lock(&self) -> SemaphoreGuard<'_, Self>
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.
Sourcefn try_lock(&self) -> Option<SemaphoreGuard<'_, Self>>
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.