]> git.lizzy.rs Git - rust.git/blobdiff - library/std/src/sync/condvar.rs
Auto merge of #76128 - poliorcetics:doc-use-arc-clone, r=KodrAus
[rust.git] / library / std / src / sync / condvar.rs
index 9b90bfd68b50f3047ec1ee6e01245efe98311a11..bc01c26a86aced8603896c7a931272a2877fb1cd 100644 (file)
@@ -1,3 +1,6 @@
+#[cfg(test)]
+mod tests;
+
 use crate::fmt;
 use crate::sync::atomic::{AtomicUsize, Ordering};
 use crate::sync::{mutex, MutexGuard, PoisonError};
@@ -11,7 +14,7 @@
 ///
 /// It is returned by the [`wait_timeout`] method.
 ///
-/// [`wait_timeout`]: struct.Condvar.html#method.wait_timeout
+/// [`wait_timeout`]: Condvar::wait_timeout
 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
 #[stable(feature = "wait_timeout", since = "1.5.0")]
 pub struct WaitTimeoutResult(bool);
@@ -33,7 +36,7 @@ impl WaitTimeoutResult {
     /// use std::time::Duration;
     ///
     /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
-    /// let pair2 = pair.clone();
+    /// let pair2 = Arc::clone(&pair);
     ///
     /// thread::spawn(move || {
     ///     let (lock, cvar) = &*pair2;
@@ -90,7 +93,7 @@ pub fn timed_out(&self) -> bool {
 /// use std::thread;
 ///
 /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
-/// let pair2 = pair.clone();
+/// let pair2 = Arc::clone(&pair);
 ///
 /// // Inside of our lock, spawn a new thread, and then wait for it to start.
 /// thread::spawn(move|| {
@@ -161,11 +164,10 @@ pub fn new() -> Condvar {
     /// mutex to ensure defined behavior across platforms. If this functionality
     /// is not desired, then unsafe primitives in `sys` are provided.
     ///
-    /// [`notify_one`]: #method.notify_one
-    /// [`notify_all`]: #method.notify_all
-    /// [poisoning]: ../sync/struct.Mutex.html#poisoning
-    /// [`Mutex`]: ../sync/struct.Mutex.html
-    /// [`panic!`]: ../../std/macro.panic.html
+    /// [`notify_one`]: Self::notify_one
+    /// [`notify_all`]: Self::notify_all
+    /// [poisoning]: super::Mutex#poisoning
+    /// [`Mutex`]: super::Mutex
     ///
     /// # Examples
     ///
@@ -174,7 +176,7 @@ pub fn new() -> Condvar {
     /// use std::thread;
     ///
     /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
-    /// let pair2 = pair.clone();
+    /// let pair2 = Arc::clone(&pair);
     ///
     /// thread::spawn(move|| {
     ///     let (lock, cvar) = &*pair2;
@@ -218,10 +220,10 @@ pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a,
     /// poisoned when this thread re-acquires the lock. For more information,
     /// see information about [poisoning] on the [`Mutex`] type.
     ///
-    /// [`notify_one`]: #method.notify_one
-    /// [`notify_all`]: #method.notify_all
-    /// [poisoning]: ../sync/struct.Mutex.html#poisoning
-    /// [`Mutex`]: ../sync/struct.Mutex.html
+    /// [`notify_one`]: Self::notify_one
+    /// [`notify_all`]: Self::notify_all
+    /// [poisoning]: super::Mutex#poisoning
+    /// [`Mutex`]: super::Mutex
     ///
     /// # Examples
     ///
@@ -230,7 +232,7 @@ pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a,
     /// use std::thread;
     ///
     /// let pair = Arc::new((Mutex::new(true), Condvar::new()));
-    /// let pair2 = pair.clone();
+    /// let pair2 = Arc::clone(&pair);
     ///
     /// thread::spawn(move|| {
     ///     let (lock, cvar) = &*pair2;
@@ -280,7 +282,7 @@ pub fn wait_while<'a, T, F>(
     /// Like [`wait`], the lock specified will be re-acquired when this function
     /// returns, regardless of whether the timeout elapsed or not.
     ///
-    /// [`wait`]: #method.wait
+    /// [`wait`]: Self::wait
     ///
     /// # Examples
     ///
@@ -289,7 +291,7 @@ pub fn wait_while<'a, T, F>(
     /// use std::thread;
     ///
     /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
-    /// let pair2 = pair.clone();
+    /// let pair2 = Arc::clone(&pair);
     ///
     /// thread::spawn(move|| {
     ///     let (lock, cvar) = &*pair2;
@@ -350,9 +352,8 @@ pub fn wait_timeout_ms<'a, T>(
     /// Like [`wait`], the lock specified will be re-acquired when this function
     /// returns, regardless of whether the timeout elapsed or not.
     ///
-    /// [`wait`]: #method.wait
-    /// [`wait_timeout_while`]: #method.wait_timeout_while
-    /// [`WaitTimeoutResult`]: struct.WaitTimeoutResult.html
+    /// [`wait`]: Self::wait
+    /// [`wait_timeout_while`]: Self::wait_timeout_while
     ///
     /// # Examples
     ///
@@ -362,7 +363,7 @@ pub fn wait_timeout_ms<'a, T>(
     /// use std::time::Duration;
     ///
     /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
-    /// let pair2 = pair.clone();
+    /// let pair2 = Arc::clone(&pair);
     ///
     /// thread::spawn(move|| {
     ///     let (lock, cvar) = &*pair2;
@@ -420,9 +421,8 @@ pub fn wait_timeout<'a, T>(
     /// Like [`wait_while`], the lock specified will be re-acquired when this
     /// function returns, regardless of whether the timeout elapsed or not.
     ///
-    /// [`wait_while`]: #method.wait_while
-    /// [`wait_timeout`]: #method.wait_timeout
-    /// [`WaitTimeoutResult`]: struct.WaitTimeoutResult.html
+    /// [`wait_while`]: Self::wait_while
+    /// [`wait_timeout`]: Self::wait_timeout
     ///
     /// # Examples
     ///
@@ -432,7 +432,7 @@ pub fn wait_timeout<'a, T>(
     /// use std::time::Duration;
     ///
     /// let pair = Arc::new((Mutex::new(true), Condvar::new()));
-    /// let pair2 = pair.clone();
+    /// let pair2 = Arc::clone(&pair);
     ///
     /// thread::spawn(move|| {
     ///     let (lock, cvar) = &*pair2;
@@ -485,9 +485,9 @@ pub fn wait_timeout_while<'a, T, F>(
     ///
     /// To wake up all threads, see [`notify_all`].
     ///
-    /// [`wait`]: #method.wait
-    /// [`wait_timeout`]: #method.wait_timeout
-    /// [`notify_all`]: #method.notify_all
+    /// [`wait`]: Self::wait
+    /// [`wait_timeout`]: Self::wait_timeout
+    /// [`notify_all`]: Self::notify_all
     ///
     /// # Examples
     ///
@@ -496,7 +496,7 @@ pub fn wait_timeout_while<'a, T, F>(
     /// use std::thread;
     ///
     /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
-    /// let pair2 = pair.clone();
+    /// let pair2 = Arc::clone(&pair);
     ///
     /// thread::spawn(move|| {
     ///     let (lock, cvar) = &*pair2;
@@ -527,7 +527,7 @@ pub fn notify_one(&self) {
     ///
     /// To wake up only one thread, see [`notify_one`].
     ///
-    /// [`notify_one`]: #method.notify_one
+    /// [`notify_one`]: Self::notify_one
     ///
     /// # Examples
     ///
@@ -536,7 +536,7 @@ pub fn notify_one(&self) {
     /// use std::thread;
     ///
     /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
-    /// let pair2 = pair.clone();
+    /// let pair2 = Arc::clone(&pair);
     ///
     /// thread::spawn(move|| {
     ///     let (lock, cvar) = &*pair2;
@@ -601,218 +601,3 @@ fn drop(&mut self) {
         unsafe { self.inner.destroy() }
     }
 }
-
-#[cfg(test)]
-mod tests {
-    use crate::sync::atomic::{AtomicBool, Ordering};
-    use crate::sync::mpsc::channel;
-    use crate::sync::{Arc, Condvar, Mutex};
-    use crate::thread;
-    use crate::time::Duration;
-
-    #[test]
-    fn smoke() {
-        let c = Condvar::new();
-        c.notify_one();
-        c.notify_all();
-    }
-
-    #[test]
-    #[cfg_attr(target_os = "emscripten", ignore)]
-    fn notify_one() {
-        let m = Arc::new(Mutex::new(()));
-        let m2 = m.clone();
-        let c = Arc::new(Condvar::new());
-        let c2 = c.clone();
-
-        let g = m.lock().unwrap();
-        let _t = thread::spawn(move || {
-            let _g = m2.lock().unwrap();
-            c2.notify_one();
-        });
-        let g = c.wait(g).unwrap();
-        drop(g);
-    }
-
-    #[test]
-    #[cfg_attr(target_os = "emscripten", ignore)]
-    fn notify_all() {
-        const N: usize = 10;
-
-        let data = Arc::new((Mutex::new(0), Condvar::new()));
-        let (tx, rx) = channel();
-        for _ in 0..N {
-            let data = data.clone();
-            let tx = tx.clone();
-            thread::spawn(move || {
-                let &(ref lock, ref cond) = &*data;
-                let mut cnt = lock.lock().unwrap();
-                *cnt += 1;
-                if *cnt == N {
-                    tx.send(()).unwrap();
-                }
-                while *cnt != 0 {
-                    cnt = cond.wait(cnt).unwrap();
-                }
-                tx.send(()).unwrap();
-            });
-        }
-        drop(tx);
-
-        let &(ref lock, ref cond) = &*data;
-        rx.recv().unwrap();
-        let mut cnt = lock.lock().unwrap();
-        *cnt = 0;
-        cond.notify_all();
-        drop(cnt);
-
-        for _ in 0..N {
-            rx.recv().unwrap();
-        }
-    }
-
-    #[test]
-    #[cfg_attr(target_os = "emscripten", ignore)]
-    fn wait_while() {
-        let pair = Arc::new((Mutex::new(false), Condvar::new()));
-        let pair2 = pair.clone();
-
-        // Inside of our lock, spawn a new thread, and then wait for it to start.
-        thread::spawn(move || {
-            let &(ref lock, ref cvar) = &*pair2;
-            let mut started = lock.lock().unwrap();
-            *started = true;
-            // We notify the condvar that the value has changed.
-            cvar.notify_one();
-        });
-
-        // Wait for the thread to start up.
-        let &(ref lock, ref cvar) = &*pair;
-        let guard = cvar.wait_while(lock.lock().unwrap(), |started| !*started);
-        assert!(*guard.unwrap());
-    }
-
-    #[test]
-    #[cfg_attr(target_os = "emscripten", ignore)]
-    fn wait_timeout_wait() {
-        let m = Arc::new(Mutex::new(()));
-        let c = Arc::new(Condvar::new());
-
-        loop {
-            let g = m.lock().unwrap();
-            let (_g, no_timeout) = c.wait_timeout(g, Duration::from_millis(1)).unwrap();
-            // spurious wakeups mean this isn't necessarily true
-            // so execute test again, if not timeout
-            if !no_timeout.timed_out() {
-                continue;
-            }
-
-            break;
-        }
-    }
-
-    #[test]
-    #[cfg_attr(target_os = "emscripten", ignore)]
-    fn wait_timeout_while_wait() {
-        let m = Arc::new(Mutex::new(()));
-        let c = Arc::new(Condvar::new());
-
-        let g = m.lock().unwrap();
-        let (_g, wait) = c.wait_timeout_while(g, Duration::from_millis(1), |_| true).unwrap();
-        // no spurious wakeups. ensure it timed-out
-        assert!(wait.timed_out());
-    }
-
-    #[test]
-    #[cfg_attr(target_os = "emscripten", ignore)]
-    fn wait_timeout_while_instant_satisfy() {
-        let m = Arc::new(Mutex::new(()));
-        let c = Arc::new(Condvar::new());
-
-        let g = m.lock().unwrap();
-        let (_g, wait) = c.wait_timeout_while(g, Duration::from_millis(0), |_| false).unwrap();
-        // ensure it didn't time-out even if we were not given any time.
-        assert!(!wait.timed_out());
-    }
-
-    #[test]
-    #[cfg_attr(target_os = "emscripten", ignore)]
-    fn wait_timeout_while_wake() {
-        let pair = Arc::new((Mutex::new(false), Condvar::new()));
-        let pair_copy = pair.clone();
-
-        let &(ref m, ref c) = &*pair;
-        let g = m.lock().unwrap();
-        let _t = thread::spawn(move || {
-            let &(ref lock, ref cvar) = &*pair_copy;
-            let mut started = lock.lock().unwrap();
-            thread::sleep(Duration::from_millis(1));
-            *started = true;
-            cvar.notify_one();
-        });
-        let (g2, wait) = c
-            .wait_timeout_while(g, Duration::from_millis(u64::MAX), |&mut notified| !notified)
-            .unwrap();
-        // ensure it didn't time-out even if we were not given any time.
-        assert!(!wait.timed_out());
-        assert!(*g2);
-    }
-
-    #[test]
-    #[cfg_attr(target_os = "emscripten", ignore)]
-    fn wait_timeout_wake() {
-        let m = Arc::new(Mutex::new(()));
-        let c = Arc::new(Condvar::new());
-
-        loop {
-            let g = m.lock().unwrap();
-
-            let c2 = c.clone();
-            let m2 = m.clone();
-
-            let notified = Arc::new(AtomicBool::new(false));
-            let notified_copy = notified.clone();
-
-            let t = thread::spawn(move || {
-                let _g = m2.lock().unwrap();
-                thread::sleep(Duration::from_millis(1));
-                notified_copy.store(true, Ordering::SeqCst);
-                c2.notify_one();
-            });
-            let (g, timeout_res) = c.wait_timeout(g, Duration::from_millis(u64::MAX)).unwrap();
-            assert!(!timeout_res.timed_out());
-            // spurious wakeups mean this isn't necessarily true
-            // so execute test again, if not notified
-            if !notified.load(Ordering::SeqCst) {
-                t.join().unwrap();
-                continue;
-            }
-            drop(g);
-
-            t.join().unwrap();
-
-            break;
-        }
-    }
-
-    #[test]
-    #[should_panic]
-    #[cfg_attr(target_os = "emscripten", ignore)]
-    fn two_mutexes() {
-        let m = Arc::new(Mutex::new(()));
-        let m2 = m.clone();
-        let c = Arc::new(Condvar::new());
-        let c2 = c.clone();
-
-        let mut g = m.lock().unwrap();
-        let _t = thread::spawn(move || {
-            let _g = m2.lock().unwrap();
-            c2.notify_one();
-        });
-        g = c.wait(g).unwrap();
-        drop(g);
-
-        let m = Mutex::new(());
-        let _ = c.wait(m.lock().unwrap()).unwrap();
-    }
-}