Update copyright notices with scripts/update-copyrights
[glibc.git] / ports / sysdeps / unix / sysv / linux / ia64 / nptl / lowlevellock.h
blob0105972b40e8b6d41085f0864b4637462132b985
1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.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 <ia64intrin.h>
26 #include <atomic.h>
27 #include <kernel-features.h>
29 #define __NR_futex 1230
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 /* Delay in spinlock loop. */
81 #define BUSY_WAIT_NOP asm ("hint @pause")
83 #define lll_futex_wait(futex, val, private) \
84 lll_futex_timed_wait (futex, val, NULL, private)
86 #define lll_futex_timed_wait(ftx, val, timespec, private) \
87 ({ \
88 DO_INLINE_SYSCALL(futex, 4, (long) (ftx), \
89 __lll_private_flag (FUTEX_WAIT, private), \
90 (int) (val), (long) (timespec)); \
91 _r10 == -1 ? -_retval : _retval; \
94 #define lll_futex_timed_wait_bitset(ftx, val, timespec, clockbit, private) \
95 ({ \
96 int __op = FUTEX_WAIT_BITSET | (clockbit); \
98 DO_INLINE_SYSCALL(futex, 4, (long) (ftx), \
99 __lll_private_flag (__op, private), \
100 (int) (val), (long) (timespec), NULL /* Unused. */, \
101 FUTEX_BITSET_MATCH_ANY); \
102 _r10 == -1 ? -_retval : _retval; \
105 #define lll_futex_wake(ftx, nr, private) \
106 ({ \
107 DO_INLINE_SYSCALL(futex, 3, (long) (ftx), \
108 __lll_private_flag (FUTEX_WAKE, private), \
109 (int) (nr)); \
110 _r10 == -1 ? -_retval : _retval; \
113 #define lll_robust_dead(futexv, private) \
114 do \
116 int *__futexp = &(futexv); \
117 atomic_or (__futexp, FUTEX_OWNER_DIED); \
118 DO_INLINE_SYSCALL(futex, 3, (long) __futexp, \
119 __lll_private_flag (FUTEX_WAKE, private), 1); \
121 while (0)
123 /* Returns non-zero if error happened, zero if success. */
124 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val, private) \
125 ({ \
126 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), \
127 __lll_private_flag (FUTEX_CMP_REQUEUE, private), \
128 (int) (nr_wake), (int) (nr_move), (long) (mutex), \
129 (int) val); \
130 _r10 == -1; \
133 /* Returns non-zero if error happened, zero if success. */
134 #define lll_futex_wake_unlock(ftx, nr_wake, nr_wake2, ftx2, private) \
135 ({ \
136 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), \
137 __lll_private_flag (FUTEX_WAKE_OP, private), \
138 (int) (nr_wake), (int) (nr_wake2), (long) (ftx2), \
139 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
140 _r10 == -1; \
143 /* Priority Inheritance support. */
144 #define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
145 lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
147 #define lll_futex_timed_wait_requeue_pi(futexp, val, timespec, clockbit, \
148 mutex, private) \
149 ({ \
150 int __op = FUTEX_WAIT_REQUEUE_PI | (clockbit); \
152 DO_INLINE_SYSCALL(futex, 5, (long) (futexp), \
153 __lll_private_flag (__op, private), \
154 (val), (timespec), mutex); \
155 _r10 == -1; \
158 #define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, val, priv) \
159 ({ \
160 DO_INLINE_SYSCALL(futex, 6, (long) (futexp), \
161 __lll_private_flag (FUTEX_CMP_REQUEUE_PI, priv), \
162 (nr_wake), (nr_move), (mutex), (val)); \
163 _r10 == -1 ? -_retval : _retval; \
167 #define __lll_trylock(futex) \
168 (atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0)
169 #define lll_trylock(futex) __lll_trylock (&(futex))
172 #define __lll_robust_trylock(futex, id) \
173 (atomic_compare_and_exchange_val_acq (futex, id, 0) != 0)
174 #define lll_robust_trylock(futex, id) \
175 __lll_robust_trylock (&(futex), id)
178 #define __lll_cond_trylock(futex) \
179 (atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0)
180 #define lll_cond_trylock(futex) __lll_cond_trylock (&(futex))
183 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
184 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
185 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
188 #define __lll_lock(futex, private) \
189 ((void) ({ \
190 int *__futex = (futex); \
191 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, \
192 1, 0), 0)) \
194 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
195 __lll_lock_wait_private (__futex); \
196 else \
197 __lll_lock_wait (__futex, private); \
200 #define lll_lock(futex, private) __lll_lock (&(futex), private)
203 #define __lll_robust_lock(futex, id, private) \
204 ({ \
205 int *__futex = (futex); \
206 int __val = 0; \
208 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
209 0), 0)) \
210 __val = __lll_robust_lock_wait (__futex, private); \
211 __val; \
213 #define lll_robust_lock(futex, id, private) \
214 __lll_robust_lock (&(futex), id, private)
217 #define __lll_cond_lock(futex, private) \
218 ((void) ({ \
219 int *__futex = (futex); \
220 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, 2, \
221 0), 0)) \
222 __lll_lock_wait (__futex, private); \
224 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
227 #define __lll_robust_cond_lock(futex, id, private) \
228 ({ \
229 int *__futex = (futex); \
230 int __val = 0; \
231 int __id = (id) | FUTEX_WAITERS; \
233 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, \
234 __id, 0), 0)) \
235 __val = __lll_robust_lock_wait (__futex, private); \
236 __val; \
238 #define lll_robust_cond_lock(futex, id, private) \
239 __lll_robust_cond_lock (&(futex), id, private)
242 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
243 int private) attribute_hidden;
244 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
245 int private) attribute_hidden;
248 #define __lll_timedlock(futex, abstime, private) \
249 ({ \
250 int *__futex = (futex); \
251 int __val = 0; \
253 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, 1, \
254 0), 0)) \
255 __val = __lll_timedlock_wait (__futex, abstime, private); \
256 __val; \
258 #define lll_timedlock(futex, abstime, private) \
259 __lll_timedlock (&(futex), abstime, private)
262 #define __lll_robust_timedlock(futex, abstime, id, private) \
263 ({ \
264 int *__futex = (futex); \
265 int __val = 0; \
267 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
268 0), 0)) \
269 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
270 __val; \
272 #define lll_robust_timedlock(futex, abstime, id, private) \
273 __lll_robust_timedlock (&(futex), abstime, id, private)
276 #define __lll_unlock(futex, private) \
277 ((void) ({ \
278 int *__futex = (futex); \
279 int __val = atomic_exchange_rel (__futex, 0); \
281 if (__builtin_expect (__val > 1, 0)) \
282 lll_futex_wake (__futex, 1, private); \
284 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
287 #define __lll_robust_unlock(futex, private) \
288 ((void) ({ \
289 int *__futex = (futex); \
290 int __val = atomic_exchange_rel (__futex, 0); \
292 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
293 lll_futex_wake (__futex, 1, private); \
295 #define lll_robust_unlock(futex, private) \
296 __lll_robust_unlock(&(futex), private)
299 #define lll_islocked(futex) \
300 (futex != 0)
302 /* Initializers for lock. */
303 #define LLL_LOCK_INITIALIZER (0)
304 #define LLL_LOCK_INITIALIZER_LOCKED (1)
306 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
307 wakeup when the clone terminates. The memory location contains the
308 thread ID while the clone is running and is reset to zero
309 afterwards. */
310 #define lll_wait_tid(tid) \
311 do \
313 __typeof (tid) __tid; \
314 while ((__tid = (tid)) != 0) \
315 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
317 while (0)
319 extern int __lll_timedwait_tid (int *, const struct timespec *)
320 attribute_hidden;
322 #define lll_timedwait_tid(tid, abstime) \
323 ({ \
324 int __res = 0; \
325 if ((tid) != 0) \
326 __res = __lll_timedwait_tid (&(tid), (abstime)); \
327 __res; \
330 #endif /* lowlevellock.h */