]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/condvar.rs
071637e3a939f82eda497d497dabdbef9e72ea31
[rust.git] / src / libstd / sys / windows / condvar.rs
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
11 use prelude::v1::*;
12
13 use cell::UnsafeCell;
14 use libc::{self, DWORD};
15 use os;
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
22 unsafe impl Send for Condvar {}
23 unsafe impl Sync for Condvar {}
24
25 pub const CONDVAR_INIT: Condvar = Condvar {
26     inner: UnsafeCell { value: ffi::CONDITION_VARIABLE_INIT }
27 };
28
29 impl Condvar {
30     #[inline]
31     pub unsafe fn new() -> Condvar { CONDVAR_INIT }
32
33     #[inline]
34     pub unsafe fn wait(&self, mutex: &Mutex) {
35         let r = ffi::SleepConditionVariableSRW(self.inner.get(),
36                                                mutex::raw(mutex),
37                                                libc::INFINITE,
38                                                0);
39         debug_assert!(r != 0);
40     }
41
42     pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
43         let r = ffi::SleepConditionVariableSRW(self.inner.get(),
44                                                mutex::raw(mutex),
45                                                dur.num_milliseconds() as DWORD,
46                                                0);
47         if r == 0 {
48             const ERROR_TIMEOUT: DWORD = 0x5B4;
49             debug_assert_eq!(os::errno() as uint, ERROR_TIMEOUT as uint);
50             false
51         } else {
52             true
53         }
54     }
55
56     #[inline]
57     pub unsafe fn notify_one(&self) {
58         ffi::WakeConditionVariable(self.inner.get())
59     }
60
61     #[inline]
62     pub unsafe fn notify_all(&self) {
63         ffi::WakeAllConditionVariable(self.inner.get())
64     }
65
66     pub unsafe fn destroy(&self) {
67         // ...
68     }
69 }