1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Martin Schwidefsky <schwidefsky@de.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>
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
40 #define FUTEX_WAIT_REQUEUE_PI 11
41 #define FUTEX_CMP_REQUEUE_PI 12
42 #define FUTEX_PRIVATE_FLAG 128
43 #define FUTEX_CLOCK_REALTIME 256
45 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
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))))
78 #define lll_futex_wait(futex, val, private) \
79 lll_futex_timed_wait (futex, val, NULL, private)
81 #define lll_futex_timed_wait(futexp, val, timespec, private) \
83 INTERNAL_SYSCALL_DECL (__err); \
85 INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
86 __lll_private_flag (FUTEX_WAIT, private), \
90 #define lll_futex_timed_wait_bitset(futexp, val, timespec, clockbit, private) \
92 INTERNAL_SYSCALL_DECL (__err); \
93 int __op = FUTEX_WAIT_BITSET | clockbit; \
95 INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
96 __lll_private_flag (__op, private), \
97 (val), (timespec), NULL /* Unused. */, \
98 FUTEX_BITSET_MATCH_ANY); \
101 #define lll_futex_wake(futexp, nr, private) \
103 INTERNAL_SYSCALL_DECL (__err); \
105 INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
106 __lll_private_flag (FUTEX_WAKE, private), \
110 #define lll_robust_dead(futexv, private) \
113 int *__futexp = &(futexv); \
115 atomic_or (__futexp, FUTEX_OWNER_DIED); \
116 lll_futex_wake (__futexp, 1, private); \
121 /* Returns non-zero if error happened, zero if success. */
122 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
124 INTERNAL_SYSCALL_DECL (__err); \
127 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
128 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
129 (nr_wake), (nr_move), (mutex), (val)); \
130 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
133 /* Returns non-zero if error happened, zero if success. */
134 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
136 INTERNAL_SYSCALL_DECL (__err); \
139 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
140 __lll_private_flag (FUTEX_WAKE_OP, private), \
141 (nr_wake), (nr_wake2), (futexp2), \
142 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
143 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
146 /* Priority Inheritance support. */
147 #define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
148 lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
150 #define lll_futex_timed_wait_requeue_pi(futexp, val, timespec, clockbit, \
153 INTERNAL_SYSCALL_DECL (__err); \
154 int __op = FUTEX_WAIT_REQUEUE_PI | clockbit; \
156 INTERNAL_SYSCALL (futex, __err, 5, (futexp), \
157 __lll_private_flag (__op, private), \
158 (val), (timespec), mutex); \
161 #define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, val, priv) \
163 INTERNAL_SYSCALL_DECL (__err); \
166 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
167 __lll_private_flag (FUTEX_CMP_REQUEUE_PI, priv),\
168 (nr_wake), (nr_move), (mutex), (val)); \
169 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
172 #define lll_compare_and_swap(futex, oldval, newval, operation) \
174 __typeof (futex) __futex = (futex); \
175 __asm __volatile (" l %1,%0\n" \
176 "0: " operation "\n" \
180 : "=Q" (*__futex), "=&d" (oldval), "=&d" (newval) \
181 : "m" (*__futex) : "cc", "memory" ); \
186 __attribute__ ((always_inline
))
187 __lll_trylock (int *futex
)
191 __asm
__volatile ("cs %0,%3,%1"
192 : "=d" (old
), "=Q" (*futex
)
193 : "0" (0), "d" (1), "m" (*futex
) : "cc", "memory" );
196 #define lll_trylock(futex) __lll_trylock (&(futex))
200 __attribute__ ((always_inline
))
201 __lll_cond_trylock (int *futex
)
205 __asm
__volatile ("cs %0,%3,%1"
206 : "=d" (old
), "=Q" (*futex
)
207 : "0" (0), "d" (2), "m" (*futex
) : "cc", "memory" );
210 #define lll_cond_trylock(futex) __lll_cond_trylock (&(futex))
214 __attribute__ ((always_inline
))
215 __lll_robust_trylock (int *futex
, int id
)
219 __asm
__volatile ("cs %0,%3,%1"
220 : "=d" (old
), "=Q" (*futex
)
221 : "0" (0), "d" (id
), "m" (*futex
) : "cc", "memory" );
224 #define lll_robust_trylock(futex, id) \
225 __lll_robust_trylock (&(futex), id)
228 extern void __lll_lock_wait_private (int *futex
) attribute_hidden
;
229 extern void __lll_lock_wait (int *futex
, int private) attribute_hidden
;
230 extern int __lll_robust_lock_wait (int *futex
, int private) attribute_hidden
;
233 __attribute__ ((always_inline
))
234 __lll_lock (int *futex
, int private)
236 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (futex
, 1, 0), 0))
238 if (__builtin_constant_p (private) && private == LLL_PRIVATE
)
239 __lll_lock_wait_private (futex
);
241 __lll_lock_wait (futex
, private);
244 #define lll_lock(futex, private) __lll_lock (&(futex), private)
247 __attribute__ ((always_inline
))
248 __lll_robust_lock (int *futex
, int id
, int private)
251 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (futex
, id
, 0),
253 result
= __lll_robust_lock_wait (futex
, private);
256 #define lll_robust_lock(futex, id, private) \
257 __lll_robust_lock (&(futex), id, private)
260 __attribute__ ((always_inline
))
261 __lll_cond_lock (int *futex
, int private)
263 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (futex
, 2, 0), 0))
264 __lll_lock_wait (futex
, private);
266 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
268 #define lll_robust_cond_lock(futex, id, private) \
269 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
271 extern int __lll_timedlock_wait
272 (int *futex
, const struct timespec
*, int private) attribute_hidden
;
273 extern int __lll_robust_timedlock_wait
274 (int *futex
, const struct timespec
*, int private) attribute_hidden
;
277 __attribute__ ((always_inline
))
278 __lll_timedlock (int *futex
, const struct timespec
*abstime
, int private)
281 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (futex
, 1, 0), 0))
282 result
= __lll_timedlock_wait (futex
, abstime
, private);
285 #define lll_timedlock(futex, abstime, private) \
286 __lll_timedlock (&(futex), abstime, private)
289 __attribute__ ((always_inline
))
290 __lll_robust_timedlock (int *futex
, const struct timespec
*abstime
,
294 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (futex
, id
, 0),
296 result
= __lll_robust_timedlock_wait (futex
, abstime
, private);
299 #define lll_robust_timedlock(futex, abstime, id, private) \
300 __lll_robust_timedlock (&(futex), abstime, id, private)
303 #define __lll_unlock(futex, private) \
307 int *__futexp = (futex); \
309 lll_compare_and_swap (__futexp, __oldval, __newval, "slr %2,%2"); \
310 if (__builtin_expect (__oldval > 1, 0)) \
311 lll_futex_wake (__futexp, 1, private); \
313 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
316 #define __lll_robust_unlock(futex, private) \
320 int *__futexp = (futex); \
322 lll_compare_and_swap (__futexp, __oldval, __newval, "slr %2,%2"); \
323 if (__builtin_expect (__oldval & FUTEX_WAITERS, 0)) \
324 lll_futex_wake (__futexp, 1, private); \
326 #define lll_robust_unlock(futex, private) \
327 __lll_robust_unlock(&(futex), private)
329 #define lll_islocked(futex) \
333 /* Initializers for lock. */
334 #define LLL_LOCK_INITIALIZER (0)
335 #define LLL_LOCK_INITIALIZER_LOCKED (1)
337 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
338 wakeup when the clone terminates. The memory location contains the
339 thread ID while the clone is running and is reset to zero
341 #define __lll_wait_tid(ptid) \
346 while ((__tid = *ptid) != 0) \
347 lll_futex_wait (ptid, __tid, LLL_SHARED); \
350 #define lll_wait_tid(tid) __lll_wait_tid(&(tid))
352 extern int __lll_timedwait_tid (int *, const struct timespec
*)
355 #define lll_timedwait_tid(tid, abstime) \
359 __res = __lll_timedwait_tid (&(tid), (abstime)); \
363 #endif /* lowlevellock.h */