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.
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.
Sourcepub fn is_poisoned(&self) -> bool
pub fn is_poisoned(&self) -> bool
Check if the queue has been poisoned.
A queue becomes poisoned if a panic unwinds out of SemaphoreState::acquire
or a with_state closure, which may have left the state
half-updated. Poisoning persists until clear_poison.
A poisoned queue stops granting permits; new acquire attempts surface the
poison rather than build on a corrupt state.
Sourcepub fn clear_poison(&mut self)
pub fn clear_poison(&mut self)
Clear the poison flag, letting the queue grant permits again.
The caller is responsible for ensuring the state is consistent first (e.g.
inspect/repair it with with_state); like
[std::sync::Mutex::clear_poison], this does not itself touch the state.
A blocking acquire whose acquire impl panicked lost its params and can
never complete; it is skipped until its future is dropped.