Trait embassy_sync::semaphore::Semaphore

source ·
pub trait Semaphore: Sized {
    type Error;

    // Required methods
    async fn acquire(
        &self,
        permits: usize,
    ) -> Result<SemaphoreReleaser<'_, Self>, Self::Error>;
    fn try_acquire(&self, permits: usize) -> Option<SemaphoreReleaser<'_, Self>>;
    async fn acquire_all(
        &self,
        min: usize,
    ) -> Result<SemaphoreReleaser<'_, Self>, Self::Error>;
    fn try_acquire_all(&self, min: usize) -> Option<SemaphoreReleaser<'_, Self>>;
    fn release(&self, permits: usize);
    fn set(&self, permits: usize);
}
Expand description

An asynchronous semaphore.

A semaphore tracks a number of permits, typically representing a pool of shared resources. Users can acquire permits to synchronize access to those resources. The semaphore does not contain the resources themselves, only the count of available permits.

Required Associated Types§

source

type Error

The error returned when the semaphore is unable to acquire the requested permits.

Required Methods§

source

async fn acquire( &self, permits: usize, ) -> Result<SemaphoreReleaser<'_, Self>, Self::Error>

Asynchronously acquire one or more permits from the semaphore.

source

fn try_acquire(&self, permits: usize) -> Option<SemaphoreReleaser<'_, Self>>

Try to immediately acquire one or more permits from the semaphore.

source

async fn acquire_all( &self, min: usize, ) -> Result<SemaphoreReleaser<'_, Self>, Self::Error>

Asynchronously acquire all permits controlled by the semaphore.

This method will wait until at least min permits are available, then acquire all available permits from the semaphore. Note that other tasks may have already acquired some permits which could be released back to the semaphore at any time. The number of permits actually acquired may be determined by calling SemaphoreReleaser::permits.

source

fn try_acquire_all(&self, min: usize) -> Option<SemaphoreReleaser<'_, Self>>

Try to immediately acquire all available permits from the semaphore, if at least min permits are available.

source

fn release(&self, permits: usize)

Release permits back to the semaphore, making them available to be acquired.

source

fn set(&self, permits: usize)

Reset the number of available permints in the semaphore to permits.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<M: RawMutex> Semaphore for GreedySemaphore<M>

§

type Error = Infallible

source§

impl<M: RawMutex, const N: usize> Semaphore for FairSemaphore<M, N>