1use std::fmt;
6use std::fmt::Debug;
7use std::sync::Condvar as StdCondvar;
8use std::sync::MutexGuard;
9use std::sync::WaitTimeoutResult;
10use std::time::Duration;
11
12static CONDVAR_POISONED: &str = "condvar is poisoned";
13
14#[derive(Default)]
16pub struct Condvar {
17 std: StdCondvar,
18}
19
20impl Condvar {
21 pub const fn new() -> Condvar {
23 Condvar {
24 std: StdCondvar::new(),
25 }
26 }
27
28 pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
30 self.std.wait(guard).expect(CONDVAR_POISONED)
31 }
32
33 pub fn wait_while<'a, T, F>(&self, guard: MutexGuard<'a, T>, condition: F) -> MutexGuard<'a, T>
36 where
37 F: FnMut(&mut T) -> bool,
38 {
39 self.std
40 .wait_while(guard, condition)
41 .expect(CONDVAR_POISONED)
42 }
43
44 pub fn wait_timeout<'a, T>(
47 &self,
48 guard: MutexGuard<'a, T>,
49 dur: Duration,
50 ) -> (MutexGuard<'a, T>, WaitTimeoutResult) {
51 self.std.wait_timeout(guard, dur).expect(CONDVAR_POISONED)
52 }
53
54 pub fn wait_timeout_while<'a, T, F>(
56 &self,
57 guard: MutexGuard<'a, T>,
58 dur: Duration,
59 condition: F,
60 ) -> (MutexGuard<'a, T>, WaitTimeoutResult)
61 where
62 F: FnMut(&mut T) -> bool,
63 {
64 self.std
65 .wait_timeout_while(guard, dur, condition)
66 .expect(CONDVAR_POISONED)
67 }
68
69 pub fn notify_one(&self) {
71 self.std.notify_one();
72 }
73
74 pub fn notify_all(&self) {
76 self.std.notify_all();
77 }
78}
79
80impl Debug for Condvar {
81 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
82 Debug::fmt(&self.std, formatter)
83 }
84}