]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/sync.rs
41e1e206a423a7c43a0869c6ec642a5aedf9d1e9
[rustc.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_RECURSIVE, pthread_mutex_t,
16 pthread_mutexattr_t};
17 pub use self::os::{PTHREAD_COND_INITIALIZER, pthread_cond_t};
18 pub use self::os::{PTHREAD_RWLOCK_INITIALIZER, pthread_rwlock_t};
19
20 extern {
21 // mutexes
22 pub fn pthread_mutex_init(lock: *mut pthread_mutex_t, attr: *const pthread_mutexattr_t)
23 -> libc::c_int;
24 pub fn pthread_mutex_destroy(lock: *mut pthread_mutex_t) -> libc::c_int;
25 pub fn pthread_mutex_lock(lock: *mut pthread_mutex_t) -> libc::c_int;
26 pub fn pthread_mutex_trylock(lock: *mut pthread_mutex_t) -> libc::c_int;
27 pub fn pthread_mutex_unlock(lock: *mut pthread_mutex_t) -> libc::c_int;
28
29 pub fn pthread_mutexattr_init(attr: *mut pthread_mutexattr_t) -> libc::c_int;
30 pub fn pthread_mutexattr_destroy(attr: *mut pthread_mutexattr_t) -> libc::c_int;
31 pub fn pthread_mutexattr_settype(attr: *mut pthread_mutexattr_t, _type: libc::c_int)
32 -> libc::c_int;
33
34 // cvars
35 pub fn pthread_cond_wait(cond: *mut pthread_cond_t,
36 lock: *mut pthread_mutex_t) -> libc::c_int;
37 pub fn pthread_cond_timedwait(cond: *mut pthread_cond_t,
38 lock: *mut pthread_mutex_t,
39 abstime: *const libc::timespec) -> libc::c_int;
40 pub fn pthread_cond_signal(cond: *mut pthread_cond_t) -> libc::c_int;
41 pub fn pthread_cond_broadcast(cond: *mut pthread_cond_t) -> libc::c_int;
42 pub fn pthread_cond_destroy(cond: *mut pthread_cond_t) -> libc::c_int;
43 pub fn gettimeofday(tp: *mut libc::timeval,
44 tz: *mut libc::c_void) -> libc::c_int;
45
46 // rwlocks
47 pub fn pthread_rwlock_destroy(lock: *mut pthread_rwlock_t) -> libc::c_int;
48 pub fn pthread_rwlock_rdlock(lock: *mut pthread_rwlock_t) -> libc::c_int;
49 pub fn pthread_rwlock_tryrdlock(lock: *mut pthread_rwlock_t) -> libc::c_int;
50 pub fn pthread_rwlock_wrlock(lock: *mut pthread_rwlock_t) -> libc::c_int;
51 pub fn pthread_rwlock_trywrlock(lock: *mut pthread_rwlock_t) -> libc::c_int;
52 pub fn pthread_rwlock_unlock(lock: *mut pthread_rwlock_t) -> libc::c_int;
53 }
54
55 #[cfg(any(target_os = "freebsd",
56 target_os = "dragonfly",
57 target_os = "bitrig",
58 target_os = "openbsd"))]
59 mod os {
60 use libc;
61
62 pub type pthread_mutex_t = *mut libc::c_void;
63 pub type pthread_mutexattr_t = *mut libc::c_void;
64 pub type pthread_cond_t = *mut libc::c_void;
65 pub type pthread_rwlock_t = *mut libc::c_void;
66
67 pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = 0 as *mut _;
68 pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = 0 as *mut _;
69 pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = 0 as *mut _;
70 pub const PTHREAD_MUTEX_RECURSIVE: libc::c_int = 2;
71 }
72
73 #[cfg(any(target_os = "macos", target_os = "ios"))]
74 mod os {
75 use libc;
76
77 #[cfg(any(target_arch = "x86_64",
78 target_arch = "aarch64"))]
79 const __PTHREAD_MUTEX_SIZE__: usize = 56;
80 #[cfg(any(target_arch = "x86",
81 target_arch = "arm"))]
82 const __PTHREAD_MUTEX_SIZE__: usize = 40;
83
84 #[cfg(any(target_arch = "x86_64",
85 target_arch = "aarch64"))]
86 const __PTHREAD_COND_SIZE__: usize = 40;
87 #[cfg(any(target_arch = "x86",
88 target_arch = "arm"))]
89 const __PTHREAD_COND_SIZE__: usize = 24;
90
91 #[cfg(any(target_arch = "x86_64",
92 target_arch = "aarch64"))]
93 const __PTHREAD_RWLOCK_SIZE__: usize = 192;
94 #[cfg(any(target_arch = "x86",
95 target_arch = "arm"))]
96 const __PTHREAD_RWLOCK_SIZE__: usize = 124;
97
98 const _PTHREAD_MUTEX_SIG_INIT: libc::c_long = 0x32AAABA7;
99 const _PTHREAD_COND_SIG_INIT: libc::c_long = 0x3CB0B1BB;
100 const _PTHREAD_RWLOCK_SIG_INIT: libc::c_long = 0x2DA8B3B4;
101
102 #[repr(C)]
103 pub struct pthread_mutex_t {
104 __sig: libc::c_long,
105 __opaque: [u8; __PTHREAD_MUTEX_SIZE__],
106 }
107 #[repr(C)]
108 pub struct pthread_mutexattr_t {
109 __sig: libc::c_long,
110 // note, that this is 16 bytes just to be safe, the actual struct might be smaller.
111 __opaque: [u8; 16],
112 }
113 #[repr(C)]
114 pub struct pthread_cond_t {
115 __sig: libc::c_long,
116 __opaque: [u8; __PTHREAD_COND_SIZE__],
117 }
118 #[repr(C)]
119 pub struct pthread_rwlock_t {
120 __sig: libc::c_long,
121 __opaque: [u8; __PTHREAD_RWLOCK_SIZE__],
122 }
123
124 pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
125 __sig: _PTHREAD_MUTEX_SIG_INIT,
126 __opaque: [0; __PTHREAD_MUTEX_SIZE__],
127 };
128 pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
129 __sig: _PTHREAD_COND_SIG_INIT,
130 __opaque: [0; __PTHREAD_COND_SIZE__],
131 };
132 pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
133 __sig: _PTHREAD_RWLOCK_SIG_INIT,
134 __opaque: [0; __PTHREAD_RWLOCK_SIZE__],
135 };
136
137 pub const PTHREAD_MUTEX_RECURSIVE: libc::c_int = 2;
138 }
139
140 #[cfg(target_os = "linux")]
141 mod os {
142 use libc;
143
144 // minus 8 because we have an 'align' field
145 #[cfg(target_arch = "x86_64")]
146 const __SIZEOF_PTHREAD_MUTEX_T: usize = 40 - 8;
147 #[cfg(any(target_arch = "x86",
148 target_arch = "arm",
149 target_arch = "mips",
150 target_arch = "mipsel",
151 target_arch = "powerpc"))]
152 const __SIZEOF_PTHREAD_MUTEX_T: usize = 24 - 8;
153 #[cfg(target_arch = "aarch64")]
154 const __SIZEOF_PTHREAD_MUTEX_T: usize = 48 - 8;
155
156 #[cfg(any(target_arch = "x86_64",
157 target_arch = "x86",
158 target_arch = "arm",
159 target_arch = "aarch64",
160 target_arch = "mips",
161 target_arch = "mipsel",
162 target_arch = "powerpc"))]
163 const __SIZEOF_PTHREAD_COND_T: usize = 48 - 8;
164
165 #[cfg(any(target_arch = "x86_64",
166 target_arch = "aarch64"))]
167 const __SIZEOF_PTHREAD_RWLOCK_T: usize = 56 - 8;
168
169 #[cfg(any(target_arch = "x86",
170 target_arch = "arm",
171 target_arch = "mips",
172 target_arch = "mipsel",
173 target_arch = "powerpc"))]
174 const __SIZEOF_PTHREAD_RWLOCK_T: usize = 32 - 8;
175
176 #[repr(C)]
177 pub struct pthread_mutex_t {
178 __align: libc::c_longlong,
179 size: [u8; __SIZEOF_PTHREAD_MUTEX_T],
180 }
181 #[repr(C)]
182 pub struct pthread_mutexattr_t {
183 __align: libc::c_longlong,
184 // note, that this is 16 bytes just to be safe, the actual struct might be smaller.
185 size: [u8; 16],
186 }
187 #[repr(C)]
188 pub struct pthread_cond_t {
189 __align: libc::c_longlong,
190 size: [u8; __SIZEOF_PTHREAD_COND_T],
191 }
192 #[repr(C)]
193 pub struct pthread_rwlock_t {
194 __align: libc::c_longlong,
195 size: [u8; __SIZEOF_PTHREAD_RWLOCK_T],
196 }
197
198 pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
199 __align: 0,
200 size: [0; __SIZEOF_PTHREAD_MUTEX_T],
201 };
202 pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
203 __align: 0,
204 size: [0; __SIZEOF_PTHREAD_COND_T],
205 };
206 pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
207 __align: 0,
208 size: [0; __SIZEOF_PTHREAD_RWLOCK_T],
209 };
210 pub const PTHREAD_MUTEX_RECURSIVE: libc::c_int = 1;
211 }
212 #[cfg(target_os = "android")]
213 mod os {
214 use libc;
215
216 #[repr(C)]
217 pub struct pthread_mutex_t { value: libc::c_int }
218 pub type pthread_mutexattr_t = libc::c_long;
219 #[repr(C)]
220 pub struct pthread_cond_t { value: libc::c_int }
221 #[repr(C)]
222 pub struct pthread_rwlock_t {
223 lock: pthread_mutex_t,
224 cond: pthread_cond_t,
225 numLocks: libc::c_int,
226 writerThreadId: libc::c_int,
227 pendingReaders: libc::c_int,
228 pendingWriters: libc::c_int,
229 reserved: [*mut libc::c_void; 4],
230 }
231
232 pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
233 value: 0,
234 };
235 pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
236 value: 0,
237 };
238 pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
239 lock: PTHREAD_MUTEX_INITIALIZER,
240 cond: PTHREAD_COND_INITIALIZER,
241 numLocks: 0,
242 writerThreadId: 0,
243 pendingReaders: 0,
244 pendingWriters: 0,
245 reserved: [0 as *mut _; 4],
246 };
247 pub const PTHREAD_MUTEX_RECURSIVE: libc::c_int = 1;
248 }