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:
S: TheSemaphoreStatevalue that this semaphore is backed by.C: A type-safe configuration value for whetherSemaphore::closecan be called, and whetherSemaphore::acquirecan fail.
Implementations§
Source§impl<S> Semaphore<S, Uncloseable>where
S: SemaphoreState + ?Sized,
impl<S> Semaphore<S, Uncloseable>where
S: SemaphoreState + ?Sized,
Sourcepub async fn must_acquire(
&self,
params: S::Params,
) -> Permit<'_, S, Uncloseable>
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>
impl<S, C> Semaphore<S, C>
Sourcepub async fn acquire(
&self,
params: S::Params,
) -> Result<Permit<'_, S, C>, C::AcquireError<S::Params>>
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.
Sourcepub fn try_acquire(
&self,
params: S::Params,
) -> Result<Permit<'_, S, C>, TryAcquireError<S::Params, C>>
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.
Sourcepub fn try_acquire_unfair(
&self,
params: S::Params,
) -> Result<Permit<'_, S, C>, TryAcquireError<S::Params, C>>
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>
impl<S, C> Semaphore<S, C>
Sourcepub fn with_state<T>(&self, f: impl FnOnce(&mut S) -> T) -> T
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.