Semaphore

Struct Semaphore 

Source
pub struct Semaphore<S, C = Uncloseable>{ /* private fields */ }
Expand description

Generic semaphore performing asynchronous permit acquisition.

See the top level docs for information about semaphores.

See Builder for methods to construct a Semaphore.

The generics on this semaphore are as follows:

Implementations§

Source§

impl<S> Semaphore<S, Uncloseable>
where S: SemaphoreState + ?Sized,

Source

pub async fn must_acquire( &self, params: S::Params, ) -> Permit<'_, S, Uncloseable>

Acquire a new permit fairly with the given parameters.

If a permit is not immediately available, this task will join a queue.

Source§

impl<S, C> Semaphore<S, C>

Source

pub async fn acquire( &self, params: S::Params, ) -> Result<Permit<'_, S, C>, C::AcquireError<S::Params>>

Acquire a new permit fairly with the given parameters.

If a permit is not immediately available, this task will join a queue.

§Errors

If this semaphore is_closed, then an AcquireError is returned.

Source

pub fn try_acquire( &self, params: S::Params, ) -> Result<Permit<'_, S, C>, TryAcquireError<S::Params, C>>

Acquire a new permit fairly with the given parameters.

If this is a LIFO semaphore, and there are other tasks waiting for permits, this will still try to acquire the permit - as this task would effectively be the last in the queue.

§Errors

If there are currently not enough permits available for the given request, then TryAcquireError::NoPermits is returned.

If this is a FIFO semaphore, and there are other tasks waiting for permits, then TryAcquireError::NoPermits is returned.

If this semaphore is_closed, then TryAcquireError::Closed is returned.

Source

pub fn try_acquire_unfair( &self, params: S::Params, ) -> Result<Permit<'_, S, C>, TryAcquireError<S::Params, C>>

Acquire a new permit, potentially unfairly, with the given parameters.

If this is a FIFO semaphore, and there are other tasks waiting for permits, this will still try to acquire the permit.

§Errors

If there are currently not enough permits available for the given request, then TryAcquireError::NoPermits is returned.

If this semaphore is_closed, then TryAcquireError::Closed is returned.

Source§

impl<S, C> Semaphore<S, C>

Source

pub fn with_state<T>(&self, f: impl FnOnce(&mut S) -> T) -> T

Access the state with mutable access.

This gives direct access to the state, be careful not to break any of your own state invariants. You can use this to peek at the current state, or to modify it, eg to add or remove permits from the semaphore.

Source

pub fn is_closed(&self) -> bool

Check if the semaphore is closed

Source§

impl<S> Semaphore<S, Closeable>
where S: SemaphoreState + ?Sized,

Source

pub fn close(&self)

Close the semaphore.

All tasks currently waiting to acquire a token will immediately stop. No new acquire attempts will succeed.

Trait Implementations§

Source§

impl<S: SemaphoreState + Debug> Debug for Semaphore<S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S, C = Uncloseable> !Freeze for Semaphore<S, C>

§

impl<S, C = Uncloseable> !RefUnwindSafe for Semaphore<S, C>

§

impl<S, C> Send for Semaphore<S, C>
where S: Send + ?Sized, <C as Sealed>::Closed<()>: Send, <S as SemaphoreState>::Permit: Send, <C as Sealed>::Closed<Option<<S as SemaphoreState>::Params>>: Send, <S as SemaphoreState>::Params: Send,

§

impl<S, C> Sync for Semaphore<S, C>
where S: Send + ?Sized, <C as Sealed>::Closed<()>: Send, <S as SemaphoreState>::Permit: Send, <C as Sealed>::Closed<Option<<S as SemaphoreState>::Params>>: Send, <S as SemaphoreState>::Params: Send,

§

impl<S, C> Unpin for Semaphore<S, C>
where S: Unpin + ?Sized, <C as Sealed>::Closed<()>: Unpin,

§

impl<S, C = Uncloseable> !UnwindSafe for Semaphore<S, C>

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.