1 /* Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
24 #include <sys/param.h>
25 #include <bits/pthreadtypes.h>
26 #include <kernel-features.h>
32 #define FUTEX_CMP_REQUEUE 4
33 #define FUTEX_WAKE_OP 5
34 #define FUTEX_LOCK_PI 6
35 #define FUTEX_UNLOCK_PI 7
36 #define FUTEX_TRYLOCK_PI 8
37 #define FUTEX_WAIT_BITSET 9
38 #define FUTEX_WAKE_BITSET 10
39 #define FUTEX_PRIVATE_FLAG 128
40 #define FUTEX_CLOCK_REALTIME 256
42 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
44 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
46 /* Values for 'private' parameter of locking macros. Yes, the
47 definition seems to be backwards. But it is not. The bit will be
48 reversed before passing to the system call. */
50 #define LLL_SHARED FUTEX_PRIVATE_FLAG
53 #if !defined NOT_IN_libc || defined IS_IN_rtld
54 /* In libc.so or ld.so all futexes are private. */
55 # ifdef __ASSUME_PRIVATE_FUTEX
56 # define __lll_private_flag(fl, private) \
57 ((fl) | FUTEX_PRIVATE_FLAG)
59 # define __lll_private_flag(fl, private) \
60 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
63 # ifdef __ASSUME_PRIVATE_FUTEX
64 # define __lll_private_flag(fl, private) \
65 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
67 # define __lll_private_flag(fl, private) \
68 (__builtin_constant_p (private) \
70 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
72 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
73 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
79 /* Initializer for compatibility lock. */
80 #define LLL_LOCK_INITIALIZER (0)
81 #define LLL_LOCK_INITIALIZER_LOCKED (1)
82 #define LLL_LOCK_INITIALIZER_WAITERS (2)
84 extern int __lll_lock_wait_private (int val
, int *__futex
)
86 extern int __lll_lock_wait (int val
, int *__futex
, int private)
88 extern int __lll_timedlock_wait (int val
, int *__futex
,
89 const struct timespec
*abstime
, int private)
91 extern int __lll_robust_lock_wait (int val
, int *__futex
, int private)
93 extern int __lll_robust_timedlock_wait (int val
, int *__futex
,
94 const struct timespec
*abstime
,
97 extern int __lll_unlock_wake_private (int *__futex
) attribute_hidden
;
98 extern int __lll_unlock_wake (int *__futex
, int private) attribute_hidden
;
100 #define lll_trylock(futex) \
101 ({ unsigned char __result; \
117 "r" (LLL_LOCK_INITIALIZER_LOCKED), \
118 "r" (LLL_LOCK_INITIALIZER) \
119 : "r0", "r1", "r2", "t", "memory"); \
122 #define lll_robust_trylock(futex, id) \
123 ({ unsigned char __result; \
140 "r" (LLL_LOCK_INITIALIZER) \
141 : "r0", "r1", "r2", "t", "memory"); \
144 #define lll_cond_trylock(futex) \
145 ({ unsigned char __result; \
161 "r" (LLL_LOCK_INITIALIZER_WAITERS), \
162 "r" (LLL_LOCK_INITIALIZER) \
163 : "r0", "r1", "r2", "t", "memory"); \
166 #define lll_lock(futex, private) \
167 (void) ({ int __result, *__futex = &(futex); \
179 : "=&r" (__result) : "r" (1), "r" (__futex) \
180 : "r0", "r1", "t", "memory"); \
183 if (__builtin_constant_p (private) \
184 && (private) == LLL_PRIVATE) \
185 __lll_lock_wait_private (__result, __futex); \
187 __lll_lock_wait (__result, __futex, (private)); \
191 #define lll_robust_lock(futex, id, private) \
192 ({ int __result, *__futex = &(futex); \
204 : "=&r" (__result) : "r" (id), "r" (__futex) \
205 : "r0", "r1", "t", "memory"); \
207 __result = __lll_robust_lock_wait (__result, __futex, private); \
210 /* Special version of lll_mutex_lock which causes the unlock function to
211 always wakeup waiters. */
212 #define lll_cond_lock(futex, private) \
213 (void) ({ int __result, *__futex = &(futex); \
225 : "=&r" (__result) : "r" (2), "r" (__futex) \
226 : "r0", "r1", "t", "memory"); \
228 __lll_lock_wait (__result, __futex, private); })
230 #define lll_robust_cond_lock(futex, id, private) \
231 ({ int __result, *__futex = &(futex); \
243 : "=&r" (__result) : "r" (id | FUTEX_WAITERS), "r" (__futex) \
244 : "r0", "r1", "t", "memory"); \
246 __result = __lll_robust_lock_wait (__result, __futex, private); \
249 #define lll_timedlock(futex, timeout, private) \
250 ({ int __result, *__futex = &(futex); \
262 : "=&r" (__result) : "r" (1), "r" (__futex) \
263 : "r0", "r1", "t", "memory"); \
265 __result = __lll_timedlock_wait (__result, __futex, timeout, private); \
268 #define lll_robust_timedlock(futex, timeout, id, private) \
269 ({ int __result, *__futex = &(futex); \
281 : "=&r" (__result) : "r" (id), "r" (__futex) \
282 : "r0", "r1", "t", "memory"); \
284 __result = __lll_robust_timedlock_wait (__result, __futex, \
288 #define lll_unlock(futex, private) \
289 (void) ({ int __result, *__futex = &(futex); \
299 : "=&r" (__result) : "r" (__futex) \
300 : "r0", "r1", "memory"); \
303 if (__builtin_constant_p (private) \
304 && (private) == LLL_PRIVATE) \
305 __lll_unlock_wake_private (__futex); \
307 __lll_unlock_wake (__futex, (private)); \
311 #define lll_robust_unlock(futex, private) \
312 (void) ({ int __result, *__futex = &(futex); \
322 : "=&r" (__result) : "r" (__futex), "r" (FUTEX_WAITERS) \
323 : "r0", "r1", "memory"); \
325 __lll_unlock_wake (__futex, private); })
327 #define lll_robust_dead(futex, private) \
328 (void) ({ int __ignore, *__futex = &(futex); \
338 : "=&r" (__ignore) : "r" (__futex), "r" (FUTEX_OWNER_DIED) \
339 : "r0", "r1", "memory"); \
340 lll_futex_wake (__futex, 1, private); })
342 # ifdef NEED_SYSCALL_INST_PAD
343 # define SYSCALL_WITH_INST_PAD "\
344 trapa #0x14; or r0,r0; or r0,r0; or r0,r0; or r0,r0; or r0,r0"
346 # define SYSCALL_WITH_INST_PAD "\
350 #define lll_futex_wait(futex, val, private) \
351 lll_futex_timed_wait (futex, val, NULL, private)
354 #define lll_futex_timed_wait(futex, val, timeout, private) \
357 register unsigned long __r3 asm ("r3") = SYS_futex; \
358 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
359 register unsigned long __r5 asm ("r5") \
360 = __lll_private_flag (FUTEX_WAIT, private); \
361 register unsigned long __r6 asm ("r6") = (unsigned long) (val); \
362 register unsigned long __r7 asm ("r7") = (timeout); \
363 __asm __volatile (SYSCALL_WITH_INST_PAD \
365 : "r" (__r3), "r" (__r4), "r" (__r5), \
366 "r" (__r6), "r" (__r7) \
372 #define lll_futex_wake(futex, nr, private) \
375 register unsigned long __r3 asm ("r3") = SYS_futex; \
376 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
377 register unsigned long __r5 asm ("r5") \
378 = __lll_private_flag (FUTEX_WAKE, private); \
379 register unsigned long __r6 asm ("r6") = (unsigned long) (nr); \
380 register unsigned long __r7 asm ("r7") = 0; \
381 __asm __volatile (SYSCALL_WITH_INST_PAD \
383 : "r" (__r3), "r" (__r4), "r" (__r5), \
384 "r" (__r6), "r" (__r7) \
389 #define lll_islocked(futex) \
390 (futex != LLL_LOCK_INITIALIZER)
392 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
393 wakeup when the clone terminates. The memory location contains the
394 thread ID while the clone is running and is reset to zero
397 #define lll_wait_tid(tid) \
399 __typeof (tid) *__tid = &(tid); \
400 while (*__tid != 0) \
401 lll_futex_wait (__tid, *__tid, LLL_SHARED); \
404 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
406 #define lll_timedwait_tid(tid, abstime) \
411 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
414 __result = __lll_timedwait_tid (&tid, abstime); \
418 #endif /* !__ASSEMBLER__ */
420 #endif /* lowlevellock.h */