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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
25 #include <sys/param.h>
26 #include <bits/pthreadtypes.h>
27 #include <kernel-features.h>
33 #define FUTEX_CMP_REQUEUE 4
34 #define FUTEX_WAKE_OP 5
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
40 #define FUTEX_PRIVATE_FLAG 128
41 #define FUTEX_CLOCK_REALTIME 256
43 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
45 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
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. */
51 #define LLL_SHARED FUTEX_PRIVATE_FLAG
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)
60 # define __lll_private_flag(fl, private) \
61 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
64 # ifdef __ASSUME_PRIVATE_FUTEX
65 # define __lll_private_flag(fl, private) \
66 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
68 # define __lll_private_flag(fl, private) \
69 (__builtin_constant_p (private) \
71 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
73 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
74 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
80 /* Initializer for compatibility lock. */
81 #define LLL_LOCK_INITIALIZER (0)
82 #define LLL_LOCK_INITIALIZER_LOCKED (1)
83 #define LLL_LOCK_INITIALIZER_WAITERS (2)
85 extern int __lll_lock_wait_private (int val
, int *__futex
)
87 extern int __lll_lock_wait (int val
, int *__futex
, int private)
89 extern int __lll_timedlock_wait (int val
, int *__futex
,
90 const struct timespec
*abstime
, int private)
92 extern int __lll_robust_lock_wait (int val
, int *__futex
, int private)
94 extern int __lll_robust_timedlock_wait (int val
, int *__futex
,
95 const struct timespec
*abstime
,
98 extern int __lll_unlock_wake_private (int *__futex
) attribute_hidden
;
99 extern int __lll_unlock_wake (int *__futex
, int private) attribute_hidden
;
101 #define lll_trylock(futex) \
102 ({ unsigned char __result; \
118 "r" (LLL_LOCK_INITIALIZER_LOCKED), \
119 "r" (LLL_LOCK_INITIALIZER) \
120 : "r0", "r1", "r2", "t", "memory"); \
123 #define lll_robust_trylock(futex, id) \
124 ({ unsigned char __result; \
141 "r" (LLL_LOCK_INITIALIZER) \
142 : "r0", "r1", "r2", "t", "memory"); \
145 #define lll_cond_trylock(futex) \
146 ({ unsigned char __result; \
162 "r" (LLL_LOCK_INITIALIZER_WAITERS), \
163 "r" (LLL_LOCK_INITIALIZER) \
164 : "r0", "r1", "r2", "t", "memory"); \
167 #define lll_lock(futex, private) \
168 (void) ({ int __result, *__futex = &(futex); \
180 : "=&r" (__result) : "r" (1), "r" (__futex) \
181 : "r0", "r1", "t", "memory"); \
184 if (__builtin_constant_p (private) \
185 && (private) == LLL_PRIVATE) \
186 __lll_lock_wait_private (__result, __futex); \
188 __lll_lock_wait (__result, __futex, (private)); \
192 #define lll_robust_lock(futex, id, private) \
193 ({ int __result, *__futex = &(futex); \
205 : "=&r" (__result) : "r" (id), "r" (__futex) \
206 : "r0", "r1", "t", "memory"); \
208 __result = __lll_robust_lock_wait (__result, __futex, private); \
211 /* Special version of lll_mutex_lock which causes the unlock function to
212 always wakeup waiters. */
213 #define lll_cond_lock(futex, private) \
214 (void) ({ int __result, *__futex = &(futex); \
226 : "=&r" (__result) : "r" (2), "r" (__futex) \
227 : "r0", "r1", "t", "memory"); \
229 __lll_lock_wait (__result, __futex, private); })
231 #define lll_robust_cond_lock(futex, id, private) \
232 ({ int __result, *__futex = &(futex); \
244 : "=&r" (__result) : "r" (id | FUTEX_WAITERS), "r" (__futex) \
245 : "r0", "r1", "t", "memory"); \
247 __result = __lll_robust_lock_wait (__result, __futex, private); \
250 #define lll_timedlock(futex, timeout, private) \
251 ({ int __result, *__futex = &(futex); \
263 : "=&r" (__result) : "r" (1), "r" (__futex) \
264 : "r0", "r1", "t", "memory"); \
266 __result = __lll_timedlock_wait (__result, __futex, timeout, private); \
269 #define lll_robust_timedlock(futex, timeout, id, private) \
270 ({ int __result, *__futex = &(futex); \
282 : "=&r" (__result) : "r" (id), "r" (__futex) \
283 : "r0", "r1", "t", "memory"); \
285 __result = __lll_robust_timedlock_wait (__result, __futex, \
289 #define lll_unlock(futex, private) \
290 (void) ({ int __result, *__futex = &(futex); \
300 : "=&r" (__result) : "r" (__futex) \
301 : "r0", "r1", "memory"); \
304 if (__builtin_constant_p (private) \
305 && (private) == LLL_PRIVATE) \
306 __lll_unlock_wake_private (__futex); \
308 __lll_unlock_wake (__futex, (private)); \
312 #define lll_robust_unlock(futex, private) \
313 (void) ({ int __result, *__futex = &(futex); \
323 : "=&r" (__result) : "r" (__futex), "r" (FUTEX_WAITERS) \
324 : "r0", "r1", "memory"); \
326 __lll_unlock_wake (__futex, private); })
328 #define lll_robust_dead(futex, private) \
329 (void) ({ int __ignore, *__futex = &(futex); \
339 : "=&r" (__ignore) : "r" (__futex), "r" (FUTEX_OWNER_DIED) \
340 : "r0", "r1", "memory"); \
341 lll_futex_wake (__futex, 1, private); })
343 # ifdef NEED_SYSCALL_INST_PAD
344 # define SYSCALL_WITH_INST_PAD "\
345 trapa #0x14; or r0,r0; or r0,r0; or r0,r0; or r0,r0; or r0,r0"
347 # define SYSCALL_WITH_INST_PAD "\
351 #define lll_futex_wait(futex, val, private) \
352 lll_futex_timed_wait (futex, val, NULL, private)
355 #define lll_futex_timed_wait(futex, val, timeout, private) \
358 register unsigned long __r3 asm ("r3") = SYS_futex; \
359 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
360 register unsigned long __r5 asm ("r5") \
361 = __lll_private_flag (FUTEX_WAIT, private); \
362 register unsigned long __r6 asm ("r6") = (unsigned long) (val); \
363 register unsigned long __r7 asm ("r7") = (timeout); \
364 __asm __volatile (SYSCALL_WITH_INST_PAD \
366 : "r" (__r3), "r" (__r4), "r" (__r5), \
367 "r" (__r6), "r" (__r7) \
373 #define lll_futex_wake(futex, nr, private) \
376 register unsigned long __r3 asm ("r3") = SYS_futex; \
377 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
378 register unsigned long __r5 asm ("r5") \
379 = __lll_private_flag (FUTEX_WAKE, private); \
380 register unsigned long __r6 asm ("r6") = (unsigned long) (nr); \
381 register unsigned long __r7 asm ("r7") = 0; \
382 __asm __volatile (SYSCALL_WITH_INST_PAD \
384 : "r" (__r3), "r" (__r4), "r" (__r5), \
385 "r" (__r6), "r" (__r7) \
390 #define lll_islocked(futex) \
391 (futex != LLL_LOCK_INITIALIZER)
393 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
394 wakeup when the clone terminates. The memory location contains the
395 thread ID while the clone is running and is reset to zero
398 #define lll_wait_tid(tid) \
400 __typeof (tid) *__tid = &(tid); \
401 while (*__tid != 0) \
402 lll_futex_wait (__tid, *__tid, LLL_SHARED); \
405 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
407 #define lll_timedwait_tid(tid, abstime) \
412 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
415 __result = __lll_timedwait_tid (&tid, abstime); \
419 #endif /* !__ASSEMBLER__ */
421 #endif /* lowlevellock.h */