summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrank Steffahn <fdsteffahn@gmail.com>2020-09-12 22:58:58 +0200
committerGitHub <noreply@github.com>2020-09-12 22:58:58 +0200
commit8d2e3bc575f51815ae7319f1e43fe6c7d664e6e4 (patch)
tree61fa8e1da832757b996abe7b03be85f156c66d6b
parent20ef28655354ae729a4af2098426a413e3f4d769 (diff)
sync: add const constructors to RwLock, Notify, and Semaphore (#2833)
* Add const constructors to `RwLock`, `Notify`, and `Semaphore`. Referring to the types in `tokio::sync`. Also add `const` to `new` for the remaining atomic integers in `src/loom` and `UnsafeCell`. Builds upon previous work in #2790 Closes #2756
-rw-r--r--tokio/src/loom/std/atomic_u16.rs2
-rw-r--r--tokio/src/loom/std/atomic_u32.rs2
-rw-r--r--tokio/src/loom/std/atomic_u8.rs2
-rw-r--r--tokio/src/loom/std/unsafe_cell.rs2
-rw-r--r--tokio/src/sync/notify.rs18
-rw-r--r--tokio/src/sync/rwlock.rs21
-rw-r--r--tokio/src/sync/semaphore.rs9
7 files changed, 52 insertions, 4 deletions
diff --git a/tokio/src/loom/std/atomic_u16.rs b/tokio/src/loom/std/atomic_u16.rs
index 70390972..c1c53120 100644
--- a/tokio/src/loom/std/atomic_u16.rs
+++ b/tokio/src/loom/std/atomic_u16.rs
@@ -11,7 +11,7 @@ unsafe impl Send for AtomicU16 {}
unsafe impl Sync for AtomicU16 {}
impl AtomicU16 {
- pub(crate) fn new(val: u16) -> AtomicU16 {
+ pub(crate) const fn new(val: u16) -> AtomicU16 {
let inner = UnsafeCell::new(std::sync::atomic::AtomicU16::new(val));
AtomicU16 { inner }
}
diff --git a/tokio/src/loom/std/atomic_u32.rs b/tokio/src/loom/std/atomic_u32.rs
index 6f786c51..61f95fb3 100644
--- a/tokio/src/loom/std/atomic_u32.rs
+++ b/tokio/src/loom/std/atomic_u32.rs
@@ -11,7 +11,7 @@ unsafe impl Send for AtomicU32 {}
unsafe impl Sync for AtomicU32 {}
impl AtomicU32 {
- pub(crate) fn new(val: u32) -> AtomicU32 {
+ pub(crate) const fn new(val: u32) -> AtomicU32 {
let inner = UnsafeCell::new(std::sync::atomic::AtomicU32::new(val));
AtomicU32 { inner }
}
diff --git a/tokio/src/loom/std/atomic_u8.rs b/tokio/src/loom/std/atomic_u8.rs
index 4fcd0df3..408aea33 100644
--- a/tokio/src/loom/std/atomic_u8.rs
+++ b/tokio/src/loom/std/atomic_u8.rs
@@ -11,7 +11,7 @@ unsafe impl Send for AtomicU8 {}
unsafe impl Sync for AtomicU8 {}
impl AtomicU8 {
- pub(crate) fn new(val: u8) -> AtomicU8 {
+ pub(crate) const fn new(val: u8) -> AtomicU8 {
let inner = UnsafeCell::new(std::sync::atomic::AtomicU8::new(val));
AtomicU8 { inner }
}
diff --git a/tokio/src/loom/std/unsafe_cell.rs b/tokio/src/loom/std/unsafe_cell.rs
index f2b03d8d..66c1d794 100644
--- a/tokio/src/loom/std/unsafe_cell.rs
+++ b/tokio/src/loom/std/unsafe_cell.rs
@@ -2,7 +2,7 @@
pub(crate) struct UnsafeCell<T>(std::cell::UnsafeCell<T>);
impl<T> UnsafeCell<T> {
- pub(crate) fn new(data: T) -> UnsafeCell<T> {
+ pub(crate) const fn new(data: T) -> UnsafeCell<T> {
UnsafeCell(std::cell::UnsafeCell::new(data))
}
diff --git a/tokio/src/sync/notify.rs b/tokio/src/sync/notify.rs
index 74c69e50..c69e2b07 100644
--- a/tokio/src/sync/notify.rs
+++ b/tokio/src/sync/notify.rs
@@ -170,6 +170,24 @@ impl Notify {
}
}
+ /// Create a new `Notify`, initialized without a permit.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use tokio::sync::Notify;
+ ///
+ /// static NOTIFY: Notify = Notify::const_new();
+ /// ```
+ #[cfg(all(feature = "parking_lot", not(all(loom, test))))]
+ #[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
+ pub const fn const_new() -> Notify {
+ Notify {
+ state: AtomicU8::new(0),
+ waiters: Mutex::const_new(LinkedList::new()),
+ }
+ }
+
/// Wait for a notification.
///
/// Equivalent to:
diff --git a/tokio/src/sync/rwlock.rs b/tokio/src/sync/rwlock.rs
index 650a7cf6..840889ba 100644
--- a/tokio/src/sync/rwlock.rs
+++ b/tokio/src/sync/rwlock.rs
@@ -485,6 +485,27 @@ impl<T: ?Sized> RwLock<T> {
}
}
+ /// Creates a new instance of an `RwLock<T>` which is unlocked.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use tokio::sync::RwLock;
+ ///
+ /// static LOCK: RwLock<i32> = RwLock::const_new(5);
+ /// ```
+ #[cfg(all(feature = "parking_lot", not(all(loom, test))))]
+ #[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
+ pub const fn const_new(value: T) -> RwLock<T>
+ where
+ T: Sized,
+ {
+ RwLock {
+ c: UnsafeCell::new(value),
+ s: Semaphore::const_new(MAX_READS),
+ }
+ }
+
/// Locks this rwlock with shared read access, causing the current task
/// to yield until the lock has been acquired.
///
diff --git a/tokio/src/sync/semaphore.rs b/tokio/src/sync/semaphore.rs
index 2489d34a..136f14e6 100644
--- a/tokio/src/sync/semaphore.rs
+++ b/tokio/src/sync/semaphore.rs
@@ -74,6 +74,15 @@ impl Semaphore {
}
}
+ /// Creates a new semaphore with the initial number of permits.
+ #[cfg(all(feature = "parking_lot", not(all(loom, test))))]
+ #[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
+ pub const fn const_new(permits: usize) -> Self {
+ Self {
+ ll_sem: ll::Semaphore::const_new(permits),
+ }
+ }
+
/// Returns the current number of available permits.
pub fn available_permits(&self) -> usize {
self.ll_sem.available_permits()