Get rid of lll_robust_dead.
[glibc.git] / sysdeps / unix / sysv / linux / powerpc / lowlevellock.h
blobd7e1e38cc6fc24890b64043fe0372b8084f2a3e2
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
22 #include <time.h>
23 #include <sys/param.h>
24 #include <bits/pthreadtypes.h>
25 #include <atomic.h>
26 #include <kernel-features.h>
28 #ifndef __NR_futex
29 # define __NR_futex 221
30 #endif
31 #define FUTEX_WAIT 0
32 #define FUTEX_WAKE 1
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. */
52 #define LLL_PRIVATE 0
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)
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
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) \
83 ({ \
84 INTERNAL_SYSCALL_DECL (__err); \
85 long int __ret; \
87 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
88 __lll_private_flag (FUTEX_WAIT, private), \
89 (val), (timespec)); \
90 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
93 #define lll_futex_timed_wait_bitset(futexp, val, timespec, clockbit, private) \
94 ({ \
95 INTERNAL_SYSCALL_DECL (__err); \
96 long int __ret; \
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) \
107 ({ \
108 INTERNAL_SYSCALL_DECL (__err); \
109 long int __ret; \
111 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
112 __lll_private_flag (FUTEX_WAKE, private), \
113 (nr), 0); \
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) \
119 ({ \
120 INTERNAL_SYSCALL_DECL (__err); \
121 long int __ret; \
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) \
131 ({ \
132 INTERNAL_SYSCALL_DECL (__err); \
133 long int __ret; \
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, \
147 mutex, private) \
148 ({ \
149 INTERNAL_SYSCALL_DECL (__err); \
150 long int __ret; \
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) \
160 ({ \
161 INTERNAL_SYSCALL_DECL (__err); \
162 long int __ret; \
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); \
171 #ifdef UP
172 # define __lll_acq_instr ""
173 # define __lll_rel_instr ""
174 #else
175 # define __lll_acq_instr "isync"
176 # ifdef _ARCH_PWR4
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
180 * safely use lwsync.
182 # define __lll_rel_instr "lwsync"
183 # else
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"
190 # endif
191 #endif
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) \
209 (void) ({ \
210 int *__futex = &(lock); \
211 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
212 0) != 0) \
214 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
215 __lll_lock_wait_private (__futex); \
216 else \
217 __lll_lock_wait (__futex, private); \
221 #define lll_robust_lock(lock, id, private) \
222 ({ \
223 int *__futex = &(lock); \
224 int __val = 0; \
225 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
226 0), 0)) \
227 __val = __lll_robust_lock_wait (__futex, private); \
228 __val; \
231 #define lll_cond_lock(lock, private) \
232 (void) ({ \
233 int *__futex = &(lock); \
234 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
235 0) != 0) \
236 __lll_lock_wait (__futex, private); \
239 #define lll_robust_cond_lock(lock, id, private) \
240 ({ \
241 int *__futex = &(lock); \
242 int __val = 0; \
243 int __id = id | FUTEX_WAITERS; \
244 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
245 0), 0)) \
246 __val = __lll_robust_lock_wait (__futex, private); \
247 __val; \
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) \
257 ({ \
258 int *__futex = &(lock); \
259 int __val = 0; \
260 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
261 0) != 0) \
262 __val = __lll_timedlock_wait (__futex, abstime, private); \
263 __val; \
266 #define lll_robust_timedlock(lock, abstime, id, private) \
267 ({ \
268 int *__futex = &(lock); \
269 int __val = 0; \
270 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
271 0), 0)) \
272 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
273 __val; \
276 #define lll_unlock(lock, private) \
277 ((void) ({ \
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) \
285 ((void) ({ \
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) \
293 (futex != 0)
296 /* Initializers for lock. */
297 #define LLL_LOCK_INITIALIZER (0)
298 #define LLL_LOCK_INITIALIZER_LOCKED (1)
300 /* The states of a lock are:
301 0 - untaken
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
308 afterwards. */
309 #define lll_wait_tid(tid) \
310 do { \
311 __typeof (tid) __tid; \
312 while ((__tid = (tid)) != 0) \
313 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
314 } while (0)
316 extern int __lll_timedwait_tid (int *, const struct timespec *)
317 attribute_hidden;
319 #define lll_timedwait_tid(tid, abstime) \
320 ({ \
321 int __res = 0; \
322 if ((tid) != 0) \
323 __res = __lll_timedwait_tid (&(tid), (abstime)); \
324 __res; \
327 #endif /* lowlevellock.h */