Skip to main content

PrioritySemaphore

Struct PrioritySemaphore 

Source
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

Source

pub const MAX_PERMITS: usize = PERMIT_MASK

Largest supported initial permit count.

Source

pub const fn new(permits: usize) -> Self

Creates a semaphore with permits concurrent permits.

§Panics

Panics when permits is larger than PrioritySemaphore::MAX_PERMITS.

Source

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.

Source

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.

Source

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.

Source

pub fn available_permits(&self) -> usize

Returns the number of permits that can be acquired immediately.

Source

pub fn queued(&self) -> usize

Returns the number of futures currently waiting in the priority queue.

Source

pub fn is_closed(&self) -> bool

Returns true after PrioritySemaphore::close has been called.

Trait Implementations§

Source§

impl Debug for PrioritySemaphore

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.