1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! compatible with std::sync::condvar except for both thread and coroutine
//! please ref the doc from std::sync::condvar
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, LockResult, PoisonError};
use std::time::Duration;
use crate::cancel::trigger_cancel_panic;
use crate::park::ParkError;
use crate::std::queue::seg_queue::SegQueue;
use super::blocking::SyncBlocker;
use super::mutex::{self, Mutex, MutexGuard};
/// A type indicating whether a timed wait on a condition variable returned
/// due to a time out or not.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct WaitTimeoutResult(bool);
impl WaitTimeoutResult {
/// Returns whether the wait was known to have timed out.
pub fn timed_out(&self) -> bool {
self.0
}
}
pub struct Condvar {
// the waiting blocker list
to_wake: SegQueue<Arc<SyncBlocker>>,
// used to verify the same mutex instance
mutex: AtomicUsize,
}
impl Condvar {
pub fn new() -> Condvar {
Condvar {
to_wake: SegQueue::new(),
mutex: AtomicUsize::new(0),
}
}
// return false if timeout happened
pub fn wait_impl<T>(&self, lock: &Mutex<T>, dur: Option<Duration>) -> Result<(), ParkError> {
let cancel = if crate::coroutine_impl::is_coroutine() {
Some(crate::coroutine_impl::current_cancel_data())
} else {
None
};
// enqueue the blocker
let cur = SyncBlocker::current();
// we can't cancel panic here!!
if let Some(c) = cancel.as_ref() {
c.disable_cancel();
}
self.to_wake.push(cur.clone());
// unlock the mutex to let other continue
mutex::unlock_mutex(lock);
if let Some(c) = cancel.as_ref() {
c.enable_cancel();
}
// wait until coming back
let ret = cur.park(dur);
// disable cancel panic
if let Some(c) = cancel.as_ref() {
c.disable_cancel();
}
// don't run the guard destructor
::std::mem::forget(lock.lock());
if ret.is_err() {
// when in a cancel state, could cause problem for the lock
// make notify never panic by disable the cancel bit
// check the unpark status
if cur.is_unparked() {
let _ = self.notify_one();
} else {
// register
cur.set_release();
// re-check unpark status
if cur.is_unparked() && cur.take_release() {
let _ = self.notify_one();
}
}
}
// drop the parker here without panic!
// it may paniced in the drop of Park
drop(cur);
// enable cancel panic
if let Some(c) = cancel.as_ref() {
c.enable_cancel();
}
ret
}
pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
let poisoned = {
let lock = mutex::guard_lock(&guard);
self.verify(lock as *const _ as usize);
let ret = self.wait_impl(lock, None);
if ret == Err(ParkError::Canceled) {
// don't set the poison flag
::std::mem::forget(guard);
// release the mutex to let other run
mutex::unlock_mutex(lock);
// now we can safely go with the cancel panic
trigger_cancel_panic();
}
mutex::guard_poison(&guard).get()
};
if poisoned {
Err(PoisonError::new(guard))
} else {
Ok(guard)
}
}
pub fn wait_timeout<'a, T>(
&self,
guard: MutexGuard<'a, T>,
dur: Duration,
) -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> {
let (poisoned, result) = {
let lock = mutex::guard_lock(&guard);
self.verify(lock as *const _ as usize);
let ret = self.wait_impl(lock, Some(dur));
if ret == Err(ParkError::Canceled) {
// don't set the poison flag
::std::mem::forget(guard);
// release the mutex to let other run
mutex::unlock_mutex(lock);
// now we can safely go with the cancel panic
trigger_cancel_panic();
}
(
mutex::guard_poison(&guard).get(),
WaitTimeoutResult(ret.is_err()),
)
};
if poisoned {
Err(PoisonError::new((guard, result)))
} else {
Ok((guard, result))
}
}
pub fn notify_one(&self) -> Result<(), ParkError> {
// NOTICE: the following code would not drop the lock!
let w = self.to_wake.pop();
if let Some(w) = w {
w.unpark()?;
if w.take_release() {
self.notify_one()?;
}
}
Ok(())
}
pub fn notify_all(&self) -> Result<(), ParkError> {
while let Some(w) = self.to_wake.pop() {
w.unpark()?;
}
Ok(())
}
fn verify(&self, addr: usize) {
match self
.mutex
.compare_exchange(0, addr, Ordering::SeqCst, Ordering::SeqCst)
{
// If we got out 0, then we have successfully bound the mutex to
// this condvar.
Ok(0) => {}
// If we get out a value that's the same as `addr`, then someone
// already beat us to the punch.
Err(n) if n == addr => {}
// Anything else and we're using more than one mutex on this condvar,
// which is currently disallowed.
_ => panic!("attempted to use a condition variable with two mutex"),
}
}
}
impl Default for Condvar {
fn default() -> Condvar {
Condvar::new()
}
}
#[cfg(test)]
mod tests {
#![feature(test)]
use crate::std::sync::channel::channel;
use crate::std::sync::{Condvar, Mutex};
use std::sync::mpsc::TryRecvError;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use std::u32;
#[test]
fn smoke() {
let c = Condvar::new();
c.notify_one();
c.notify_all();
}
#[test]
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]
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();
assert_eq!(*cnt, N);
*cnt = 0;
cond.notify_all();
drop(cnt);
for _ in 0..N {
rx.recv().unwrap();
}
}
#[test]
fn wait_timeout() {
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 (g, _no_timeout) = c.wait_timeout(g, Duration::from_millis(1)).unwrap();
// spurious wakeups mean this isn't necessarily true
// assert!(!no_timeout);
let _t = thread::spawn(move || {
let _g = m2.lock().unwrap();
c2.notify_one();
});
let (g, timeout_res) = c
.wait_timeout(g, Duration::from_millis(u32::MAX as u64))
.unwrap();
assert!(!timeout_res.timed_out());
drop(g);
}
#[test]
#[should_panic]
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();
}
#[test]
fn test_condvar_canceled() {
const N: usize = 10;
let data = Arc::new((Mutex::new(0), Condvar::new()));
let (tx, rx) = channel();
let mut vec = vec![];
for _ in 0..N {
let data = data.clone();
let tx = tx.clone();
let h = co!(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();
});
vec.push(h);
}
drop(tx);
let &(ref lock, ref cond) = &*data;
rx.recv().unwrap();
let mut cnt = lock.lock().unwrap();
assert_eq!(*cnt, N);
*cnt = 0;
drop(cnt);
assert_eq!(rx.try_recv(), Err(TryRecvError::Empty));
const M: usize = 3;
// we cancel some of the coroutine
for i in 0..M {
unsafe { vec[i].coroutine().cancel() };
}
// note that cancel and notify has contention here
// which could cause some coroutine can't get any signal
// if the cancelled coroutine unparked successfully
// make sure every coroutine get the correct signal
// crate::coroutine::sleep(::std::time::Duration::from_millis(1));
// or use `notify_all()` to make sure all the coroutines
// get a signal
cond.notify_all();
for _ in 0..N - M {
// cond.notify_one();
rx.recv().unwrap();
}
for h in vec {
h.join().ok();
}
// for most cases this assertion is correct
// rarely try_recv would return Ok(())
// assert_eq!(rx.try_recv(), Err(TryRecvError::Disconnected));
}
}