summaryrefslogtreecommitdiffstats
path: root/tokio/src/sync
diff options
context:
space:
mode:
Diffstat (limited to 'tokio/src/sync')
-rw-r--r--tokio/src/sync/notify.rs18
-rw-r--r--tokio/src/sync/rwlock.rs21
-rw-r--r--tokio/src/sync/semaphore.rs9
3 files changed, 48 insertions, 0 deletions
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()