Skip to content

Commit

Permalink
Clamp permit count Semaphore::const_new
Browse files Browse the repository at this point in the history
  • Loading branch information
mental32 committed Sep 9, 2020
1 parent 3fc62b2 commit a800dc2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
14 changes: 11 additions & 3 deletions tokio/src/sync/batch_semaphore.rs
Expand Up @@ -126,9 +126,17 @@ impl Semaphore {
/// Creates a new semaphore with the initial number of permits
///
/// Maximum number of permits on 32-bit platforms is `1<<29`.
#[cfg(all(feature = "parking_lot", not(all(loom, test)),))]
pub(crate) const fn const_new(permits: usize) -> Self {
// FIXME: assertions and by extension panics are still being worked on: https://github.com/rust-lang/rust/issues/74925
///
/// If the specified number of permits exceeds the maximum permit amount
/// Then the value will get clamped to the maximum number of permits.
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
pub(crate) const fn const_new(mut permits: usize) -> Self {
if permits > Self::MAX_PERMITS {
// NOTE: assertions and by extension panics are still being worked on: https://github.com/rust-lang/rust/issues/74925
// currently we just clamp the permit count when it exceeds the max
permits = Self::MAX_PERMITS;
}

Self {
permits: AtomicUsize::new(permits << Self::PERMIT_SHIFT),
waiters: Mutex::const_new(Waitlist {
Expand Down
1 change: 0 additions & 1 deletion tokio/src/util/linked_list.rs
Expand Up @@ -72,7 +72,6 @@ unsafe impl<T: Sync> Sync for Pointers<T> {}

impl<L, T> LinkedList<L, T> {
/// Creates an empty linked list.
#[allow(dead_code)] // NOTE: This will get removed with: https://github.com/tokio-rs/tokio/pull/2790
pub(crate) const fn new() -> LinkedList<L, T> {
LinkedList {
head: None,
Expand Down

0 comments on commit a800dc2

Please sign in to comment.