Get rid of lll_robust_dead.
[glibc.git] / sysdeps / unix / sysv / linux / alpha / lowlevellock.h
blobbd727621adc04d6c94a8d71fcbf427f51ea90c34
1 /* Copyright (C) 2003-2014 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
21 #include <time.h>
22 #include <sys/param.h>
23 #include <bits/pthreadtypes.h>
24 #include <atomic.h>
25 #include <sysdep.h>
26 #include <kernel-features.h>
29 #define __NR_futex 394
30 #define FUTEX_WAIT 0
31 #define FUTEX_WAKE 1
32 #define FUTEX_REQUEUE 3
33 #define FUTEX_CMP_REQUEUE 4
34 #define FUTEX_WAKE_OP 5
35 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
36 #define FUTEX_LOCK_PI 6
37 #define FUTEX_UNLOCK_PI 7
38 #define FUTEX_TRYLOCK_PI 8
39 #define FUTEX_WAIT_BITSET 9
40 #define FUTEX_WAKE_BITSET 10
41 #define FUTEX_WAIT_REQUEUE_PI 11
42 #define FUTEX_CMP_REQUEUE_PI 12
43 #define FUTEX_PRIVATE_FLAG 128
44 #define FUTEX_CLOCK_REALTIME 256
46 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
48 /* Values for 'private' parameter of locking macros. Yes, the
49 definition seems to be backwards. But it is not. The bit will be
50 reversed before passing to the system call. */
51 #define LLL_PRIVATE 0
52 #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)
60 # else
61 # define __lll_private_flag(fl, private) \
62 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
63 # endif
64 #else
65 # ifdef __ASSUME_PRIVATE_FUTEX
66 # define __lll_private_flag(fl, private) \
67 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
68 # else
69 # define __lll_private_flag(fl, private) \
70 (__builtin_constant_p (private) \
71 ? ((private) == 0 \
72 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
73 : (fl)) \
74 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
75 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
76 # endif
77 #endif
80 #define lll_futex_wait(futexp, val, private) \
81 lll_futex_timed_wait (futexp, val, NULL, private)
83 #define lll_futex_timed_wait_bitset(futexp, val, timespec, clockbit, private) \
84 ({ \
85 INTERNAL_SYSCALL_DECL (__err); \
86 long int __ret; \
87 int __op = FUTEX_WAIT_BITSET | clockbit; \
89 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
90 __lll_private_flag (__op, private), \
91 (val), (timespec), NULL /* Unused. */, \
92 FUTEX_BITSET_MATCH_ANY); \
93 __ret; \
96 #define lll_futex_timed_wait(futexp, val, timespec, private) \
97 ({ \
98 INTERNAL_SYSCALL_DECL (__err); \
99 long int __ret; \
100 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
101 __lll_private_flag (FUTEX_WAIT, private), \
102 (val), (timespec)); \
103 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret; \
106 #define lll_futex_wake(futexp, nr, private) \
107 ({ \
108 INTERNAL_SYSCALL_DECL (__err); \
109 long int __ret; \
110 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
111 __lll_private_flag (FUTEX_WAKE, private), \
112 (nr), 0); \
113 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret; \
116 /* Returns non-zero if error happened, zero if success. */
117 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
118 ({ \
119 INTERNAL_SYSCALL_DECL (__err); \
120 long int __ret; \
121 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
122 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
123 (nr_wake), (nr_move), (mutex), (val)); \
124 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
127 /* Returns non-zero if error happened, zero if success. */
128 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
129 ({ \
130 INTERNAL_SYSCALL_DECL (__err); \
131 long int __ret; \
132 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
133 __lll_private_flag (FUTEX_WAKE_OP, private), \
134 (nr_wake), (nr_wake2), (futexp2), \
135 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
136 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
139 /* Priority Inheritance support. */
140 #define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
141 lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
143 #define lll_futex_timed_wait_requeue_pi(futexp, val, timespec, clockbit, \
144 mutex, private) \
145 ({ \
146 INTERNAL_SYSCALL_DECL (__err); \
147 int __op = FUTEX_WAIT_REQUEUE_PI | clockbit; \
149 INTERNAL_SYSCALL (futex, __err, 5, (futexp), \
150 __lll_private_flag (__op, private), \
151 (val), (timespec), mutex); \
154 #define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, val, priv) \
155 ({ \
156 INTERNAL_SYSCALL_DECL (__err); \
157 long int __ret; \
159 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
160 __lll_private_flag (FUTEX_CMP_REQUEUE_PI, priv),\
161 (nr_wake), (nr_move), (mutex), (val)); \
162 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
165 static inline int __attribute__((always_inline))
166 __lll_trylock(int *futex)
168 return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
170 #define lll_trylock(lock) __lll_trylock (&(lock))
173 static inline int __attribute__((always_inline))
174 __lll_cond_trylock(int *futex)
176 return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
178 #define lll_cond_trylock(lock) __lll_cond_trylock (&(lock))
181 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
182 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
183 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
185 static inline void __attribute__((always_inline))
186 __lll_lock(int *futex, int private)
188 if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
190 if (__builtin_constant_p (private) && private == LLL_PRIVATE)
191 __lll_lock_wait_private (futex);
192 else
193 __lll_lock_wait (futex, private);
196 #define lll_lock(futex, private) __lll_lock (&(futex), private)
199 static inline int __attribute__ ((always_inline))
200 __lll_robust_lock (int *futex, int id, int private)
202 int result = 0;
203 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
204 result = __lll_robust_lock_wait (futex, private);
205 return result;
207 #define lll_robust_lock(futex, id, private) \
208 __lll_robust_lock (&(futex), id, private)
211 static inline void __attribute__ ((always_inline))
212 __lll_cond_lock (int *futex, int private)
214 if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0)
215 __lll_lock_wait (futex, private);
217 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
220 #define lll_robust_cond_lock(futex, id, private) \
221 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
224 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
225 int private) attribute_hidden;
226 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
227 int private) attribute_hidden;
229 static inline int __attribute__ ((always_inline))
230 __lll_timedlock (int *futex, const struct timespec *abstime, int private)
232 int result = 0;
233 if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
234 result = __lll_timedlock_wait (futex, abstime, private);
235 return result;
237 #define lll_timedlock(futex, abstime, private) \
238 __lll_timedlock (&(futex), abstime, private)
241 static inline int __attribute__ ((always_inline))
242 __lll_robust_timedlock (int *futex, const struct timespec *abstime,
243 int id, int private)
245 int result = 0;
246 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
247 result = __lll_robust_timedlock_wait (futex, abstime, private);
248 return result;
250 #define lll_robust_timedlock(futex, abstime, id, private) \
251 __lll_robust_timedlock (&(futex), abstime, id, private)
254 #define __lll_unlock(futex, private) \
255 (void) \
256 ({ int *__futex = (futex); \
257 int __oldval = atomic_exchange_rel (__futex, 0); \
258 if (__builtin_expect (__oldval > 1, 0)) \
259 lll_futex_wake (__futex, 1, private); \
261 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
264 #define __lll_robust_unlock(futex, private) \
265 (void) \
266 ({ int *__futex = (futex); \
267 int __oldval = atomic_exchange_rel (__futex, 0); \
268 if (__builtin_expect (__oldval & FUTEX_WAITERS, 0)) \
269 lll_futex_wake (__futex, 1, private); \
271 #define lll_robust_unlock(futex, private) \
272 __lll_robust_unlock(&(futex), private)
275 #define lll_islocked(futex) \
276 (futex != 0)
278 /* Initializers for lock. */
279 #define LLL_LOCK_INITIALIZER (0)
280 #define LLL_LOCK_INITIALIZER_LOCKED (1)
283 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
284 wakeup when the clone terminates. The memory location contains the
285 thread ID while the clone is running and is reset to zero
286 afterwards. */
287 #define lll_wait_tid(tid) \
288 do { \
289 __typeof (tid) __tid; \
290 while ((__tid = (tid)) != 0) \
291 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
292 } while (0)
294 extern int __lll_timedwait_tid (int *, const struct timespec *)
295 attribute_hidden;
297 #define lll_timedwait_tid(tid, abstime) \
298 ({ \
299 int __res = 0; \
300 if ((tid) != 0) \
301 __res = __lll_timedwait_tid (&(tid), (abstime)); \
302 __res; \
305 #endif /* lowlevellock.h */