pub struct Semaphore { /* private fields */ }Expand description
A counting semaphore: .acquire().await waits (without blocking the
OS thread) until a permit is available, then holds it until the
returned SemaphorePermit is dropped.
Implementations§
Source§impl Semaphore
impl Semaphore
Sourcepub const fn new(permits: usize) -> Self
pub const fn new(permits: usize) -> Self
Create a semaphore with permits available immediately.
Sourcepub fn available_permits(&self) -> usize
pub fn available_permits(&self) -> usize
Current number of permits available for immediate acquisition.
Sourcepub fn add_permits(&self, n: usize)
pub fn add_permits(&self, n: usize)
Add n permits, waking waiters as needed.
Sourcepub async fn acquire(&self) -> SemaphorePermit<'_>
pub async fn acquire(&self) -> SemaphorePermit<'_>
Acquire one permit, waiting if none are currently available.
Sourcepub fn try_acquire(&self) -> Result<SemaphorePermit<'_>, TryAcquireError>
pub fn try_acquire(&self) -> Result<SemaphorePermit<'_>, TryAcquireError>
Acquire one permit if immediately available, without waiting.
§Errors
Returns TryAcquireError::NoPermits if none are currently free.
Auto Trait Implementations§
impl !Freeze for Semaphore
impl !RefUnwindSafe for Semaphore
impl Send for Semaphore
impl Sync for Semaphore
impl Unpin for Semaphore
impl UnsafeUnpin for Semaphore
impl UnwindSafe for Semaphore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more