]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 2005-2014 Free Software Foundation, Inc. |
554066b8 MS |
2 | |
3 | This file is part of the GNU C Library. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #ifndef _LOWLEVELLOCK_H | |
20 | #define _LOWLEVELLOCK_H 1 | |
21 | ||
22 | #include <time.h> | |
23 | #include <sys/param.h> | |
24 | #include <bits/pthreadtypes.h> | |
25 | #include <atomic.h> | |
26 | #include <sysdep.h> | |
27 | #include <kernel-features.h> | |
28 | ||
29 | #define FUTEX_WAIT 0 | |
30 | #define FUTEX_WAKE 1 | |
31 | #define FUTEX_REQUEUE 3 | |
32 | #define FUTEX_CMP_REQUEUE 4 | |
33 | #define FUTEX_WAKE_OP 5 | |
34 | #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1) | |
35 | #define FUTEX_LOCK_PI 6 | |
36 | #define FUTEX_UNLOCK_PI 7 | |
37 | #define FUTEX_TRYLOCK_PI 8 | |
38 | #define FUTEX_WAIT_BITSET 9 | |
39 | #define FUTEX_WAKE_BITSET 10 | |
0f197fe5 MS |
40 | #define FUTEX_WAIT_REQUEUE_PI 11 |
41 | #define FUTEX_CMP_REQUEUE_PI 12 | |
554066b8 MS |
42 | #define FUTEX_PRIVATE_FLAG 128 |
43 | #define FUTEX_CLOCK_REALTIME 256 | |
44 | ||
45 | #define FUTEX_BITSET_MATCH_ANY 0xffffffff | |
46 | ||
47 | /* Values for 'private' parameter of locking macros. Yes, the | |
48 | definition seems to be backwards. But it is not. The bit will be | |
49 | reversed before passing to the system call. */ | |
50 | #define LLL_PRIVATE 0 | |
51 | #define LLL_SHARED FUTEX_PRIVATE_FLAG | |
52 | ||
53 | ||
54 | #if !defined NOT_IN_libc || defined IS_IN_rtld | |
55 | /* In libc.so or ld.so all futexes are private. */ | |
56 | # ifdef __ASSUME_PRIVATE_FUTEX | |
57 | # define __lll_private_flag(fl, private) \ | |
58 | ((fl) | FUTEX_PRIVATE_FLAG) | |
59 | # else | |
60 | # define __lll_private_flag(fl, private) \ | |
61 | ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) | |
62 | # endif | |
63 | #else | |
64 | # ifdef __ASSUME_PRIVATE_FUTEX | |
65 | # define __lll_private_flag(fl, private) \ | |
66 | (((fl) | FUTEX_PRIVATE_FLAG) ^ (private)) | |
67 | # else | |
68 | # define __lll_private_flag(fl, private) \ | |
69 | (__builtin_constant_p (private) \ | |
70 | ? ((private) == 0 \ | |
71 | ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \ | |
72 | : (fl)) \ | |
73 | : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \ | |
74 | & THREAD_GETMEM (THREAD_SELF, header.private_futex)))) | |
75 | # endif | |
76 | #endif | |
77 | ||
78 | ||
79 | #define lll_futex_wait(futexp, val, private) \ | |
80 | lll_futex_timed_wait(futexp, val, NULL, private) | |
81 | ||
82 | #define lll_futex_timed_wait(futexp, val, timespec, private) \ | |
83 | ({ \ | |
84 | INTERNAL_SYSCALL_DECL (__err); \ | |
85 | long int __ret; \ | |
86 | __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \ | |
87 | __lll_private_flag (FUTEX_WAIT, private), \ | |
88 | (val), (timespec)); \ | |
89 | __ret; \ | |
90 | }) | |
91 | ||
92 | #define lll_futex_timed_wait_bitset(futexp, val, timespec, clockbit, private) \ | |
93 | ({ \ | |
94 | INTERNAL_SYSCALL_DECL (__err); \ | |
95 | long int __ret; \ | |
96 | int __op = FUTEX_WAIT_BITSET | clockbit; \ | |
97 | __ret = INTERNAL_SYSCALL (futex, __err, 6, (long) (futexp), \ | |
98 | __lll_private_flag (__op, private), \ | |
99 | (val), (timespec), NULL /* Unused. */, \ | |
100 | FUTEX_BITSET_MATCH_ANY); \ | |
86fe56bb | 101 | __ret; \ |
554066b8 MS |
102 | }) |
103 | ||
104 | #define lll_futex_wake(futexp, nr, private) \ | |
105 | ({ \ | |
106 | INTERNAL_SYSCALL_DECL (__err); \ | |
107 | long int __ret; \ | |
108 | __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \ | |
109 | __lll_private_flag (FUTEX_WAKE, private), \ | |
110 | (nr), 0); \ | |
111 | __ret; \ | |
112 | }) | |
113 | ||
114 | #define lll_robust_dead(futexv, private) \ | |
115 | do \ | |
116 | { \ | |
117 | int *__futexp = &(futexv); \ | |
118 | atomic_or (__futexp, FUTEX_OWNER_DIED); \ | |
119 | lll_futex_wake (__futexp, 1, private); \ | |
120 | } \ | |
121 | while (0) | |
122 | ||
123 | /* Returns non-zero if error happened, zero if success. */ | |
124 | #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \ | |
125 | ({ \ | |
126 | INTERNAL_SYSCALL_DECL (__err); \ | |
127 | long int __ret; \ | |
128 | __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \ | |
129 | __lll_private_flag (FUTEX_CMP_REQUEUE, private),\ | |
130 | (nr_wake), (nr_move), (mutex), (val)); \ | |
131 | INTERNAL_SYSCALL_ERROR_P (__ret, __err); \ | |
132 | }) | |
133 | ||
134 | ||
135 | /* Returns non-zero if error happened, zero if success. */ | |
136 | #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \ | |
137 | ({ \ | |
138 | INTERNAL_SYSCALL_DECL (__err); \ | |
139 | long int __ret; \ | |
140 | __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \ | |
141 | __lll_private_flag (FUTEX_WAKE_OP, private), \ | |
142 | (nr_wake), (nr_wake2), (futexp2), \ | |
143 | FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \ | |
144 | INTERNAL_SYSCALL_ERROR_P (__ret, __err); \ | |
145 | }) | |
146 | ||
147 | ||
0f197fe5 MS |
148 | /* Priority Inheritance support. */ |
149 | #define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \ | |
150 | lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private) | |
151 | ||
152 | #define lll_futex_timed_wait_requeue_pi(futexp, val, timespec, clockbit, \ | |
153 | mutex, private) \ | |
154 | ({ \ | |
155 | INTERNAL_SYSCALL_DECL (__err); \ | |
156 | long int __ret; \ | |
157 | int __op = FUTEX_WAIT_REQUEUE_PI | clockbit; \ | |
158 | \ | |
159 | __ret = INTERNAL_SYSCALL (futex, __err, 5, (futexp), \ | |
160 | __lll_private_flag (__op, private), \ | |
161 | (val), (timespec), mutex); \ | |
162 | INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \ | |
163 | }) | |
164 | ||
165 | #define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, val, priv) \ | |
166 | ({ \ | |
167 | INTERNAL_SYSCALL_DECL (__err); \ | |
168 | long int __ret; \ | |
169 | \ | |
170 | __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \ | |
171 | __lll_private_flag (FUTEX_CMP_REQUEUE_PI, priv),\ | |
172 | (nr_wake), (nr_move), (mutex), (val)); \ | |
173 | INTERNAL_SYSCALL_ERROR_P (__ret, __err); \ | |
174 | }) | |
175 | ||
176 | ||
554066b8 MS |
177 | #define lll_trylock(lock) \ |
178 | atomic_compare_and_exchange_val_acq(&(lock), 1, 0) | |
179 | ||
180 | #define lll_cond_trylock(lock) \ | |
181 | atomic_compare_and_exchange_val_acq(&(lock), 2, 0) | |
182 | ||
554066b8 MS |
183 | extern void __lll_lock_wait_private (int *futex) attribute_hidden; |
184 | extern void __lll_lock_wait (int *futex, int private) attribute_hidden; | |
185 | extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden; | |
186 | ||
187 | #define __lll_lock(futex, private) \ | |
188 | ((void) ({ \ | |
189 | int *__futex = (futex); \ | |
190 | if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, \ | |
191 | 1, 0), 0)) \ | |
192 | { \ | |
193 | if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \ | |
194 | __lll_lock_wait_private (__futex); \ | |
195 | else \ | |
196 | __lll_lock_wait (__futex, private); \ | |
197 | } \ | |
198 | })) | |
199 | #define lll_lock(futex, private) __lll_lock (&(futex), private) | |
200 | ||
201 | ||
202 | #define __lll_robust_lock(futex, id, private) \ | |
203 | ({ \ | |
204 | int *__futex = (futex); \ | |
205 | int __val = 0; \ | |
206 | \ | |
207 | if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \ | |
208 | 0), 0)) \ | |
209 | __val = __lll_robust_lock_wait (__futex, private); \ | |
210 | __val; \ | |
211 | }) | |
212 | #define lll_robust_lock(futex, id, private) \ | |
213 | __lll_robust_lock (&(futex), id, private) | |
214 | ||
215 | ||
216 | #define __lll_cond_lock(futex, private) \ | |
217 | ((void) ({ \ | |
218 | int *__futex = (futex); \ | |
219 | if (__builtin_expect (atomic_exchange_acq (__futex, 2), 0)) \ | |
220 | __lll_lock_wait (__futex, private); \ | |
221 | })) | |
222 | #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private) | |
223 | ||
224 | ||
225 | #define lll_robust_cond_lock(futex, id, private) \ | |
226 | __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private) | |
227 | ||
228 | ||
229 | extern int __lll_timedlock_wait (int *futex, const struct timespec *, | |
230 | int private) attribute_hidden; | |
231 | extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *, | |
232 | int private) attribute_hidden; | |
233 | ||
234 | #define __lll_timedlock(futex, abstime, private) \ | |
235 | ({ \ | |
236 | int *__futex = (futex); \ | |
237 | int __val = 0; \ | |
238 | \ | |
239 | if (__builtin_expect (atomic_exchange_acq (__futex, 1), 0)) \ | |
240 | __val = __lll_timedlock_wait (__futex, abstime, private); \ | |
241 | __val; \ | |
242 | }) | |
243 | #define lll_timedlock(futex, abstime, private) \ | |
244 | __lll_timedlock (&(futex), abstime, private) | |
245 | ||
246 | ||
247 | #define __lll_robust_timedlock(futex, abstime, id, private) \ | |
248 | ({ \ | |
249 | int *__futex = (futex); \ | |
250 | int __val = 0; \ | |
251 | \ | |
252 | if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \ | |
253 | 0), 0)) \ | |
254 | __val = __lll_robust_timedlock_wait (__futex, abstime, private); \ | |
255 | __val; \ | |
256 | }) | |
257 | #define lll_robust_timedlock(futex, abstime, id, private) \ | |
258 | __lll_robust_timedlock (&(futex), abstime, id, private) | |
259 | ||
260 | ||
261 | #define __lll_unlock(futex, private) \ | |
262 | (void) \ | |
263 | ({ int *__futex = (futex); \ | |
264 | int __oldval = atomic_exchange_rel (__futex, 0); \ | |
265 | if (__builtin_expect (__oldval > 1, 0)) \ | |
266 | lll_futex_wake (__futex, 1, private); \ | |
267 | }) | |
268 | ||
269 | #define lll_unlock(futex, private) __lll_unlock(&(futex), private) | |
270 | ||
271 | ||
272 | #define __lll_robust_unlock(futex, private) \ | |
273 | (void) \ | |
274 | ({ int *__futex = (futex); \ | |
275 | int __oldval = atomic_exchange_rel (__futex, 0); \ | |
276 | if (__builtin_expect (__oldval & FUTEX_WAITERS, 0)) \ | |
277 | lll_futex_wake (__futex, 1, private); \ | |
278 | }) | |
279 | #define lll_robust_unlock(futex, private) \ | |
280 | __lll_robust_unlock(&(futex), private) | |
281 | ||
282 | ||
283 | #define lll_islocked(futex) \ | |
284 | (futex != 0) | |
285 | ||
286 | ||
287 | /* Our internal lock implementation is identical to the binary-compatible | |
288 | mutex implementation. */ | |
289 | ||
290 | /* Initializers for lock. */ | |
291 | #define LLL_LOCK_INITIALIZER (0) | |
292 | #define LLL_LOCK_INITIALIZER_LOCKED (1) | |
293 | ||
294 | /* The states of a lock are: | |
295 | 0 - untaken | |
296 | 1 - taken by one user | |
297 | >1 - taken by more users */ | |
298 | ||
299 | /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex | |
300 | wakeup when the clone terminates. The memory location contains the | |
301 | thread ID while the clone is running and is reset to zero | |
302 | afterwards. */ | |
303 | #define lll_wait_tid(tid) \ | |
304 | do { \ | |
305 | __typeof (tid) __tid; \ | |
306 | while ((__tid = (tid)) != 0) \ | |
307 | lll_futex_wait (&(tid), __tid, LLL_SHARED);\ | |
308 | } while (0) | |
309 | ||
310 | extern int __lll_timedwait_tid (int *, const struct timespec *) | |
311 | attribute_hidden; | |
312 | ||
313 | #define lll_timedwait_tid(tid, abstime) \ | |
314 | ({ \ | |
315 | int __res = 0; \ | |
316 | if ((tid) != 0) \ | |
317 | __res = __lll_timedwait_tid (&(tid), (abstime)); \ | |
318 | __res; \ | |
319 | }) | |
320 | ||
321 | #endif /* lowlevellock.h */ |