1 /* Copyright (C) 2003-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #ifndef _LOWLEVELLOCK_H
19 #define _LOWLEVELLOCK_H 1
23 #include <sys/param.h>
24 #include <bits/pthreadtypes.h>
25 #include <kernel-features.h>
31 #define FUTEX_CMP_REQUEUE 4
32 #define FUTEX_WAKE_OP 5
33 #define FUTEX_LOCK_PI 6
34 #define FUTEX_UNLOCK_PI 7
35 #define FUTEX_TRYLOCK_PI 8
36 #define FUTEX_WAIT_BITSET 9
37 #define FUTEX_WAKE_BITSET 10
38 #define FUTEX_PRIVATE_FLAG 128
39 #define FUTEX_CLOCK_REALTIME 256
41 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
43 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
45 /* Values for 'private' parameter of locking macros. Yes, the
46 definition seems to be backwards. But it is not. The bit will be
47 reversed before passing to the system call. */
49 #define LLL_SHARED FUTEX_PRIVATE_FLAG
52 #if !defined NOT_IN_libc || defined IS_IN_rtld
53 /* In libc.so or ld.so all futexes are private. */
54 # ifdef __ASSUME_PRIVATE_FUTEX
55 # define __lll_private_flag(fl, private) \
56 ((fl) | FUTEX_PRIVATE_FLAG)
58 # define __lll_private_flag(fl, private) \
59 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
62 # ifdef __ASSUME_PRIVATE_FUTEX
63 # define __lll_private_flag(fl, private) \
64 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
66 # define __lll_private_flag(fl, private) \
67 (__builtin_constant_p (private) \
69 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
71 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
72 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
78 /* Initializer for compatibility lock. */
79 #define LLL_LOCK_INITIALIZER (0)
80 #define LLL_LOCK_INITIALIZER_LOCKED (1)
81 #define LLL_LOCK_INITIALIZER_WAITERS (2)
83 extern int __lll_lock_wait_private (int val
, int *__futex
)
85 extern int __lll_lock_wait (int val
, int *__futex
, int private)
87 extern int __lll_timedlock_wait (int val
, int *__futex
,
88 const struct timespec
*abstime
, int private)
90 extern int __lll_robust_lock_wait (int val
, int *__futex
, int private)
92 extern int __lll_robust_timedlock_wait (int val
, int *__futex
,
93 const struct timespec
*abstime
,
96 extern int __lll_unlock_wake_private (int *__futex
) attribute_hidden
;
97 extern int __lll_unlock_wake (int *__futex
, int private) attribute_hidden
;
99 #define lll_trylock(futex) \
100 ({ unsigned char __result; \
116 "r" (LLL_LOCK_INITIALIZER_LOCKED), \
117 "r" (LLL_LOCK_INITIALIZER) \
118 : "r0", "r1", "r2", "t", "memory"); \
121 #define lll_robust_trylock(futex, id) \
122 ({ unsigned char __result; \
139 "r" (LLL_LOCK_INITIALIZER) \
140 : "r0", "r1", "r2", "t", "memory"); \
143 #define lll_cond_trylock(futex) \
144 ({ unsigned char __result; \
160 "r" (LLL_LOCK_INITIALIZER_WAITERS), \
161 "r" (LLL_LOCK_INITIALIZER) \
162 : "r0", "r1", "r2", "t", "memory"); \
165 #define lll_lock(futex, private) \
166 (void) ({ int __result, *__futex = &(futex); \
178 : "=&r" (__result) : "r" (1), "r" (__futex) \
179 : "r0", "r1", "t", "memory"); \
182 if (__builtin_constant_p (private) \
183 && (private) == LLL_PRIVATE) \
184 __lll_lock_wait_private (__result, __futex); \
186 __lll_lock_wait (__result, __futex, (private)); \
190 #define lll_robust_lock(futex, id, private) \
191 ({ int __result, *__futex = &(futex); \
203 : "=&r" (__result) : "r" (id), "r" (__futex) \
204 : "r0", "r1", "t", "memory"); \
206 __result = __lll_robust_lock_wait (__result, __futex, private); \
209 /* Special version of lll_mutex_lock which causes the unlock function to
210 always wakeup waiters. */
211 #define lll_cond_lock(futex, private) \
212 (void) ({ int __result, *__futex = &(futex); \
224 : "=&r" (__result) : "r" (2), "r" (__futex) \
225 : "r0", "r1", "t", "memory"); \
227 __lll_lock_wait (__result, __futex, private); })
229 #define lll_robust_cond_lock(futex, id, private) \
230 ({ int __result, *__futex = &(futex); \
242 : "=&r" (__result) : "r" (id | FUTEX_WAITERS), "r" (__futex) \
243 : "r0", "r1", "t", "memory"); \
245 __result = __lll_robust_lock_wait (__result, __futex, private); \
248 #define lll_timedlock(futex, timeout, private) \
249 ({ int __result, *__futex = &(futex); \
261 : "=&r" (__result) : "r" (1), "r" (__futex) \
262 : "r0", "r1", "t", "memory"); \
264 __result = __lll_timedlock_wait (__result, __futex, timeout, private); \
267 #define lll_robust_timedlock(futex, timeout, id, private) \
268 ({ int __result, *__futex = &(futex); \
280 : "=&r" (__result) : "r" (id), "r" (__futex) \
281 : "r0", "r1", "t", "memory"); \
283 __result = __lll_robust_timedlock_wait (__result, __futex, \
287 #define lll_unlock(futex, private) \
288 (void) ({ int __result, *__futex = &(futex); \
298 : "=&r" (__result) : "r" (__futex) \
299 : "r0", "r1", "memory"); \
302 if (__builtin_constant_p (private) \
303 && (private) == LLL_PRIVATE) \
304 __lll_unlock_wake_private (__futex); \
306 __lll_unlock_wake (__futex, (private)); \
310 #define lll_robust_unlock(futex, private) \
311 (void) ({ int __result, *__futex = &(futex); \
321 : "=&r" (__result) : "r" (__futex), "r" (FUTEX_WAITERS) \
322 : "r0", "r1", "memory"); \
324 __lll_unlock_wake (__futex, private); })
326 #define lll_robust_dead(futex, private) \
327 (void) ({ int __ignore, *__futex = &(futex); \
337 : "=&r" (__ignore) : "r" (__futex), "r" (FUTEX_OWNER_DIED) \
338 : "r0", "r1", "memory"); \
339 lll_futex_wake (__futex, 1, private); })
341 # ifdef NEED_SYSCALL_INST_PAD
342 # define SYSCALL_WITH_INST_PAD "\
343 trapa #0x14; or r0,r0; or r0,r0; or r0,r0; or r0,r0; or r0,r0"
345 # define SYSCALL_WITH_INST_PAD "\
349 #define lll_futex_wait(futex, val, private) \
350 lll_futex_timed_wait (futex, val, NULL, private)
353 #define lll_futex_timed_wait(futex, val, timeout, private) \
356 register unsigned long __r3 asm ("r3") = SYS_futex; \
357 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
358 register unsigned long __r5 asm ("r5") \
359 = __lll_private_flag (FUTEX_WAIT, private); \
360 register unsigned long __r6 asm ("r6") = (unsigned long) (val); \
361 register unsigned long __r7 asm ("r7") = (timeout); \
362 __asm __volatile (SYSCALL_WITH_INST_PAD \
364 : "r" (__r3), "r" (__r4), "r" (__r5), \
365 "r" (__r6), "r" (__r7) \
371 #define lll_futex_wake(futex, nr, private) \
374 register unsigned long __r3 asm ("r3") = SYS_futex; \
375 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
376 register unsigned long __r5 asm ("r5") \
377 = __lll_private_flag (FUTEX_WAKE, private); \
378 register unsigned long __r6 asm ("r6") = (unsigned long) (nr); \
379 register unsigned long __r7 asm ("r7") = 0; \
380 __asm __volatile (SYSCALL_WITH_INST_PAD \
382 : "r" (__r3), "r" (__r4), "r" (__r5), \
383 "r" (__r6), "r" (__r7) \
388 #define lll_islocked(futex) \
389 (futex != LLL_LOCK_INITIALIZER)
391 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
392 wakeup when the clone terminates. The memory location contains the
393 thread ID while the clone is running and is reset to zero
396 #define lll_wait_tid(tid) \
398 __typeof (tid) *__tid = &(tid); \
399 while (*__tid != 0) \
400 lll_futex_wait (__tid, *__tid, LLL_SHARED); \
403 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
405 #define lll_timedwait_tid(tid, abstime) \
410 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
413 __result = __lll_timedwait_tid (&tid, abstime); \
417 #endif /* !__ASSEMBLER__ */
419 #endif /* lowlevellock.h */