Semaphore

Struct Semaphore 

Source
#[repr(C, align(16))]
pub struct Semaphore { /* private fields */ }
Expand description

Simple counting blocking or non-blocking lock

Implementations§

Source§

impl Semaphore

Source

pub const fn new(initial: u32) -> Semaphore

Instantiate a new semaphore with a given initial value

§Example
    let mut sema = Semaphore::new(5); // semaphore could be used/aquired 5 times
Source

pub fn up(&self)

increase the inner count of a semaphore allowing it to be used as many times as the inner counters value

§Example
    let mut sema = Semaphore::new(0);
    sema.up(); // the counter of the semaphore will be increased
Source

pub fn down(&self)

decrease the inner count of a semaphore. This blocks the current core if the current count is 0 and could not beeing decreased. For an unblocking operation use Semaphore::try_down

§Example
    let sema = Semaphore::new(0);
    sema.down();
    // if we reache this line, we have used the semaphore and decreased the counter by 1
Source

pub fn try_down(&self) -> Result<(), ()>

try to decrease a semaphore for usage. Returns [value@Ok] if the semaphore could be used.

§Example
    let sema = Semaphore::new(0);
    if sema.try_down().is_ok() {
        // do something... the counter of the semaphore has been decreased by 1
    }

Trait Implementations§

Source§

impl Debug for Semaphore

Source§

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

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

impl Default for Semaphore

Source§

fn default() -> Self

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

impl Send for Semaphore

Source§

impl Sync for Semaphore

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