pub struct SemaphoreQueue<S: SemaphoreState<Params = Params, Permit = Permit> + ?Sized, C: IsCloseable, Params = <S as SemaphoreState>::Params, Permit = <S as SemaphoreState>::Permit> { /* private fields */ }
Expand description
A queue that manages the acquisition of permits from a SemaphoreState
, or queues tasks
if no permits are available.
Implementations§
Source§impl<S: SemaphoreState + ?Sized, C: IsCloseable> SemaphoreQueue<S, C>
impl<S: SemaphoreState + ?Sized, C: IsCloseable> SemaphoreQueue<S, C>
Sourcepub fn acquire<R: RawMutex>(
this: &Mutex<R, Self>,
params: S::Params,
order: FairOrder,
) -> Acquire<'_, S, C, R> ⓘ
pub fn acquire<R: RawMutex>( this: &Mutex<R, Self>, params: S::Params, order: FairOrder, ) -> Acquire<'_, S, C, R> ⓘ
Acquire a permit, or join the queue if not currently available.
- If the order is
FairOrder::Lifo
, then we enqueue at the front of the queue. - If the order is
FairOrder::Fifo
, then we enqueue at the back of the queue.
Sourcepub fn try_acquire(
&mut self,
params: S::Params,
fairness: Fairness,
) -> Result<S::Permit, TryAcquireError<S::Params, C>>
pub fn try_acquire( &mut self, params: S::Params, fairness: Fairness, ) -> Result<S::Permit, TryAcquireError<S::Params, C>>
Try acquire a permit without joining the queue.
- If the fairness is
Fairness::Unfair
, orFairness::Fair(FairOrder::Lifo)
, then we always try acquire a permit. - If the fairness is
Fairness::Fair(FairOrder::Fifo)
, then we only try acquire a permit if the queue is empty.
§Errors
If there are currently not enough permits available for the given request,
then TryAcquireError::NoPermits
is returned.
If this is a Fairness::Fair(FairOrder::Fifo)
semaphore queue,
and there are other tasks waiting for permits,
then TryAcquireError::NoPermits
is returned.
If this semaphore is_closed
, then TryAcquireError::Closed
is returned.
Source§impl<S: SemaphoreState, C: IsCloseable> SemaphoreQueue<S, C>
impl<S: SemaphoreState, C: IsCloseable> SemaphoreQueue<S, C>
Sourcepub fn new(state: S) -> Self
pub fn new(state: S) -> Self
Construct a new semaphore queue, with the given SemaphoreState
.
Source§impl<S: SemaphoreState + ?Sized, C: IsCloseable> SemaphoreQueue<S, C>
impl<S: SemaphoreState + ?Sized, C: IsCloseable> SemaphoreQueue<S, C>
Sourcepub fn with_state<T>(&mut self, f: impl FnOnce(&mut S) -> T) -> T
pub fn with_state<T>(&mut 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.