pub struct Semaphore { /* private fields */ }Expand description
Counted async semaphore.
Implementations§
Source§impl Semaphore
impl Semaphore
Sourcepub const fn new(permits: usize) -> Self
pub const fn new(permits: usize) -> Self
Build a semaphore with permits initial permits.
Under --cfg loom, async_lock::Semaphore::new and
event_listener::Event::new are deliberately non-const (loom’s
model needs a runtime execution context). Only the const
qualifier is affected; the constructed Semaphore is identical
either way. Semaphore isn’t part of the loom-tested
Notify/watch protocol, but the crate is built as one unit under
--cfg loom, so this split just keeps it compiling.
Sourcepub async fn acquire(&self) -> Result<SemaphorePermit<'_>, AcquireError>
pub async fn acquire(&self) -> Result<SemaphorePermit<'_>, AcquireError>
Acquire one permit, awaiting if necessary. Returns
Err(AcquireError) if the semaphore is (or becomes) closed
while waiting.
Sourcepub fn try_acquire(&self) -> Result<SemaphorePermit<'_>, TryAcquireError>
pub fn try_acquire(&self) -> Result<SemaphorePermit<'_>, TryAcquireError>
Try to acquire one permit without waiting.
Sourcepub fn add_permits(&self, n: usize)
pub fn add_permits(&self, n: usize)
Inject n additional permits into the pool.
Sourcepub fn available_permits(&self) -> usize
pub fn available_permits(&self) -> usize
Current available permit count. Snapshot only — may change concurrently before the caller observes the return value.