]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/sync.rs
Rewrite Condvar::wait_timeout and make it public
[rust.git] / src / libstd / sys / unix / sync.rs
1 // Copyright 2014-2015 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 #![allow(bad_style)]
12
13 use libc;
14
15 pub use self::os::{PTHREAD_MUTEX_INITIALIZER, pthread_mutex_t};
16 pub use self::os::{PTHREAD_COND_INITIALIZER, pthread_cond_t};
17 pub use self::os::{PTHREAD_RWLOCK_INITIALIZER, pthread_rwlock_t};
18
19 extern {
20     // mutexes
21     pub fn pthread_mutex_destroy(lock: *mut pthread_mutex_t) -> libc::c_int;
22     pub fn pthread_mutex_lock(lock: *mut pthread_mutex_t) -> libc::c_int;
23     pub fn pthread_mutex_trylock(lock: *mut pthread_mutex_t) -> libc::c_int;
24     pub fn pthread_mutex_unlock(lock: *mut pthread_mutex_t) -> libc::c_int;
25
26     // cvars
27     pub fn pthread_cond_wait(cond: *mut pthread_cond_t,
28                              lock: *mut pthread_mutex_t) -> libc::c_int;
29     pub fn pthread_cond_timedwait(cond: *mut pthread_cond_t,
30                               lock: *mut pthread_mutex_t,
31                               abstime: *const libc::timespec) -> libc::c_int;
32     pub fn pthread_cond_signal(cond: *mut pthread_cond_t) -> libc::c_int;
33     pub fn pthread_cond_broadcast(cond: *mut pthread_cond_t) -> libc::c_int;
34     pub fn pthread_cond_destroy(cond: *mut pthread_cond_t) -> libc::c_int;
35     pub fn gettimeofday(tp: *mut libc::timeval,
36                         tz: *mut libc::c_void) -> libc::c_int;
37
38     // rwlocks
39     pub fn pthread_rwlock_destroy(lock: *mut pthread_rwlock_t) -> libc::c_int;
40     pub fn pthread_rwlock_rdlock(lock: *mut pthread_rwlock_t) -> libc::c_int;
41     pub fn pthread_rwlock_tryrdlock(lock: *mut pthread_rwlock_t) -> libc::c_int;
42     pub fn pthread_rwlock_wrlock(lock: *mut pthread_rwlock_t) -> libc::c_int;
43     pub fn pthread_rwlock_trywrlock(lock: *mut pthread_rwlock_t) -> libc::c_int;
44     pub fn pthread_rwlock_unlock(lock: *mut pthread_rwlock_t) -> libc::c_int;
45 }
46
47 #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
48 mod os {
49     use libc;
50
51     pub type pthread_mutex_t = *mut libc::c_void;
52     pub type pthread_cond_t = *mut libc::c_void;
53     pub type pthread_rwlock_t = *mut libc::c_void;
54
55     pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = 0 as *mut _;
56     pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = 0 as *mut _;
57     pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = 0 as *mut _;
58 }
59
60 #[cfg(any(target_os = "macos", target_os = "ios"))]
61 mod os {
62     use libc;
63
64     #[cfg(any(target_arch = "x86_64",
65               target_arch = "aarch64"))]
66     const __PTHREAD_MUTEX_SIZE__: uint = 56;
67     #[cfg(any(target_arch = "x86",
68               target_arch = "arm"))]
69     const __PTHREAD_MUTEX_SIZE__: uint = 40;
70
71     #[cfg(any(target_arch = "x86_64",
72               target_arch = "aarch64"))]
73     const __PTHREAD_COND_SIZE__: uint = 40;
74     #[cfg(any(target_arch = "x86",
75               target_arch = "arm"))]
76     const __PTHREAD_COND_SIZE__: uint = 24;
77
78     #[cfg(any(target_arch = "x86_64",
79               target_arch = "aarch64"))]
80     const __PTHREAD_RWLOCK_SIZE__: uint = 192;
81     #[cfg(any(target_arch = "x86",
82               target_arch = "arm"))]
83     const __PTHREAD_RWLOCK_SIZE__: uint = 124;
84
85     const _PTHREAD_MUTEX_SIG_INIT: libc::c_long = 0x32AAABA7;
86     const _PTHREAD_COND_SIG_INIT: libc::c_long = 0x3CB0B1BB;
87     const _PTHREAD_RWLOCK_SIG_INIT: libc::c_long = 0x2DA8B3B4;
88
89     #[repr(C)]
90     pub struct pthread_mutex_t {
91         __sig: libc::c_long,
92         __opaque: [u8; __PTHREAD_MUTEX_SIZE__],
93     }
94     #[repr(C)]
95     pub struct pthread_cond_t {
96         __sig: libc::c_long,
97         __opaque: [u8; __PTHREAD_COND_SIZE__],
98     }
99     #[repr(C)]
100     pub struct pthread_rwlock_t {
101         __sig: libc::c_long,
102         __opaque: [u8; __PTHREAD_RWLOCK_SIZE__],
103     }
104
105     pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
106         __sig: _PTHREAD_MUTEX_SIG_INIT,
107         __opaque: [0; __PTHREAD_MUTEX_SIZE__],
108     };
109     pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
110         __sig: _PTHREAD_COND_SIG_INIT,
111         __opaque: [0; __PTHREAD_COND_SIZE__],
112     };
113     pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
114         __sig: _PTHREAD_RWLOCK_SIG_INIT,
115         __opaque: [0; __PTHREAD_RWLOCK_SIZE__],
116     };
117 }
118
119 #[cfg(target_os = "linux")]
120 mod os {
121     use libc;
122
123     // minus 8 because we have an 'align' field
124     #[cfg(target_arch = "x86_64")]
125     const __SIZEOF_PTHREAD_MUTEX_T: uint = 40 - 8;
126     #[cfg(any(target_arch = "x86",
127               target_arch = "arm",
128               target_arch = "mips",
129               target_arch = "mipsel"))]
130     const __SIZEOF_PTHREAD_MUTEX_T: uint = 24 - 8;
131     #[cfg(target_arch = "aarch64")]
132     const __SIZEOF_PTHREAD_MUTEX_T: uint = 48 - 8;
133
134     #[cfg(any(target_arch = "x86_64",
135               target_arch = "x86",
136               target_arch = "arm",
137               target_arch = "aarch64",
138               target_arch = "mips",
139               target_arch = "mipsel"))]
140     const __SIZEOF_PTHREAD_COND_T: uint = 48 - 8;
141
142     #[cfg(any(target_arch = "x86_64",
143               target_arch = "aarch64"))]
144     const __SIZEOF_PTHREAD_RWLOCK_T: uint = 56 - 8;
145
146     #[cfg(any(target_arch = "x86",
147               target_arch = "arm",
148               target_arch = "mips",
149               target_arch = "mipsel"))]
150     const __SIZEOF_PTHREAD_RWLOCK_T: uint = 32 - 8;
151
152     #[repr(C)]
153     pub struct pthread_mutex_t {
154         __align: libc::c_longlong,
155         size: [u8; __SIZEOF_PTHREAD_MUTEX_T],
156     }
157     #[repr(C)]
158     pub struct pthread_cond_t {
159         __align: libc::c_longlong,
160         size: [u8; __SIZEOF_PTHREAD_COND_T],
161     }
162     #[repr(C)]
163     pub struct pthread_rwlock_t {
164         __align: libc::c_longlong,
165         size: [u8; __SIZEOF_PTHREAD_RWLOCK_T],
166     }
167
168     pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
169         __align: 0,
170         size: [0; __SIZEOF_PTHREAD_MUTEX_T],
171     };
172     pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
173         __align: 0,
174         size: [0; __SIZEOF_PTHREAD_COND_T],
175     };
176     pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
177         __align: 0,
178         size: [0; __SIZEOF_PTHREAD_RWLOCK_T],
179     };
180 }
181 #[cfg(target_os = "android")]
182 mod os {
183     use libc;
184
185     #[repr(C)]
186     pub struct pthread_mutex_t { value: libc::c_int }
187     #[repr(C)]
188     pub struct pthread_cond_t { value: libc::c_int }
189     #[repr(C)]
190     pub struct pthread_rwlock_t {
191         lock: pthread_mutex_t,
192         cond: pthread_cond_t,
193         numLocks: libc::c_int,
194         writerThreadId: libc::c_int,
195         pendingReaders: libc::c_int,
196         pendingWriters: libc::c_int,
197         reserved: [*mut libc::c_void; 4],
198     }
199
200     pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
201         value: 0,
202     };
203     pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
204         value: 0,
205     };
206     pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
207         lock: PTHREAD_MUTEX_INITIALIZER,
208         cond: PTHREAD_COND_INITIALIZER,
209         numLocks: 0,
210         writerThreadId: 0,
211         pendingReaders: 0,
212         pendingWriters: 0,
213         reserved: [0 as *mut _; 4],
214     };
215 }