Skip to main content

Semaphore

Struct Semaphore 

Source
pub struct Semaphore<const MAX: u8> { /* private fields */ }
Expand description

An async counting semaphore.

static SEM: rivet::sync::Semaphore<1> = rivet::sync::Semaphore::new(0);

#[rivet::task(priority = 1)]
async fn waiter() {
    loop {
        SEM.acquire().await;
        // got the semaphore
    }
}

// ISR or another task:
fn signaler() {
    SEM.release();
}

Implementations§

Source§

impl<const MAX: u8> Semaphore<MAX>

Source

pub const fn new(initial: u8) -> Self

Create a new semaphore with the given initial count.

Source

pub fn try_acquire(&self) -> bool

Try to acquire without blocking. Returns true if acquired.

Source

pub fn acquire(&self) -> Acquire<'_, MAX>

Acquire the semaphore, yielding the task if it’s currently taken.

§Panics

Panics if polled outside of a task context (i.e. not from within the executor’s poll of a #[rivet::task]).

Source

pub fn release(&self)

Release the semaphore. If tasks are waiting, the highest-priority one is woken and handed the token directly. Safe to call from ISR context.

Trait Implementations§

Source§

impl<const MAX: u8> Sync for Semaphore<MAX>

Auto Trait Implementations§

§

impl<const MAX: u8> !Freeze for Semaphore<MAX>

§

impl<const MAX: u8> RefUnwindSafe for Semaphore<MAX>

§

impl<const MAX: u8> Send for Semaphore<MAX>

§

impl<const MAX: u8> Unpin for Semaphore<MAX>

§

impl<const MAX: u8> UnsafeUnpin for Semaphore<MAX>

§

impl<const MAX: u8> UnwindSafe for Semaphore<MAX>

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.