pub struct PrioritySemaphore { /* private fields */ }Expand description
A runtime-independent, priority-aware asynchronous semaphore.
Acquiring an immediately available permit is lock-free. Under contention, returned permits are reserved directly for the highest-priority waiter, so a newly arriving task cannot steal a wake-up.
Implementations§
Source§impl PrioritySemaphore
impl PrioritySemaphore
Sourcepub const MAX_PERMITS: usize = PERMIT_MASK
pub const MAX_PERMITS: usize = PERMIT_MASK
Largest supported initial permit count.
Sourcepub const fn new(permits: usize) -> Self
pub const fn new(permits: usize) -> Self
Creates a semaphore with permits concurrent permits.
§Panics
Panics when permits is larger than PrioritySemaphore::MAX_PERMITS.
Sourcepub fn acquire(self: &Arc<Self>, priority: Priority) -> AcquireFuture ⓘ
pub fn acquire(self: &Arc<Self>, priority: Priority) -> AcquireFuture ⓘ
Acquires one permit at priority.
The returned future is cancellation-safe. If it is cancelled after a permit has already been assigned, that permit is immediately passed to the next waiter or returned to the semaphore.
Sourcepub fn try_acquire(
self: &Arc<Self>,
_priority: Priority,
) -> Result<Permit, TryAcquireError>
pub fn try_acquire( self: &Arc<Self>, _priority: Priority, ) -> Result<Permit, TryAcquireError>
Attempts to acquire one immediately available permit.
This method never bypasses already queued waiters. priority is
accepted for API symmetry, but only affects queued acquisitions.
Sourcepub fn close(&self)
pub fn close(&self)
Closes the semaphore and wakes every queued waiter.
Closing is idempotent. Permits acquired before the close remain valid, while all subsequent acquisition attempts fail.
Sourcepub fn available_permits(&self) -> usize
pub fn available_permits(&self) -> usize
Returns the number of permits that can be acquired immediately.
Sourcepub fn queued(&self) -> usize
pub fn queued(&self) -> usize
Returns the number of futures currently waiting in the priority queue.
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Returns true after PrioritySemaphore::close has been called.