1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
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
23 #include <sys/param.h>
24 #include <bits/pthreadtypes.h>
26 #include <kernel-features.h>
29 # define __NR_futex 221
33 #define FUTEX_REQUEUE 3
34 #define FUTEX_CMP_REQUEUE 4
35 #define FUTEX_WAKE_OP 5
36 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
37 #define FUTEX_LOCK_PI 6
38 #define FUTEX_UNLOCK_PI 7
39 #define FUTEX_TRYLOCK_PI 8
40 #define FUTEX_WAIT_BITSET 9
41 #define FUTEX_WAKE_BITSET 10
42 #define FUTEX_WAIT_REQUEUE_PI 11
43 #define FUTEX_CMP_REQUEUE_PI 12
44 #define FUTEX_PRIVATE_FLAG 128
45 #define FUTEX_CLOCK_REALTIME 256
47 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
49 /* Values for 'private' parameter of locking macros. Yes, the
50 definition seems to be backwards. But it is not. The bit will be
51 reversed before passing to the system call. */
53 #define LLL_SHARED FUTEX_PRIVATE_FLAG
55 #if !defined NOT_IN_libc || defined IS_IN_rtld
56 /* In libc.so or ld.so all futexes are private. */
57 # ifdef __ASSUME_PRIVATE_FUTEX
58 # define __lll_private_flag(fl, private) \
59 ((fl) | FUTEX_PRIVATE_FLAG)
61 # define __lll_private_flag(fl, private) \
62 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
65 # ifdef __ASSUME_PRIVATE_FUTEX
66 # define __lll_private_flag(fl, private) \
67 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
69 # define __lll_private_flag(fl, private) \
70 (__builtin_constant_p (private) \
72 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
74 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
75 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
79 #define lll_futex_wait(futexp, val, private) \
80 lll_futex_timed_wait (futexp, val, NULL, private)
82 #define lll_futex_timed_wait(futexp, val, timespec, private) \
84 INTERNAL_SYSCALL_DECL (__err); \
87 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
88 __lll_private_flag (FUTEX_WAIT, private), \
90 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
93 #define lll_futex_timed_wait_bitset(futexp, val, timespec, clockbit, private) \
95 INTERNAL_SYSCALL_DECL (__err); \
97 int __op = FUTEX_WAIT_BITSET | clockbit; \
99 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
100 __lll_private_flag (__op, private), \
101 (val), (timespec), NULL /* Unused. */, \
102 FUTEX_BITSET_MATCH_ANY); \
103 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
106 #define lll_futex_wake(futexp, nr, private) \
108 INTERNAL_SYSCALL_DECL (__err); \
111 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
112 __lll_private_flag (FUTEX_WAKE, private), \
114 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
117 /* Returns non-zero if error happened, zero if success. */
118 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
120 INTERNAL_SYSCALL_DECL (__err); \
123 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
124 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
125 (nr_wake), (nr_move), (mutex), (val)); \
126 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
129 /* Returns non-zero if error happened, zero if success. */
130 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
132 INTERNAL_SYSCALL_DECL (__err); \
135 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
136 __lll_private_flag (FUTEX_WAKE_OP, private), \
137 (nr_wake), (nr_wake2), (futexp2), \
138 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
139 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
142 /* Priority Inheritance support. */
143 #define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
144 lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
146 #define lll_futex_timed_wait_requeue_pi(futexp, val, timespec, clockbit, \
149 INTERNAL_SYSCALL_DECL (__err); \
151 int __op = FUTEX_WAIT_REQUEUE_PI | clockbit; \
153 __ret = INTERNAL_SYSCALL (futex, __err, 5, (futexp), \
154 __lll_private_flag (__op, private), \
155 (val), (timespec), mutex); \
156 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
159 #define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, val, priv) \
161 INTERNAL_SYSCALL_DECL (__err); \
164 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
165 __lll_private_flag (FUTEX_CMP_REQUEUE_PI, priv),\
166 (nr_wake), (nr_move), (mutex), (val)); \
167 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
172 # define __lll_acq_instr ""
173 # define __lll_rel_instr ""
175 # define __lll_acq_instr "isync"
178 * Newer powerpc64 processors support the new "light weight" sync (lwsync)
179 * So if the build is using -mcpu=[power4,power5,power5+,970] we can
182 # define __lll_rel_instr "lwsync"
185 * Older powerpc32 processors don't support the new "light weight"
186 * sync (lwsync). So the only safe option is to use normal sync
187 * for all powerpc32 applications.
189 # define __lll_rel_instr "sync"
193 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
194 #define __lll_trylock(futex) __lll_robust_trylock (futex, 1)
196 #define lll_trylock(lock) __lll_trylock (&(lock))
198 /* Set *futex to 2 if it is 0, atomically. Returns the old value */
199 #define __lll_cond_trylock(futex) __lll_robust_trylock (futex, 2)
201 #define lll_cond_trylock(lock) __lll_cond_trylock (&(lock))
204 extern void __lll_lock_wait_private (int *futex
) attribute_hidden
;
205 extern void __lll_lock_wait (int *futex
, int private) attribute_hidden
;
206 extern int __lll_robust_lock_wait (int *futex
, int private) attribute_hidden
;
208 #define lll_lock(lock, private) \
210 int *__futex = &(lock); \
211 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
214 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
215 __lll_lock_wait_private (__futex); \
217 __lll_lock_wait (__futex, private); \
221 #define lll_robust_lock(lock, id, private) \
223 int *__futex = &(lock); \
225 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
227 __val = __lll_robust_lock_wait (__futex, private); \
231 #define lll_cond_lock(lock, private) \
233 int *__futex = &(lock); \
234 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
236 __lll_lock_wait (__futex, private); \
239 #define lll_robust_cond_lock(lock, id, private) \
241 int *__futex = &(lock); \
243 int __id = id | FUTEX_WAITERS; \
244 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
246 __val = __lll_robust_lock_wait (__futex, private); \
251 extern int __lll_timedlock_wait
252 (int *futex
, const struct timespec
*, int private) attribute_hidden
;
253 extern int __lll_robust_timedlock_wait
254 (int *futex
, const struct timespec
*, int private) attribute_hidden
;
256 #define lll_timedlock(lock, abstime, private) \
258 int *__futex = &(lock); \
260 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
262 __val = __lll_timedlock_wait (__futex, abstime, private); \
266 #define lll_robust_timedlock(lock, abstime, id, private) \
268 int *__futex = &(lock); \
270 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
272 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
276 #define lll_unlock(lock, private) \
278 int *__futex = &(lock); \
279 int __val = atomic_exchange_rel (__futex, 0); \
280 if (__glibc_unlikely (__val > 1)) \
281 lll_futex_wake (__futex, 1, private); \
284 #define lll_robust_unlock(lock, private) \
286 int *__futex = &(lock); \
287 int __val = atomic_exchange_rel (__futex, 0); \
288 if (__glibc_unlikely (__val & FUTEX_WAITERS)) \
289 lll_futex_wake (__futex, 1, private); \
292 #define lll_islocked(futex) \
296 /* Initializers for lock. */
297 #define LLL_LOCK_INITIALIZER (0)
298 #define LLL_LOCK_INITIALIZER_LOCKED (1)
300 /* The states of a lock are:
302 1 - taken by one user
303 >1 - taken by more users */
305 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
306 wakeup when the clone terminates. The memory location contains the
307 thread ID while the clone is running and is reset to zero
309 #define lll_wait_tid(tid) \
311 __typeof (tid) __tid; \
312 while ((__tid = (tid)) != 0) \
313 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
316 extern int __lll_timedwait_tid (int *, const struct timespec
*)
319 #define lll_timedwait_tid(tid, abstime) \
323 __res = __lll_timedwait_tid (&(tid), (abstime)); \
327 #endif /* lowlevellock.h */