]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/sync.rs
rollup merge of #21151: brson/beta
[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               target_arch = "powerpc"))]
131     const __SIZEOF_PTHREAD_MUTEX_T: uint = 24 - 8;
132     #[cfg(target_arch = "aarch64")]
133     const __SIZEOF_PTHREAD_MUTEX_T: uint = 48 - 8;
134
135     #[cfg(any(target_arch = "x86_64",
136               target_arch = "x86",
137               target_arch = "arm",
138               target_arch = "aarch64",
139               target_arch = "mips",
140               target_arch = "mipsel",
141               target_arch = "powerpc"))]
142     const __SIZEOF_PTHREAD_COND_T: uint = 48 - 8;
143
144     #[cfg(any(target_arch = "x86_64",
145               target_arch = "aarch64"))]
146     const __SIZEOF_PTHREAD_RWLOCK_T: uint = 56 - 8;
147
148     #[cfg(any(target_arch = "x86",
149               target_arch = "arm",
150               target_arch = "mips",
151               target_arch = "mipsel",
152               target_arch = "powerpc"))]
153     const __SIZEOF_PTHREAD_RWLOCK_T: uint = 32 - 8;
154
155     #[repr(C)]
156     pub struct pthread_mutex_t {
157         __align: libc::c_longlong,
158         size: [u8; __SIZEOF_PTHREAD_MUTEX_T],
159     }
160     #[repr(C)]
161     pub struct pthread_cond_t {
162         __align: libc::c_longlong,
163         size: [u8; __SIZEOF_PTHREAD_COND_T],
164     }
165     #[repr(C)]
166     pub struct pthread_rwlock_t {
167         __align: libc::c_longlong,
168         size: [u8; __SIZEOF_PTHREAD_RWLOCK_T],
169     }
170
171     pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
172         __align: 0,
173         size: [0; __SIZEOF_PTHREAD_MUTEX_T],
174     };
175     pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
176         __align: 0,
177         size: [0; __SIZEOF_PTHREAD_COND_T],
178     };
179     pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
180         __align: 0,
181         size: [0; __SIZEOF_PTHREAD_RWLOCK_T],
182     };
183 }
184 #[cfg(target_os = "android")]
185 mod os {
186     use libc;
187
188     #[repr(C)]
189     pub struct pthread_mutex_t { value: libc::c_int }
190     #[repr(C)]
191     pub struct pthread_cond_t { value: libc::c_int }
192     #[repr(C)]
193     pub struct pthread_rwlock_t {
194         lock: pthread_mutex_t,
195         cond: pthread_cond_t,
196         numLocks: libc::c_int,
197         writerThreadId: libc::c_int,
198         pendingReaders: libc::c_int,
199         pendingWriters: libc::c_int,
200         reserved: [*mut libc::c_void; 4],
201     }
202
203     pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
204         value: 0,
205     };
206     pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
207         value: 0,
208     };
209     pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
210         lock: PTHREAD_MUTEX_INITIALIZER,
211         cond: PTHREAD_COND_INITIALIZER,
212         numLocks: 0,
213         writerThreadId: 0,
214         pendingReaders: 0,
215         pendingWriters: 0,
216         reserved: [0 as *mut _; 4],
217     };
218 }