pub struct Semaphore { /* private fields */ }Expand description
A semaphore maintains a set of permits. Permits are used to synchronize access to a shared resource. A semaphore differs from a mutex in that it can allow more than one concurrent caller to access the shared resource at a time.
Implementations§
Source§impl Semaphore
impl Semaphore
Sourcepub const fn new(permits: usize) -> Self
pub const fn new(permits: usize) -> Self
Creates a new instance of Semaphore with the given permits count.
This is typically used to create a static semaphore:
use hydra::Semaphore;
static RATE_LIMIT: Semaphore = Semaphore::new(100);Sourcepub async fn acquire(&self) -> SemaphorePermit<'_>
pub async fn acquire(&self) -> SemaphorePermit<'_>
Acquires one permit asynchronously waiting for one to become available.
Sourcepub async fn acquire_many(&self, count: u32) -> SemaphorePermit<'_>
pub async fn acquire_many(&self, count: u32) -> SemaphorePermit<'_>
Acquires many permits asynchronously waiting for them to become available.
Sourcepub fn try_acquire(&self) -> Result<SemaphorePermit<'_>, NoPermits>
pub fn try_acquire(&self) -> Result<SemaphorePermit<'_>, NoPermits>
Attempts to acquire a permit, returning an error if there are none available.
Auto Trait Implementations§
impl !Freeze for Semaphore
impl RefUnwindSafe for Semaphore
impl Send for Semaphore
impl Sync for Semaphore
impl Unpin 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