diff options
author | mental <m3nta1@yahoo.com> | 2020-09-12 10:55:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-12 11:55:03 +0200 |
commit | 20ef28655354ae729a4af2098426a413e3f4d769 (patch) | |
tree | 1122ae239c47b3553f4b50741a756e36de34a707 /tokio/src/sync/batch_semaphore.rs | |
parent | 2bc9a4815259c8ff4daa5e24f128ec826970d17f (diff) |
sync: add const-constructors for some sync primitives (#2790)
Co-authored-by: Mikail Bagishov <bagishov.mikail@yandex.ru>
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
Co-authored-by: Alice Ryhl <alice@ryhl.io>
Diffstat (limited to 'tokio/src/sync/batch_semaphore.rs')
-rw-r--r-- | tokio/src/sync/batch_semaphore.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/tokio/src/sync/batch_semaphore.rs b/tokio/src/sync/batch_semaphore.rs index c48a911c..a1048ca3 100644 --- a/tokio/src/sync/batch_semaphore.rs +++ b/tokio/src/sync/batch_semaphore.rs @@ -123,6 +123,27 @@ impl Semaphore { } } + /// Creates a new semaphore with the initial number of permits + /// + /// Maximum number of permits on 32-bit platforms is `1<<29`. + /// + /// 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 { + queue: LinkedList::new(), + closed: false, + }), + } + } + /// Returns the current number of available permits pub(crate) fn available_permits(&self) -> usize { self.permits.load(Acquire) >> Self::PERMIT_SHIFT |