]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/sync.rs
Imported Upstream version 1.3.0+dfsg1
[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 = "netbsd",
59 target_os = "openbsd"))]
60 mod os {
61 use libc;
62
63 pub type pthread_mutex_t = *mut libc::c_void;
64 pub type pthread_mutexattr_t = *mut libc::c_void;
65 pub type pthread_cond_t = *mut libc::c_void;
66 pub type pthread_rwlock_t = *mut libc::c_void;
67
68 pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = 0 as *mut _;
69 pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = 0 as *mut _;
70 pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = 0 as *mut _;
71 pub const PTHREAD_MUTEX_RECURSIVE: libc::c_int = 2;
72 }
73
74 #[cfg(any(target_os = "macos", target_os = "ios"))]
75 mod os {
76 use libc;
77
78 #[cfg(any(target_arch = "x86_64",
79 target_arch = "aarch64"))]
80 const __PTHREAD_MUTEX_SIZE__: usize = 56;
81 #[cfg(any(target_arch = "x86",
82 target_arch = "arm"))]
83 const __PTHREAD_MUTEX_SIZE__: usize = 40;
84
85 #[cfg(any(target_arch = "x86_64",
86 target_arch = "aarch64"))]
87 const __PTHREAD_COND_SIZE__: usize = 40;
88 #[cfg(any(target_arch = "x86",
89 target_arch = "arm"))]
90 const __PTHREAD_COND_SIZE__: usize = 24;
91
92 #[cfg(any(target_arch = "x86_64",
93 target_arch = "aarch64"))]
94 const __PTHREAD_RWLOCK_SIZE__: usize = 192;
95 #[cfg(any(target_arch = "x86",
96 target_arch = "arm"))]
97 const __PTHREAD_RWLOCK_SIZE__: usize = 124;
98
99 const _PTHREAD_MUTEX_SIG_INIT: libc::c_long = 0x32AAABA7;
100 const _PTHREAD_COND_SIG_INIT: libc::c_long = 0x3CB0B1BB;
101 const _PTHREAD_RWLOCK_SIG_INIT: libc::c_long = 0x2DA8B3B4;
102
103 #[repr(C)]
104 pub struct pthread_mutex_t {
105 __sig: libc::c_long,
106 __opaque: [u8; __PTHREAD_MUTEX_SIZE__],
107 }
108 #[repr(C)]
109 pub struct pthread_mutexattr_t {
110 __sig: libc::c_long,
111 // note, that this is 16 bytes just to be safe, the actual struct might be smaller.
112 __opaque: [u8; 16],
113 }
114 #[repr(C)]
115 pub struct pthread_cond_t {
116 __sig: libc::c_long,
117 __opaque: [u8; __PTHREAD_COND_SIZE__],
118 }
119 #[repr(C)]
120 pub struct pthread_rwlock_t {
121 __sig: libc::c_long,
122 __opaque: [u8; __PTHREAD_RWLOCK_SIZE__],
123 }
124
125 pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
126 __sig: _PTHREAD_MUTEX_SIG_INIT,
127 __opaque: [0; __PTHREAD_MUTEX_SIZE__],
128 };
129 pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
130 __sig: _PTHREAD_COND_SIG_INIT,
131 __opaque: [0; __PTHREAD_COND_SIZE__],
132 };
133 pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
134 __sig: _PTHREAD_RWLOCK_SIG_INIT,
135 __opaque: [0; __PTHREAD_RWLOCK_SIZE__],
136 };
137
138 pub const PTHREAD_MUTEX_RECURSIVE: libc::c_int = 2;
139 }
140
141 #[cfg(target_os = "linux")]
142 mod os {
143 use libc;
144
145 // minus 8 because we have an 'align' field
146 #[cfg(target_arch = "x86_64")]
147 const __SIZEOF_PTHREAD_MUTEX_T: usize = 40 - 8;
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_MUTEX_T: usize = 24 - 8;
154 #[cfg(target_arch = "aarch64")]
155 const __SIZEOF_PTHREAD_MUTEX_T: usize = 48 - 8;
156
157 #[cfg(any(target_arch = "x86_64",
158 target_arch = "x86",
159 target_arch = "arm",
160 target_arch = "aarch64",
161 target_arch = "mips",
162 target_arch = "mipsel",
163 target_arch = "powerpc"))]
164 const __SIZEOF_PTHREAD_COND_T: usize = 48 - 8;
165
166 #[cfg(any(target_arch = "x86_64",
167 target_arch = "aarch64"))]
168 const __SIZEOF_PTHREAD_RWLOCK_T: usize = 56 - 8;
169
170 #[cfg(any(target_arch = "x86",
171 target_arch = "arm",
172 target_arch = "mips",
173 target_arch = "mipsel",
174 target_arch = "powerpc"))]
175 const __SIZEOF_PTHREAD_RWLOCK_T: usize = 32 - 8;
176
177 #[repr(C)]
178 pub struct pthread_mutex_t {
179 __align: libc::c_longlong,
180 size: [u8; __SIZEOF_PTHREAD_MUTEX_T],
181 }
182 #[repr(C)]
183 pub struct pthread_mutexattr_t {
184 __align: libc::c_longlong,
185 // note, that this is 16 bytes just to be safe, the actual struct might be smaller.
186 size: [u8; 16],
187 }
188 #[repr(C)]
189 pub struct pthread_cond_t {
190 __align: libc::c_longlong,
191 size: [u8; __SIZEOF_PTHREAD_COND_T],
192 }
193 #[repr(C)]
194 pub struct pthread_rwlock_t {
195 __align: libc::c_longlong,
196 size: [u8; __SIZEOF_PTHREAD_RWLOCK_T],
197 }
198
199 pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
200 __align: 0,
201 size: [0; __SIZEOF_PTHREAD_MUTEX_T],
202 };
203 pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
204 __align: 0,
205 size: [0; __SIZEOF_PTHREAD_COND_T],
206 };
207 pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
208 __align: 0,
209 size: [0; __SIZEOF_PTHREAD_RWLOCK_T],
210 };
211 pub const PTHREAD_MUTEX_RECURSIVE: libc::c_int = 1;
212 }
213 #[cfg(target_os = "android")]
214 mod os {
215 use libc;
216
217 #[repr(C)]
218 pub struct pthread_mutex_t { value: libc::c_int }
219 pub type pthread_mutexattr_t = libc::c_long;
220 #[repr(C)]
221 pub struct pthread_cond_t { value: libc::c_int }
222 #[repr(C)]
223 pub struct pthread_rwlock_t {
224 lock: pthread_mutex_t,
225 cond: pthread_cond_t,
226 numLocks: libc::c_int,
227 writerThreadId: libc::c_int,
228 pendingReaders: libc::c_int,
229 pendingWriters: libc::c_int,
230 reserved: [*mut libc::c_void; 4],
231 }
232
233 pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
234 value: 0,
235 };
236 pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
237 value: 0,
238 };
239 pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
240 lock: PTHREAD_MUTEX_INITIALIZER,
241 cond: PTHREAD_COND_INITIALIZER,
242 numLocks: 0,
243 writerThreadId: 0,
244 pendingReaders: 0,
245 pendingWriters: 0,
246 reserved: [0 as *mut _; 4],
247 };
248 pub const PTHREAD_MUTEX_RECURSIVE: libc::c_int = 1;
249 }