Struct tokio::sync::batch_semaphore::Semaphore

source ·
pub(crate) struct Semaphore {
    waiters: Mutex<Waitlist>,
    permits: AtomicUsize,
}
Expand description

An asynchronous counting semaphore which permits waiting on multiple permits at once.

Fields§

§waiters: Mutex<Waitlist>§permits: AtomicUsize

The current number of available permits in the semaphore.

Implementations§

source§

impl Semaphore

source

pub(crate) const MAX_PERMITS: usize = 2_305_843_009_213_693_951usize

The maximum number of permits which a semaphore can hold.

Note that this reserves three bits of flags in the permit counter, but we only actually use one of them. However, the previous semaphore implementation used three bits, so we will continue to reserve them to avoid a breaking change if additional flags need to be added in the future.

source

const CLOSED: usize = 1usize

source

const PERMIT_SHIFT: usize = 1usize

source

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

Creates a new semaphore with the initial number of permits

Maximum number of permits on 32-bit platforms is 1<<29.

source

pub(crate) const fn const_new(permits: usize) -> Self

Creates a new semaphore with the initial number of permits.

Maximum number of permits on 32-bit platforms is 1<<29.

source

pub(crate) fn new_closed() -> Self

Creates a new closed semaphore with 0 permits.

source

pub(crate) const fn const_new_closed() -> Self

Creates a new closed semaphore with 0 permits.

source

pub(crate) fn available_permits(&self) -> usize

Returns the current number of available permits.

source

pub(crate) fn release(&self, added: usize)

Adds added new permits to the semaphore.

The maximum number of permits is usize::MAX >> 3, and this function will panic if the limit is exceeded.

source

pub(crate) fn close(&self)

Closes the semaphore. This prevents the semaphore from issuing new permits and notifies all pending waiters.

source

pub(crate) fn is_closed(&self) -> bool

Returns true if the semaphore is closed.

source

pub(crate) fn try_acquire( &self, num_permits: usize, ) -> Result<(), TryAcquireError>

source

pub(crate) fn acquire(&self, num_permits: usize) -> Acquire<'_>

source

fn add_permits_locked(&self, rem: usize, waiters: MutexGuard<'_, Waitlist>)

Release rem permits to the semaphore’s wait list, starting from the end of the queue.

If rem exceeds the number of permits needed by the wait list, the remainder are assigned back to the semaphore.

source

pub(crate) fn forget_permits(&self, n: usize) -> usize

Decrease a semaphore’s permits by a maximum of n.

If there are insufficient permits and it’s not possible to reduce by n, return the number of permits that were actually reduced.

source

fn poll_acquire( &self, cx: &mut Context<'_>, num_permits: usize, node: Pin<&mut Waiter>, queued: bool, ) -> Poll<Result<(), AcquireError>>

Trait Implementations§

source§

impl Debug for Semaphore

source§

fn fmt(&self, fmt: &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>,

§

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>,

§

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.