Skip to content

Commit

Permalink
Add Semaphore::add_permits()
Browse files Browse the repository at this point in the history
  • Loading branch information
Jules-Bertholet committed May 26, 2023
1 parent f738cfd commit 216fd1b
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ impl Semaphore {
listener: None,
}
}
}

impl Semaphore {
/// Attempts to get an owned permit for a concurrent operation.
///
/// If the permit could not be acquired at this time, then [`None`] is returned. Otherwise, an
Expand Down Expand Up @@ -152,6 +150,30 @@ impl Semaphore {
listener: None,
}
}

/// Adds `n` additional permits to the semaphore.
///
/// # Examples
///
/// ```
/// use async_lock::Semaphore;
///
/// # futures_lite::future::block_on(async {
/// let s = Semaphore::new(1);
///
/// let _guard = s.acquire().await;
/// assert!(s.try_acquire().is_none());
///
/// s.add_permits(2);
///
/// let _guard = s.acquire().await;
/// let _guard = s.acquire().await;
/// # });
/// ```
pub fn add_permits(&self, n: usize) {
self.count.fetch_add(n, Ordering::AcqRel);
self.event.notify(n);
}
}

/// The future returned by [`Semaphore::acquire`].
Expand Down

0 comments on commit 216fd1b

Please sign in to comment.