embassy-sync

Crates

git

Versions

default

Flavors

embassy_sync::semaphore

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

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§