From a800dc221a45cc07af936819ce4e4f7b07a4607c Mon Sep 17 00:00:00 2001 From: mental Date: Wed, 9 Sep 2020 09:00:04 +0100 Subject: [PATCH] Clamp permit count `Semaphore::const_new` --- tokio/src/sync/batch_semaphore.rs | 14 +++++++++++--- tokio/src/util/linked_list.rs | 1 - 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/tokio/src/sync/batch_semaphore.rs b/tokio/src/sync/batch_semaphore.rs index a291b6c0396..e23d81052ff 100644 --- a/tokio/src/sync/batch_semaphore.rs +++ b/tokio/src/sync/batch_semaphore.rs @@ -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 { diff --git a/tokio/src/util/linked_list.rs b/tokio/src/util/linked_list.rs index e6d6c5885c4..940d6af9200 100644 --- a/tokio/src/util/linked_list.rs +++ b/tokio/src/util/linked_list.rs @@ -72,7 +72,6 @@ unsafe impl Sync for Pointers {} impl LinkedList { /// 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 { LinkedList { head: None,