diff options
| author | Connor Tsui <connor.tsui20@gmail.com> | 2025-07-24 15:24:22 +0200 |
|---|---|---|
| committer | Connor Tsui <connor.tsui20@gmail.com> | 2025-08-23 09:20:47 -0400 |
| commit | b8ee38b79e6bfbb1376785f765a233e96d0c5da2 (patch) | |
| tree | bafd3b9c183dd56d61d2850013a15f53b7fb5cda /library/std/src | |
| parent | a8163bd6f4f2bc3ba8fb56df9d48622070ba68ab (diff) | |
| download | rust-b8ee38b79e6bfbb1376785f765a233e96d0c5da2.tar.gz rust-b8ee38b79e6bfbb1376785f765a233e96d0c5da2.zip | |
move `WaitTimeoutResult` up to `mod.rs`
Since `WaitTimeoutResult` is poison-agnostic, we want to use the same type for both variants of `Condvar`. Signed-off-by: Connor Tsui <connor.tsui20@gmail.com>
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/sync/mod.rs | 65 | ||||
| -rw-r--r-- | library/std/src/sync/nonpoison.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sync/nonpoison/condvar.rs | 70 | ||||
| -rw-r--r-- | library/std/src/sync/poison.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sync/poison/condvar.rs | 66 |
5 files changed, 68 insertions, 137 deletions
diff --git a/library/std/src/sync/mod.rs b/library/std/src/sync/mod.rs index 6ef3bf25cf6..97c04d07eaf 100644 --- a/library/std/src/sync/mod.rs +++ b/library/std/src/sync/mod.rs @@ -209,7 +209,7 @@ pub use self::poison::{LockResult, PoisonError}; #[doc(inline)] pub use self::poison::{ Mutex, MutexGuard, TryLockError, TryLockResult, - Condvar, WaitTimeoutResult, + Condvar, Once, OnceState, RwLock, RwLockReadGuard, RwLockWriteGuard, }; @@ -234,3 +234,66 @@ mod barrier; mod lazy_lock; mod once_lock; mod reentrant_lock; + +/// A type indicating whether a timed wait on a condition variable returned +/// due to a time out or not. +/// +/// It is returned by the [`wait_timeout`] method. +/// +/// [`wait_timeout`]: Condvar::wait_timeout +#[derive(Debug, PartialEq, Eq, Copy, Clone)] +#[stable(feature = "wait_timeout", since = "1.5.0")] +pub struct WaitTimeoutResult(bool); + +impl WaitTimeoutResult { + /// Returns `true` if the wait was known to have timed out. + /// + /// # Examples + /// + /// This example spawns a thread which will sleep 20 milliseconds before + /// updating a boolean value and then notifying the condvar. + /// + /// The main thread will wait with a 10 millisecond timeout on the condvar + /// and will leave the loop upon timeout. + /// + /// ``` + /// use std::sync::{Arc, Condvar, Mutex}; + /// use std::thread; + /// use std::time::Duration; + /// + /// let pair = Arc::new((Mutex::new(false), Condvar::new())); + /// let pair2 = Arc::clone(&pair); + /// + /// # let handle = + /// thread::spawn(move || { + /// let (lock, cvar) = &*pair2; + /// + /// // Let's wait 20 milliseconds before notifying the condvar. + /// thread::sleep(Duration::from_millis(20)); + /// + /// let mut started = lock.lock().unwrap(); + /// // We update the boolean value. + /// *started = true; + /// cvar.notify_one(); + /// }); + /// + /// // Wait for the thread to start up. + /// let (lock, cvar) = &*pair; + /// loop { + /// // Let's put a timeout on the condvar's wait. + /// let result = cvar.wait_timeout(lock.lock().unwrap(), Duration::from_millis(10)).unwrap(); + /// // 10 milliseconds have passed. + /// if result.1.timed_out() { + /// // timed out now and we can leave. + /// break + /// } + /// } + /// # // Prevent leaks for Miri. + /// # let _ = handle.join(); + /// ``` + #[must_use] + #[stable(feature = "wait_timeout", since = "1.5.0")] + pub fn timed_out(&self) -> bool { + self.0 + } +} diff --git a/library/std/src/sync/nonpoison.rs b/library/std/src/sync/nonpoison.rs index 96ecf7084fa..ec3587263f4 100644 --- a/library/std/src/sync/nonpoison.rs +++ b/library/std/src/sync/nonpoison.rs @@ -30,7 +30,7 @@ impl fmt::Display for WouldBlock { } #[unstable(feature = "nonpoison_condvar", issue = "134645")] -pub use self::condvar::{Condvar, WaitTimeoutResult}; +pub use self::condvar::Condvar; #[unstable(feature = "mapped_lock_guards", issue = "117108")] pub use self::mutex::MappedMutexGuard; #[unstable(feature = "nonpoison_mutex", issue = "134645")] diff --git a/library/std/src/sync/nonpoison/condvar.rs b/library/std/src/sync/nonpoison/condvar.rs index 9744c87d9cc..49afdd87818 100644 --- a/library/std/src/sync/nonpoison/condvar.rs +++ b/library/std/src/sync/nonpoison/condvar.rs @@ -1,77 +1,9 @@ use crate::fmt; +use crate::sync::WaitTimeoutResult; use crate::sync::nonpoison::{MutexGuard, mutex}; use crate::sys::sync as sys; use crate::time::{Duration, Instant}; -/// A type indicating whether a timed wait on a condition variable returned -/// due to a time out or not. -/// -/// It is returned by the [`wait_timeout`] method. -/// -/// [`wait_timeout`]: Condvar::wait_timeout -#[derive(Debug, PartialEq, Eq, Copy, Clone)] -#[unstable(feature = "nonpoison_condvar", issue = "134645")] -pub struct WaitTimeoutResult(bool); - -// FIXME(nonpoison_condvar) this type is duplicated in `poison`. How do we share types that are -// poison-agnostic? -impl WaitTimeoutResult { - /// Returns `true` if the wait was known to have timed out. - /// - /// # Examples - /// - /// This example spawns a thread which will sleep 20 milliseconds before - /// updating a boolean value and then notifying the condvar. - /// - /// The main thread will wait with a 10 millisecond timeout on the condvar - /// and will leave the loop upon timeout. - /// - /// ``` - /// #![feature(nonpoison_mutex)] - /// #![feature(nonpoison_condvar)] - /// - /// use std::sync::nonpoison::{Mutex, Condvar}; - /// use std::sync::Arc; - /// use std::thread; - /// use std::time::Duration; - /// - /// let pair = Arc::new((Mutex::new(false), Condvar::new())); - /// let pair2 = Arc::clone(&pair); - /// - /// # let handle = - /// thread::spawn(move || { - /// let (lock, cvar) = &*pair2; - /// - /// // Let's wait 20 milliseconds before notifying the condvar. - /// thread::sleep(Duration::from_millis(20)); - /// - /// let mut started = lock.lock(); - /// // We update the boolean value. - /// *started = true; - /// cvar.notify_one(); - /// }); - /// - /// // Wait for the thread to start up. - /// let (lock, cvar) = &*pair; - /// loop { - /// // Let's put a timeout on the condvar's wait. - /// let result = cvar.wait_timeout(lock.lock(), Duration::from_millis(10)); - /// // 10 milliseconds have passed. - /// if result.1.timed_out() { - /// // timed out now and we can leave. - /// break - /// } - /// } - /// # // Prevent leaks for Miri. - /// # let _ = handle.join(); - /// ``` - #[must_use] - #[unstable(feature = "nonpoison_condvar", issue = "134645")] - pub fn timed_out(&self) -> bool { - self.0 - } -} - /// A Condition Variable /// /// For more information about condition variables, check out the documentation for the poisoning diff --git a/library/std/src/sync/poison.rs b/library/std/src/sync/poison.rs index 31889dcc10f..dc4774dcca1 100644 --- a/library/std/src/sync/poison.rs +++ b/library/std/src/sync/poison.rs @@ -61,7 +61,7 @@ //! then the lock will not be poisoned. #[stable(feature = "rust1", since = "1.0.0")] -pub use self::condvar::{Condvar, WaitTimeoutResult}; +pub use self::condvar::Condvar; #[unstable(feature = "mapped_lock_guards", issue = "117108")] pub use self::mutex::MappedMutexGuard; #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/sync/poison/condvar.rs b/library/std/src/sync/poison/condvar.rs index 0e9d4233c65..5dc2b510f3a 100644 --- a/library/std/src/sync/poison/condvar.rs +++ b/library/std/src/sync/poison/condvar.rs @@ -1,73 +1,9 @@ use crate::fmt; +use crate::sync::WaitTimeoutResult; use crate::sync::poison::{self, LockResult, MutexGuard, PoisonError, mutex}; use crate::sys::sync as sys; use crate::time::{Duration, Instant}; -/// A type indicating whether a timed wait on a condition variable returned -/// due to a time out or not. -/// -/// It is returned by the [`wait_timeout`] method. -/// -/// [`wait_timeout`]: Condvar::wait_timeout -#[derive(Debug, PartialEq, Eq, Copy, Clone)] -#[stable(feature = "wait_timeout", since = "1.5.0")] -pub struct WaitTimeoutResult(bool); - -// FIXME(nonpoison_condvar): `WaitTimeoutResult` is actually poisoning-agnostic, it seems. -// Should we take advantage of this fact? -impl WaitTimeoutResult { - /// Returns `true` if the wait was known to have timed out. - /// - /// # Examples - /// - /// This example spawns a thread which will sleep 20 milliseconds before - /// updating a boolean value and then notifying the condvar. - /// - /// The main thread will wait with a 10 millisecond timeout on the condvar - /// and will leave the loop upon timeout. - /// - /// ``` - /// use std::sync::{Arc, Condvar, Mutex}; - /// use std::thread; - /// use std::time::Duration; - /// - /// let pair = Arc::new((Mutex::new(false), Condvar::new())); - /// let pair2 = Arc::clone(&pair); - /// - /// # let handle = - /// thread::spawn(move || { - /// let (lock, cvar) = &*pair2; - /// - /// // Let's wait 20 milliseconds before notifying the condvar. - /// thread::sleep(Duration::from_millis(20)); - /// - /// let mut started = lock.lock().unwrap(); - /// // We update the boolean value. - /// *started = true; - /// cvar.notify_one(); - /// }); - /// - /// // Wait for the thread to start up. - /// let (lock, cvar) = &*pair; - /// loop { - /// // Let's put a timeout on the condvar's wait. - /// let result = cvar.wait_timeout(lock.lock().unwrap(), Duration::from_millis(10)).unwrap(); - /// // 10 milliseconds have passed. - /// if result.1.timed_out() { - /// // timed out now and we can leave. - /// break - /// } - /// } - /// # // Prevent leaks for Miri. - /// # let _ = handle.join(); - /// ``` - #[must_use] - #[stable(feature = "wait_timeout", since = "1.5.0")] - pub fn timed_out(&self) -> bool { - self.0 - } -} - /// A Condition Variable /// /// Condition variables represent the ability to block a thread such that it |
