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
impl Semaphore
sourcepub(crate) const MAX_PERMITS: usize = 2_305_843_009_213_693_951usize
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.
const CLOSED: usize = 1usize
const PERMIT_SHIFT: usize = 1usize
sourcepub(crate) fn new(permits: usize) -> Self
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
.
sourcepub(crate) const fn const_new(permits: usize) -> Self
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
.
sourcepub(crate) fn new_closed() -> Self
pub(crate) fn new_closed() -> Self
Creates a new closed semaphore with 0 permits.
sourcepub(crate) const fn const_new_closed() -> Self
pub(crate) const fn const_new_closed() -> Self
Creates a new closed semaphore with 0 permits.
sourcepub(crate) fn available_permits(&self) -> usize
pub(crate) fn available_permits(&self) -> usize
Returns the current number of available permits.
sourcepub(crate) fn release(&self, added: usize)
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.
sourcepub(crate) fn close(&self)
pub(crate) fn close(&self)
Closes the semaphore. This prevents the semaphore from issuing new permits and notifies all pending waiters.
pub(crate) fn try_acquire( &self, num_permits: usize, ) -> Result<(), TryAcquireError>
pub(crate) fn acquire(&self, num_permits: usize) -> Acquire<'_> ⓘ
sourcefn add_permits_locked(&self, rem: usize, waiters: MutexGuard<'_, Waitlist>)
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.
sourcepub(crate) fn forget_permits(&self, n: usize) -> usize
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.