std/sync/poison/condvar.rs
1use crate::fmt;
2use crate::sync::WaitTimeoutResult;
3use crate::sync::poison::{self, LockResult, MutexGuard, PoisonError, mutex};
4use crate::sys::sync as sys;
5use crate::time::{Duration, Instant};
6
7/// A Condition Variable
8///
9/// Condition variables represent the ability to block a thread such that it
10/// consumes no CPU time while waiting for an event to occur. Condition
11/// variables are typically associated with a boolean predicate (a condition)
12/// and a mutex. The predicate is always verified inside of the mutex before
13/// determining that a thread must block.
14///
15/// Functions in this module will block the current **thread** of execution.
16/// Note that any attempt to use multiple mutexes on the same condition
17/// variable may result in a runtime panic.
18///
19/// # Examples
20///
21/// ```
22/// use std::sync::{Arc, Mutex, Condvar};
23/// use std::thread;
24///
25/// let pair = Arc::new((Mutex::new(false), Condvar::new()));
26/// let pair2 = Arc::clone(&pair);
27///
28/// // Inside of our lock, spawn a new thread, and then wait for it to start.
29/// thread::spawn(move || {
30/// let (lock, cvar) = &*pair2;
31/// let mut started = lock.lock().unwrap();
32/// *started = true;
33/// // We notify the condvar that the value has changed.
34/// cvar.notify_one();
35/// });
36///
37/// // Wait for the thread to start up.
38/// let (lock, cvar) = &*pair;
39/// let mut started = lock.lock().unwrap();
40/// while !*started {
41/// started = cvar.wait(started).unwrap();
42/// }
43/// ```
44#[stable(feature = "rust1", since = "1.0.0")]
45pub struct Condvar {
46 inner: sys::Condvar,
47}
48
49impl Condvar {
50 /// Creates a new condition variable which is ready to be waited on and
51 /// notified.
52 ///
53 /// # Examples
54 ///
55 /// ```
56 /// use std::sync::Condvar;
57 ///
58 /// let condvar = Condvar::new();
59 /// ```
60 #[stable(feature = "rust1", since = "1.0.0")]
61 #[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
62 #[must_use]
63 #[inline]
64 pub const fn new() -> Condvar {
65 Condvar { inner: sys::Condvar::new() }
66 }
67
68 /// Blocks the current thread until this condition variable receives a
69 /// notification.
70 ///
71 /// This function will atomically unlock the mutex specified (represented by
72 /// `guard`) and block the current thread. This means that any calls
73 /// to [`notify_one`] or [`notify_all`] which happen logically after the
74 /// mutex is unlocked are candidates to wake this thread up. When this
75 /// function call returns, the lock specified will have been re-acquired.
76 ///
77 /// Note that this function is susceptible to spurious wakeups. Condition
78 /// variables normally have a boolean predicate associated with them, and
79 /// the predicate must always be checked each time this function returns to
80 /// protect against spurious wakeups.
81 ///
82 /// # Errors
83 ///
84 /// This function will return an error if the mutex being waited on is
85 /// poisoned when this thread re-acquires the lock. For more information,
86 /// see information about [poisoning] on the [`Mutex`] type.
87 ///
88 /// # Panics
89 ///
90 /// This function may [`panic!`] if it is used with more than one mutex
91 /// over time.
92 ///
93 /// [`notify_one`]: Self::notify_one
94 /// [`notify_all`]: Self::notify_all
95 /// [poisoning]: super::Mutex#poisoning
96 /// [`Mutex`]: super::Mutex
97 ///
98 /// # Examples
99 ///
100 /// ```
101 /// use std::sync::{Arc, Mutex, Condvar};
102 /// use std::thread;
103 ///
104 /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
105 /// let pair2 = Arc::clone(&pair);
106 ///
107 /// thread::spawn(move || {
108 /// let (lock, cvar) = &*pair2;
109 /// let mut started = lock.lock().unwrap();
110 /// *started = true;
111 /// // We notify the condvar that the value has changed.
112 /// cvar.notify_one();
113 /// });
114 ///
115 /// // Wait for the thread to start up.
116 /// let (lock, cvar) = &*pair;
117 /// let mut started = lock.lock().unwrap();
118 /// // As long as the value inside the `Mutex<bool>` is `false`, we wait.
119 /// while !*started {
120 /// started = cvar.wait(started).unwrap();
121 /// }
122 /// ```
123 #[stable(feature = "rust1", since = "1.0.0")]
124 #[rustc_should_not_be_called_on_const_items]
125 pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
126 let poisoned = unsafe {
127 let lock = mutex::guard_lock(&guard);
128 self.inner.wait(lock);
129 mutex::guard_poison(&guard).get()
130 };
131 if poisoned { Err(PoisonError::new(guard)) } else { Ok(guard) }
132 }
133
134 /// Blocks the current thread until the provided condition becomes false.
135 ///
136 /// `condition` is checked immediately; if not met (returns `true`), this
137 /// will [`wait`] for the next notification then check again. This repeats
138 /// until `condition` returns `false`, in which case this function returns.
139 ///
140 /// This function will atomically unlock the mutex specified (represented by
141 /// `guard`) and block the current thread. This means that any calls
142 /// to [`notify_one`] or [`notify_all`] which happen logically after the
143 /// mutex is unlocked are candidates to wake this thread up. When this
144 /// function call returns, the lock specified will have been re-acquired.
145 ///
146 /// # Errors
147 ///
148 /// This function will return an error if the mutex being waited on is
149 /// poisoned when this thread re-acquires the lock. For more information,
150 /// see information about [poisoning] on the [`Mutex`] type.
151 ///
152 /// [`wait`]: Self::wait
153 /// [`notify_one`]: Self::notify_one
154 /// [`notify_all`]: Self::notify_all
155 /// [poisoning]: super::Mutex#poisoning
156 /// [`Mutex`]: super::Mutex
157 ///
158 /// # Examples
159 ///
160 /// ```
161 /// use std::sync::{Arc, Mutex, Condvar};
162 /// use std::thread;
163 ///
164 /// let pair = Arc::new((Mutex::new(true), Condvar::new()));
165 /// let pair2 = Arc::clone(&pair);
166 ///
167 /// thread::spawn(move || {
168 /// let (lock, cvar) = &*pair2;
169 /// let mut pending = lock.lock().unwrap();
170 /// *pending = false;
171 /// // We notify the condvar that the value has changed.
172 /// cvar.notify_one();
173 /// });
174 ///
175 /// // Wait for the thread to start up.
176 /// let (lock, cvar) = &*pair;
177 /// // As long as the value inside the `Mutex<bool>` is `true`, we wait.
178 /// let _guard = cvar.wait_while(lock.lock().unwrap(), |pending| { *pending }).unwrap();
179 /// ```
180 #[stable(feature = "wait_until", since = "1.42.0")]
181 #[rustc_should_not_be_called_on_const_items]
182 pub fn wait_while<'a, T, F>(
183 &self,
184 mut guard: MutexGuard<'a, T>,
185 mut condition: F,
186 ) -> LockResult<MutexGuard<'a, T>>
187 where
188 F: FnMut(&mut T) -> bool,
189 {
190 while condition(&mut *guard) {
191 guard = self.wait(guard)?;
192 }
193 Ok(guard)
194 }
195
196 /// Waits on this condition variable for a notification, timing out after a
197 /// specified duration.
198 ///
199 /// The semantics of this function are equivalent to [`wait`]
200 /// except that the thread will be blocked for roughly no longer
201 /// than `ms` milliseconds. This method should not be used for
202 /// precise timing due to anomalies such as preemption or platform
203 /// differences that might not cause the maximum amount of time
204 /// waited to be precisely `ms`.
205 ///
206 /// Note that the best effort is made to ensure that the time waited is
207 /// measured with a monotonic clock, and not affected by the changes made to
208 /// the system time.
209 ///
210 /// The returned boolean is `false` only if the timeout is known
211 /// to have elapsed.
212 ///
213 /// Like [`wait`], the lock specified will be re-acquired when this function
214 /// returns, regardless of whether the timeout elapsed or not.
215 ///
216 /// [`wait`]: Self::wait
217 ///
218 /// # Examples
219 ///
220 /// ```
221 /// use std::sync::{Arc, Mutex, Condvar};
222 /// use std::thread;
223 ///
224 /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
225 /// let pair2 = Arc::clone(&pair);
226 ///
227 /// thread::spawn(move || {
228 /// let (lock, cvar) = &*pair2;
229 /// let mut started = lock.lock().unwrap();
230 /// *started = true;
231 /// // We notify the condvar that the value has changed.
232 /// cvar.notify_one();
233 /// });
234 ///
235 /// // Wait for the thread to start up.
236 /// let (lock, cvar) = &*pair;
237 /// let mut started = lock.lock().unwrap();
238 /// // As long as the value inside the `Mutex<bool>` is `false`, we wait.
239 /// loop {
240 /// let result = cvar.wait_timeout_ms(started, 10).unwrap();
241 /// // 10 milliseconds have passed, or maybe the value changed!
242 /// started = result.0;
243 /// if *started == true {
244 /// // We received the notification and the value has been updated, we can leave.
245 /// break
246 /// }
247 /// }
248 /// ```
249 #[stable(feature = "rust1", since = "1.0.0")]
250 #[rustc_should_not_be_called_on_const_items]
251 #[deprecated(since = "1.6.0", note = "replaced by `std::sync::Condvar::wait_timeout`")]
252 pub fn wait_timeout_ms<'a, T>(
253 &self,
254 guard: MutexGuard<'a, T>,
255 ms: u32,
256 ) -> LockResult<(MutexGuard<'a, T>, bool)> {
257 let res = self.wait_timeout(guard, Duration::from_millis(ms as u64));
258 poison::map_result(res, |(a, b)| (a, !b.timed_out()))
259 }
260
261 /// Waits on this condition variable for a notification, timing out after a
262 /// specified duration.
263 ///
264 /// The semantics of this function are equivalent to [`wait`] except that
265 /// the thread will be blocked for roughly no longer than `dur`. This
266 /// method should not be used for precise timing due to anomalies such as
267 /// preemption or platform differences that might not cause the maximum
268 /// amount of time waited to be precisely `dur`.
269 ///
270 /// Note that the best effort is made to ensure that the time waited is
271 /// measured with a monotonic clock, and not affected by the changes made to
272 /// the system time. This function is susceptible to spurious wakeups.
273 /// Condition variables normally have a boolean predicate associated with
274 /// them, and the predicate must always be checked each time this function
275 /// returns to protect against spurious wakeups. Furthermore, since the timeout
276 /// is given relative to the moment this function is called, it needs to be adjusted
277 /// when this function is called in a loop. The [`wait_timeout_while`] method
278 /// lets you wait with a timeout while a predicate is true, taking care of all these concerns.
279 ///
280 /// The returned [`WaitTimeoutResult`] value indicates if the timeout is
281 /// known to have elapsed.
282 ///
283 /// Like [`wait`], the lock specified will be re-acquired when this function
284 /// returns, regardless of whether the timeout elapsed or not.
285 ///
286 /// [`wait`]: Self::wait
287 /// [`wait_timeout_while`]: Self::wait_timeout_while
288 ///
289 /// # Examples
290 ///
291 /// ```
292 /// use std::sync::{Arc, Mutex, Condvar};
293 /// use std::thread;
294 /// use std::time::Duration;
295 ///
296 /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
297 /// let pair2 = Arc::clone(&pair);
298 ///
299 /// thread::spawn(move || {
300 /// let (lock, cvar) = &*pair2;
301 /// let mut started = lock.lock().unwrap();
302 /// *started = true;
303 /// // We notify the condvar that the value has changed.
304 /// cvar.notify_one();
305 /// });
306 ///
307 /// // wait for the thread to start up
308 /// let (lock, cvar) = &*pair;
309 /// let mut started = lock.lock().unwrap();
310 /// // as long as the value inside the `Mutex<bool>` is `false`, we wait
311 /// loop {
312 /// let result = cvar.wait_timeout(started, Duration::from_millis(10)).unwrap();
313 /// // 10 milliseconds have passed, or maybe the value changed!
314 /// started = result.0;
315 /// if *started == true {
316 /// // We received the notification and the value has been updated, we can leave.
317 /// break
318 /// }
319 /// }
320 /// ```
321 #[stable(feature = "wait_timeout", since = "1.5.0")]
322 #[rustc_should_not_be_called_on_const_items]
323 pub fn wait_timeout<'a, T>(
324 &self,
325 guard: MutexGuard<'a, T>,
326 dur: Duration,
327 ) -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> {
328 let (poisoned, result) = unsafe {
329 let lock = mutex::guard_lock(&guard);
330 let success = self.inner.wait_timeout(lock, dur);
331 (mutex::guard_poison(&guard).get(), WaitTimeoutResult(!success))
332 };
333 if poisoned { Err(PoisonError::new((guard, result))) } else { Ok((guard, result)) }
334 }
335
336 /// Waits on this condition variable for a notification, timing out after a
337 /// specified duration.
338 ///
339 /// The semantics of this function are equivalent to [`wait_while`] except
340 /// that the thread will be blocked for roughly no longer than `dur`. This
341 /// method should not be used for precise timing due to anomalies such as
342 /// preemption or platform differences that might not cause the maximum
343 /// amount of time waited to be precisely `dur`.
344 ///
345 /// Note that the best effort is made to ensure that the time waited is
346 /// measured with a monotonic clock, and not affected by the changes made to
347 /// the system time.
348 ///
349 /// The returned [`WaitTimeoutResult`] value indicates if the timeout is
350 /// known to have elapsed without the condition being met.
351 ///
352 /// Like [`wait_while`], the lock specified will be re-acquired when this
353 /// function returns, regardless of whether the timeout elapsed or not.
354 ///
355 /// [`wait_while`]: Self::wait_while
356 /// [`wait_timeout`]: Self::wait_timeout
357 ///
358 /// # Examples
359 ///
360 /// ```
361 /// use std::sync::{Arc, Mutex, Condvar};
362 /// use std::thread;
363 /// use std::time::Duration;
364 ///
365 /// let pair = Arc::new((Mutex::new(true), Condvar::new()));
366 /// let pair2 = Arc::clone(&pair);
367 ///
368 /// thread::spawn(move || {
369 /// let (lock, cvar) = &*pair2;
370 /// let mut pending = lock.lock().unwrap();
371 /// *pending = false;
372 /// // We notify the condvar that the value has changed.
373 /// cvar.notify_one();
374 /// });
375 ///
376 /// // wait for the thread to start up
377 /// let (lock, cvar) = &*pair;
378 /// let result = cvar.wait_timeout_while(
379 /// lock.lock().unwrap(),
380 /// Duration::from_millis(100),
381 /// |&mut pending| pending,
382 /// ).unwrap();
383 /// if result.1.timed_out() {
384 /// // timed-out without the condition ever evaluating to false.
385 /// }
386 /// // access the locked mutex via result.0
387 /// ```
388 #[stable(feature = "wait_timeout_until", since = "1.42.0")]
389 #[rustc_should_not_be_called_on_const_items]
390 pub fn wait_timeout_while<'a, T, F>(
391 &self,
392 mut guard: MutexGuard<'a, T>,
393 dur: Duration,
394 mut condition: F,
395 ) -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)>
396 where
397 F: FnMut(&mut T) -> bool,
398 {
399 let start = Instant::now();
400 loop {
401 if !condition(&mut *guard) {
402 return Ok((guard, WaitTimeoutResult(false)));
403 }
404 let timeout = match dur.checked_sub(start.elapsed()) {
405 Some(timeout) => timeout,
406 None => return Ok((guard, WaitTimeoutResult(true))),
407 };
408 guard = self.wait_timeout(guard, timeout)?.0;
409 }
410 }
411
412 /// Wakes up one blocked thread on this condvar.
413 ///
414 /// If there is a blocked thread on this condition variable, then it will
415 /// be woken up from its call to [`wait`] or [`wait_timeout`]. Calls to
416 /// `notify_one` are not buffered in any way.
417 ///
418 /// To wake up all threads, see [`notify_all`].
419 ///
420 /// [`wait`]: Self::wait
421 /// [`wait_timeout`]: Self::wait_timeout
422 /// [`notify_all`]: Self::notify_all
423 ///
424 /// # Examples
425 ///
426 /// ```
427 /// use std::sync::{Arc, Mutex, Condvar};
428 /// use std::thread;
429 ///
430 /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
431 /// let pair2 = Arc::clone(&pair);
432 ///
433 /// thread::spawn(move || {
434 /// let (lock, cvar) = &*pair2;
435 /// let mut started = lock.lock().unwrap();
436 /// *started = true;
437 /// // We notify the condvar that the value has changed.
438 /// cvar.notify_one();
439 /// });
440 ///
441 /// // Wait for the thread to start up.
442 /// let (lock, cvar) = &*pair;
443 /// let mut started = lock.lock().unwrap();
444 /// // As long as the value inside the `Mutex<bool>` is `false`, we wait.
445 /// while !*started {
446 /// started = cvar.wait(started).unwrap();
447 /// }
448 /// ```
449 #[stable(feature = "rust1", since = "1.0.0")]
450 #[rustc_should_not_be_called_on_const_items]
451 pub fn notify_one(&self) {
452 self.inner.notify_one()
453 }
454
455 /// Wakes up all blocked threads on this condvar.
456 ///
457 /// This method will ensure that any current waiters on the condition
458 /// variable are awoken. Calls to `notify_all()` are not buffered in any
459 /// way.
460 ///
461 /// To wake up only one thread, see [`notify_one`].
462 ///
463 /// [`notify_one`]: Self::notify_one
464 ///
465 /// # Examples
466 ///
467 /// ```
468 /// use std::sync::{Arc, Mutex, Condvar};
469 /// use std::thread;
470 ///
471 /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
472 /// let pair2 = Arc::clone(&pair);
473 ///
474 /// thread::spawn(move || {
475 /// let (lock, cvar) = &*pair2;
476 /// let mut started = lock.lock().unwrap();
477 /// *started = true;
478 /// // We notify the condvar that the value has changed.
479 /// cvar.notify_all();
480 /// });
481 ///
482 /// // Wait for the thread to start up.
483 /// let (lock, cvar) = &*pair;
484 /// let mut started = lock.lock().unwrap();
485 /// // As long as the value inside the `Mutex<bool>` is `false`, we wait.
486 /// while !*started {
487 /// started = cvar.wait(started).unwrap();
488 /// }
489 /// ```
490 #[stable(feature = "rust1", since = "1.0.0")]
491 #[rustc_should_not_be_called_on_const_items]
492 pub fn notify_all(&self) {
493 self.inner.notify_all()
494 }
495}
496
497#[stable(feature = "std_debug", since = "1.16.0")]
498impl fmt::Debug for Condvar {
499 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
500 f.debug_struct("Condvar").finish_non_exhaustive()
501 }
502}
503
504#[stable(feature = "condvar_default", since = "1.10.0")]
505impl Default for Condvar {
506 /// Creates a `Condvar` which is ready to be waited on and notified.
507 fn default() -> Condvar {
508 Condvar::new()
509 }
510}