From 91445a5bb84f99d7430752d8022fc625aaa7439a Mon Sep 17 00:00:00 2001 From: mental Date: Mon, 24 Aug 2020 15:05:29 +0100 Subject: [PATCH 1/9] Add a bunch of const constructors --- tokio/src/lib.rs | 4 ++++ tokio/src/loom/std/atomic_usize.rs | 9 +++++++++ tokio/src/loom/std/parking_lot.rs | 9 +++++++++ tokio/src/sync/batch_semaphore.rs | 18 ++++++++++++++++++ tokio/src/sync/mutex.rs | 23 +++++++++++++++++++++++ tokio/src/util/linked_list.rs | 13 +++++++++++++ 6 files changed, 76 insertions(+) diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index e308f4f8136..2d115f3dc00 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -17,6 +17,10 @@ ))] #![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(docsrs, feature(doc_alias))] +#![cfg_attr(all( + feature = "parking_lot", + not(all(loom, test)), +), feature(const_fn))] //! A runtime for writing reliable, asynchronous, and slim applications. //! diff --git a/tokio/src/loom/std/atomic_usize.rs b/tokio/src/loom/std/atomic_usize.rs index 0fe998f1f9e..fffa3efcc94 100644 --- a/tokio/src/loom/std/atomic_usize.rs +++ b/tokio/src/loom/std/atomic_usize.rs @@ -16,6 +16,15 @@ impl AtomicUsize { AtomicUsize { inner } } + #[cfg(all( + feature = "parking_lot", + not(all(loom, test)), + ))] + pub(crate) const fn const_new(val: usize) -> AtomicUsize { + let inner = UnsafeCell::new(std::sync::atomic::AtomicUsize::new(val)); + AtomicUsize { inner } + } + /// Performs an unsynchronized load. /// /// # Safety diff --git a/tokio/src/loom/std/parking_lot.rs b/tokio/src/loom/std/parking_lot.rs index 25d94af44f5..566c7df9b9f 100644 --- a/tokio/src/loom/std/parking_lot.rs +++ b/tokio/src/loom/std/parking_lot.rs @@ -26,6 +26,15 @@ impl Mutex { Mutex(parking_lot::Mutex::new(t)) } + #[inline] + #[cfg(all( + feature = "parking_lot", + not(all(loom, test)), + ))] + pub(crate) const fn const_new(t: T) -> Mutex { + Mutex(parking_lot::const_mutex(t)) + } + #[inline] pub(crate) fn lock(&self) -> LockResult> { Ok(self.0.lock()) diff --git a/tokio/src/sync/batch_semaphore.rs b/tokio/src/sync/batch_semaphore.rs index aad6c6f1f8e..0888a9ade32 100644 --- a/tokio/src/sync/batch_semaphore.rs +++ b/tokio/src/sync/batch_semaphore.rs @@ -123,6 +123,24 @@ 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 + Self { + permits: AtomicUsize::const_new(permits << Self::PERMIT_SHIFT), + waiters: Mutex::const_new(Waitlist { + queue: LinkedList::const_new(), + closed: false, + }), + } + } + /// Returns the current number of available permits pub(crate) fn available_permits(&self) -> usize { self.permits.load(Acquire) >> Self::PERMIT_SHIFT diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index df348457fec..1d5cd996407 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -219,6 +219,29 @@ impl Mutex { } } + /// Creates a new lock in an unlocked state ready for use. + /// + /// # Examples + /// + /// ``` + /// use tokio::sync::Mutex; + /// + /// static lock: Mutex = Mutex::const_new(5); + /// ``` + #[cfg(all( + feature = "parking_lot", + not(all(loom, test)), + ))] + pub const fn const_new(t: T) -> Self + where + T: Sized + { + Self { + c: UnsafeCell::new(t), + s: semaphore::Semaphore::const_new(1), + } + } + /// Locks this mutex, causing the current task /// to yield until the lock has been acquired. /// When the lock has been acquired, function returns a [`MutexGuard`]. diff --git a/tokio/src/util/linked_list.rs b/tokio/src/util/linked_list.rs index aa3ce771887..3df2f30b07e 100644 --- a/tokio/src/util/linked_list.rs +++ b/tokio/src/util/linked_list.rs @@ -75,6 +75,19 @@ impl LinkedList { } } + /// Creates an empty linked list + #[cfg(all( + feature = "parking_lot", + not(all(loom, test)), + ))] + pub(crate) const fn const_new() -> LinkedList { + LinkedList { + head: None, + tail: None, + } + } + + /// Adds an element first in the list. pub(crate) fn push_front(&mut self, val: T::Handle) { // The value should not be dropped, it is being inserted into the list From 633799ee20569115fac4684482cc15b4e794b537 Mon Sep 17 00:00:00 2001 From: mental Date: Mon, 24 Aug 2020 15:28:48 +0100 Subject: [PATCH 2/9] rustfmt --- tokio/src/lib.rs | 5 +---- tokio/src/loom/std/atomic_usize.rs | 5 +---- tokio/src/loom/std/parking_lot.rs | 5 +---- tokio/src/sync/batch_semaphore.rs | 5 +---- tokio/src/sync/mutex.rs | 7 ++----- tokio/src/util/linked_list.rs | 6 +----- 6 files changed, 7 insertions(+), 26 deletions(-) diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index 2d115f3dc00..a1514c6ebe6 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -17,10 +17,7 @@ ))] #![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(docsrs, feature(doc_alias))] -#![cfg_attr(all( - feature = "parking_lot", - not(all(loom, test)), -), feature(const_fn))] +#![cfg_attr(all(feature = "parking_lot", not(all(loom, test)),), feature(const_fn))] //! A runtime for writing reliable, asynchronous, and slim applications. //! diff --git a/tokio/src/loom/std/atomic_usize.rs b/tokio/src/loom/std/atomic_usize.rs index fffa3efcc94..8e7c6a4e98f 100644 --- a/tokio/src/loom/std/atomic_usize.rs +++ b/tokio/src/loom/std/atomic_usize.rs @@ -16,10 +16,7 @@ impl AtomicUsize { AtomicUsize { inner } } - #[cfg(all( - feature = "parking_lot", - not(all(loom, test)), - ))] + #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] pub(crate) const fn const_new(val: usize) -> AtomicUsize { let inner = UnsafeCell::new(std::sync::atomic::AtomicUsize::new(val)); AtomicUsize { inner } diff --git a/tokio/src/loom/std/parking_lot.rs b/tokio/src/loom/std/parking_lot.rs index 566c7df9b9f..4138b60db3d 100644 --- a/tokio/src/loom/std/parking_lot.rs +++ b/tokio/src/loom/std/parking_lot.rs @@ -27,10 +27,7 @@ impl Mutex { } #[inline] - #[cfg(all( - feature = "parking_lot", - not(all(loom, test)), - ))] + #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] pub(crate) const fn const_new(t: T) -> Mutex { Mutex(parking_lot::const_mutex(t)) } diff --git a/tokio/src/sync/batch_semaphore.rs b/tokio/src/sync/batch_semaphore.rs index 0888a9ade32..131d8dfa4e4 100644 --- a/tokio/src/sync/batch_semaphore.rs +++ b/tokio/src/sync/batch_semaphore.rs @@ -126,10 +126,7 @@ 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)), - ))] + #[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 Self { diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index 1d5cd996407..ca1ef4818c7 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -228,13 +228,10 @@ impl Mutex { /// /// static lock: Mutex = Mutex::const_new(5); /// ``` - #[cfg(all( - feature = "parking_lot", - not(all(loom, test)), - ))] + #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] pub const fn const_new(t: T) -> Self where - T: Sized + T: Sized, { Self { c: UnsafeCell::new(t), diff --git a/tokio/src/util/linked_list.rs b/tokio/src/util/linked_list.rs index 3df2f30b07e..4d1b28bab82 100644 --- a/tokio/src/util/linked_list.rs +++ b/tokio/src/util/linked_list.rs @@ -76,10 +76,7 @@ impl LinkedList { } /// Creates an empty linked list - #[cfg(all( - feature = "parking_lot", - not(all(loom, test)), - ))] + #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] pub(crate) const fn const_new() -> LinkedList { LinkedList { head: None, @@ -87,7 +84,6 @@ impl LinkedList { } } - /// Adds an element first in the list. pub(crate) fn push_front(&mut self, val: T::Handle) { // The value should not be dropped, it is being inserted into the list From e1092882e313c33d8609ca8769505c9ab255d502 Mon Sep 17 00:00:00 2001 From: mental Date: Mon, 24 Aug 2020 15:35:18 +0100 Subject: [PATCH 3/9] FIx tokio/src/sync/mutex.rs doctest Co-authored-by: Mikail Bagishov --- tokio/src/sync/mutex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index ca1ef4818c7..b354d31e56d 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -226,7 +226,7 @@ impl Mutex { /// ``` /// use tokio::sync::Mutex; /// - /// static lock: Mutex = Mutex::const_new(5); + /// static lock: Mutex = Mutex::const_new(5); /// ``` #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] pub const fn const_new(t: T) -> Self From 3d53e15328c77031af8c30c6abd1eae7e772e64a Mon Sep 17 00:00:00 2001 From: mental Date: Mon, 24 Aug 2020 15:44:03 +0100 Subject: [PATCH 4/9] feature gate const-constructors to nightly they require feature(const_fn) which is not available on stable. --- tokio/src/lib.rs | 5 ++++- tokio/src/loom/std/atomic_usize.rs | 2 +- tokio/src/loom/std/parking_lot.rs | 2 +- tokio/src/sync/batch_semaphore.rs | 2 +- tokio/src/sync/mutex.rs | 2 +- tokio/src/util/linked_list.rs | 2 +- 6 files changed, 9 insertions(+), 6 deletions(-) diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index a1514c6ebe6..dd0e0f12364 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -17,7 +17,10 @@ ))] #![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(docsrs, feature(doc_alias))] -#![cfg_attr(all(feature = "parking_lot", not(all(loom, test)),), feature(const_fn))] +#![cfg_attr( + all(feature = "nightly", feature = "parking_lot", not(all(loom, test))), + feature(const_fn) +)] //! A runtime for writing reliable, asynchronous, and slim applications. //! diff --git a/tokio/src/loom/std/atomic_usize.rs b/tokio/src/loom/std/atomic_usize.rs index 8e7c6a4e98f..2057890fd6f 100644 --- a/tokio/src/loom/std/atomic_usize.rs +++ b/tokio/src/loom/std/atomic_usize.rs @@ -16,7 +16,7 @@ impl AtomicUsize { AtomicUsize { inner } } - #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] + #[cfg(all(feature = "nightly", feature = "parking_lot", not(all(loom, test)),))] pub(crate) const fn const_new(val: usize) -> AtomicUsize { let inner = UnsafeCell::new(std::sync::atomic::AtomicUsize::new(val)); AtomicUsize { inner } diff --git a/tokio/src/loom/std/parking_lot.rs b/tokio/src/loom/std/parking_lot.rs index 4138b60db3d..e37cae34f47 100644 --- a/tokio/src/loom/std/parking_lot.rs +++ b/tokio/src/loom/std/parking_lot.rs @@ -27,7 +27,7 @@ impl Mutex { } #[inline] - #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] + #[cfg(all(feature = "nightly", feature = "parking_lot", not(all(loom, test)),))] pub(crate) const fn const_new(t: T) -> Mutex { Mutex(parking_lot::const_mutex(t)) } diff --git a/tokio/src/sync/batch_semaphore.rs b/tokio/src/sync/batch_semaphore.rs index 131d8dfa4e4..3cd89cfe5eb 100644 --- a/tokio/src/sync/batch_semaphore.rs +++ b/tokio/src/sync/batch_semaphore.rs @@ -126,7 +126,7 @@ 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)),))] + #[cfg(all(feature = "nightly", 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 Self { diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index b354d31e56d..c965656ad91 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -228,7 +228,7 @@ impl Mutex { /// /// static lock: Mutex = Mutex::const_new(5); /// ``` - #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] + #[cfg(all(feature = "nightly", feature = "parking_lot", not(all(loom, test)),))] pub const fn const_new(t: T) -> Self where T: Sized, diff --git a/tokio/src/util/linked_list.rs b/tokio/src/util/linked_list.rs index 4d1b28bab82..c0d2b046806 100644 --- a/tokio/src/util/linked_list.rs +++ b/tokio/src/util/linked_list.rs @@ -76,7 +76,7 @@ impl LinkedList { } /// Creates an empty linked list - #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] + #[cfg(all(feature = "nightly", feature = "parking_lot", not(all(loom, test)),))] pub(crate) const fn const_new() -> LinkedList { LinkedList { head: None, From 38c9dd15b37e143f63ea950434c9569494fdeeb2 Mon Sep 17 00:00:00 2001 From: mental Date: Mon, 24 Aug 2020 18:35:50 +0100 Subject: [PATCH 5/9] Add doc_cfg attributres to const-constructors Co-authored-by: Eliza Weisman --- tokio/src/loom/std/parking_lot.rs | 1 + tokio/src/sync/mutex.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/tokio/src/loom/std/parking_lot.rs b/tokio/src/loom/std/parking_lot.rs index e37cae34f47..f5f4d2bba6b 100644 --- a/tokio/src/loom/std/parking_lot.rs +++ b/tokio/src/loom/std/parking_lot.rs @@ -28,6 +28,7 @@ impl Mutex { #[inline] #[cfg(all(feature = "nightly", feature = "parking_lot", not(all(loom, test)),))] + #[cfg_attr(docsrs, doc(cfg(all(feature = "parking_lot", feature = "nightly"))))] pub(crate) const fn const_new(t: T) -> Mutex { Mutex(parking_lot::const_mutex(t)) } diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index c965656ad91..85c50c39334 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -229,6 +229,7 @@ impl Mutex { /// static lock: Mutex = Mutex::const_new(5); /// ``` #[cfg(all(feature = "nightly", feature = "parking_lot", not(all(loom, test)),))] + #[cfg_attr(docsrs, doc(cfg(all(feature = "parking_lot", feature = "nightly"))))] pub const fn const_new(t: T) -> Self where T: Sized, From 39b8a30ff800db98a39c06edadc79250920d9cd4 Mon Sep 17 00:00:00 2001 From: mental Date: Thu, 3 Sep 2020 08:42:05 +0100 Subject: [PATCH 6/9] Remove nightly feature requirement --- tokio/src/lib.rs | 4 ---- tokio/src/loom/std/atomic_usize.rs | 2 +- tokio/src/loom/std/parking_lot.rs | 4 ++-- tokio/src/sync/batch_semaphore.rs | 4 ++-- tokio/src/sync/mutex.rs | 6 +++--- 5 files changed, 8 insertions(+), 12 deletions(-) diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index ea52ff0e998..7bf9f7435c1 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -17,10 +17,6 @@ ))] #![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(docsrs, feature(doc_alias))] -#![cfg_attr( - all(feature = "nightly", feature = "parking_lot", not(all(loom, test))), - feature(const_fn) -)] //! A runtime for writing reliable, asynchronous, and slim applications. //! diff --git a/tokio/src/loom/std/atomic_usize.rs b/tokio/src/loom/std/atomic_usize.rs index 2057890fd6f..8e7c6a4e98f 100644 --- a/tokio/src/loom/std/atomic_usize.rs +++ b/tokio/src/loom/std/atomic_usize.rs @@ -16,7 +16,7 @@ impl AtomicUsize { AtomicUsize { inner } } - #[cfg(all(feature = "nightly", feature = "parking_lot", not(all(loom, test)),))] + #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] pub(crate) const fn const_new(val: usize) -> AtomicUsize { let inner = UnsafeCell::new(std::sync::atomic::AtomicUsize::new(val)); AtomicUsize { inner } diff --git a/tokio/src/loom/std/parking_lot.rs b/tokio/src/loom/std/parking_lot.rs index f5f4d2bba6b..41c47ddd31e 100644 --- a/tokio/src/loom/std/parking_lot.rs +++ b/tokio/src/loom/std/parking_lot.rs @@ -27,8 +27,8 @@ impl Mutex { } #[inline] - #[cfg(all(feature = "nightly", feature = "parking_lot", not(all(loom, test)),))] - #[cfg_attr(docsrs, doc(cfg(all(feature = "parking_lot", feature = "nightly"))))] + #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] + #[cfg_attr(docsrs, doc(cfg(all(feature = "parking_lot",))))] pub(crate) const fn const_new(t: T) -> Mutex { Mutex(parking_lot::const_mutex(t)) } diff --git a/tokio/src/sync/batch_semaphore.rs b/tokio/src/sync/batch_semaphore.rs index 97475ec6653..4288e421e6e 100644 --- a/tokio/src/sync/batch_semaphore.rs +++ b/tokio/src/sync/batch_semaphore.rs @@ -126,13 +126,13 @@ 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 = "nightly", feature = "parking_lot", not(all(loom, test)),))] + #[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 Self { permits: AtomicUsize::const_new(permits << Self::PERMIT_SHIFT), waiters: Mutex::const_new(Waitlist { - queue: LinkedList::const_new(), + queue: LinkedList::new(), closed: false, }), } diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index 85c50c39334..594163f714c 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -226,10 +226,10 @@ impl Mutex { /// ``` /// use tokio::sync::Mutex; /// - /// static lock: Mutex = Mutex::const_new(5); + /// static LOCK: Mutex = Mutex::const_new(5); /// ``` - #[cfg(all(feature = "nightly", feature = "parking_lot", not(all(loom, test)),))] - #[cfg_attr(docsrs, doc(cfg(all(feature = "parking_lot", feature = "nightly"))))] + #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] + #[cfg_attr(docsrs, doc(cfg(all(feature = "parking_lot",))))] pub const fn const_new(t: T) -> Self where T: Sized, From a1fa12ae7b6b47fc668a390cf9e00d0d0792938f Mon Sep 17 00:00:00 2001 From: mental Date: Mon, 7 Sep 2020 14:41:21 +0100 Subject: [PATCH 7/9] Inline `AtomicUsize::const_new` to just `AtomicUsize::new` --- tokio/src/loom/std/atomic_usize.rs | 8 +------- tokio/src/sync/batch_semaphore.rs | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/tokio/src/loom/std/atomic_usize.rs b/tokio/src/loom/std/atomic_usize.rs index 8e7c6a4e98f..0d5f36e4310 100644 --- a/tokio/src/loom/std/atomic_usize.rs +++ b/tokio/src/loom/std/atomic_usize.rs @@ -11,13 +11,7 @@ unsafe impl Send for AtomicUsize {} unsafe impl Sync for AtomicUsize {} impl AtomicUsize { - pub(crate) fn new(val: usize) -> AtomicUsize { - let inner = UnsafeCell::new(std::sync::atomic::AtomicUsize::new(val)); - AtomicUsize { inner } - } - - #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] - pub(crate) const fn const_new(val: usize) -> AtomicUsize { + pub(crate) const fn new(val: usize) -> AtomicUsize { let inner = UnsafeCell::new(std::sync::atomic::AtomicUsize::new(val)); AtomicUsize { inner } } diff --git a/tokio/src/sync/batch_semaphore.rs b/tokio/src/sync/batch_semaphore.rs index 4288e421e6e..a291b6c0396 100644 --- a/tokio/src/sync/batch_semaphore.rs +++ b/tokio/src/sync/batch_semaphore.rs @@ -130,7 +130,7 @@ impl Semaphore { 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 Self { - permits: AtomicUsize::const_new(permits << Self::PERMIT_SHIFT), + permits: AtomicUsize::new(permits << Self::PERMIT_SHIFT), waiters: Mutex::const_new(Waitlist { queue: LinkedList::new(), closed: false, From 92dc9c300f41cfd6bac2d353825a8d3cd9130c2f Mon Sep 17 00:00:00 2001 From: mental Date: Wed, 9 Sep 2020 09:00:04 +0100 Subject: [PATCH 8/9] Clamp permit count `Semaphore::const_new` --- tokio/src/sync/batch_semaphore.rs | 12 +++++++++--- tokio/src/util/linked_list.rs | 1 - 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tokio/src/sync/batch_semaphore.rs b/tokio/src/sync/batch_semaphore.rs index a291b6c0396..a1048ca3734 100644 --- a/tokio/src/sync/batch_semaphore.rs +++ b/tokio/src/sync/batch_semaphore.rs @@ -126,9 +126,15 @@ 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 { + // 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 = 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, From eeb56082aae6e2ecb2101a49d5b201d3911515ac Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Sat, 12 Sep 2020 10:44:34 +0200 Subject: [PATCH 9/9] Simplify cfg_attr on const_new --- tokio/src/sync/mutex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index 594163f714c..71f0136f005 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -229,7 +229,7 @@ impl Mutex { /// static LOCK: Mutex = Mutex::const_new(5); /// ``` #[cfg(all(feature = "parking_lot", not(all(loom, test)),))] - #[cfg_attr(docsrs, doc(cfg(all(feature = "parking_lot",))))] + #[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))] pub const fn const_new(t: T) -> Self where T: Sized,