]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
c34b1796 AL |
11 | use prelude::v1::*; |
12 | ||
1a4d82fc JJ |
13 | use cell::UnsafeCell; |
14 | use libc::{self, DWORD}; | |
c34b1796 | 15 | use sys::os; |
1a4d82fc JJ |
16 | use sys::mutex::{self, Mutex}; |
17 | use sys::sync as ffi; | |
18 | use time::Duration; | |
19 | ||
20 | pub struct Condvar { inner: UnsafeCell<ffi::CONDITION_VARIABLE> } | |
21 | ||
c34b1796 AL |
22 | unsafe impl Send for Condvar {} |
23 | unsafe impl Sync for Condvar {} | |
24 | ||
1a4d82fc | 25 | impl Condvar { |
62682a34 SL |
26 | pub const fn new() -> Condvar { |
27 | Condvar { inner: UnsafeCell::new(ffi::CONDITION_VARIABLE_INIT) } | |
28 | } | |
1a4d82fc JJ |
29 | |
30 | #[inline] | |
31 | pub unsafe fn wait(&self, mutex: &Mutex) { | |
85aaf69f SL |
32 | let r = ffi::SleepConditionVariableSRW(self.inner.get(), |
33 | mutex::raw(mutex), | |
34 | libc::INFINITE, | |
35 | 0); | |
1a4d82fc JJ |
36 | debug_assert!(r != 0); |
37 | } | |
38 | ||
39 | pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool { | |
85aaf69f SL |
40 | let r = ffi::SleepConditionVariableSRW(self.inner.get(), |
41 | mutex::raw(mutex), | |
d9579d0f | 42 | super::dur2timeout(dur), |
85aaf69f | 43 | 0); |
1a4d82fc JJ |
44 | if r == 0 { |
45 | const ERROR_TIMEOUT: DWORD = 0x5B4; | |
c34b1796 | 46 | debug_assert_eq!(os::errno() as usize, ERROR_TIMEOUT as usize); |
1a4d82fc JJ |
47 | false |
48 | } else { | |
49 | true | |
50 | } | |
51 | } | |
52 | ||
53 | #[inline] | |
54 | pub unsafe fn notify_one(&self) { | |
55 | ffi::WakeConditionVariable(self.inner.get()) | |
56 | } | |
57 | ||
58 | #[inline] | |
59 | pub unsafe fn notify_all(&self) { | |
60 | ffi::WakeAllConditionVariable(self.inner.get()) | |
61 | } | |
62 | ||
63 | pub unsafe fn destroy(&self) { | |
64 | // ... | |
65 | } | |
66 | } |