]>
Commit | Line | Data |
---|---|---|
532ac7d7 XL |
1 | use crate::cell::UnsafeCell; |
2 | use crate::sys::c; | |
5e7ed085 | 3 | use crate::sys::locks::{mutex, Mutex}; |
532ac7d7 XL |
4 | use crate::sys::os; |
5 | use crate::time::Duration; | |
1a4d82fc | 6 | |
dfeec247 XL |
7 | pub struct Condvar { |
8 | inner: UnsafeCell<c::CONDITION_VARIABLE>, | |
9 | } | |
1a4d82fc | 10 | |
29967ef6 XL |
11 | pub type MovableCondvar = Condvar; |
12 | ||
c34b1796 AL |
13 | unsafe impl Send for Condvar {} |
14 | unsafe impl Sync for Condvar {} | |
15 | ||
1a4d82fc | 16 | impl Condvar { |
923072b8 | 17 | #[inline] |
62682a34 | 18 | pub const fn new() -> Condvar { |
c1a9b12d | 19 | Condvar { inner: UnsafeCell::new(c::CONDITION_VARIABLE_INIT) } |
62682a34 | 20 | } |
1a4d82fc JJ |
21 | |
22 | #[inline] | |
23 | pub unsafe fn wait(&self, mutex: &Mutex) { | |
dfeec247 | 24 | let r = c::SleepConditionVariableSRW(self.inner.get(), mutex::raw(mutex), c::INFINITE, 0); |
1a4d82fc JJ |
25 | debug_assert!(r != 0); |
26 | } | |
27 | ||
28 | pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool { | |
dfeec247 XL |
29 | let r = c::SleepConditionVariableSRW( |
30 | self.inner.get(), | |
31 | mutex::raw(mutex), | |
5e7ed085 | 32 | crate::sys::windows::dur2timeout(dur), |
dfeec247 XL |
33 | 0, |
34 | ); | |
1a4d82fc | 35 | if r == 0 { |
92a42be0 | 36 | debug_assert_eq!(os::errno() as usize, c::ERROR_TIMEOUT as usize); |
1a4d82fc JJ |
37 | false |
38 | } else { | |
39 | true | |
40 | } | |
41 | } | |
42 | ||
43 | #[inline] | |
44 | pub unsafe fn notify_one(&self) { | |
c1a9b12d | 45 | c::WakeConditionVariable(self.inner.get()) |
1a4d82fc JJ |
46 | } |
47 | ||
48 | #[inline] | |
49 | pub unsafe fn notify_all(&self) { | |
c1a9b12d | 50 | c::WakeAllConditionVariable(self.inner.get()) |
1a4d82fc | 51 | } |
1a4d82fc | 52 | } |