2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / unix / sysv / linux / ia64 / lowlevellock.h
blobada79851e2714223da372e9a318b5376af9f0e26
1 /* Copyright (C) 2003, 2004, 2006, 2007 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
23 #include <time.h>
24 #include <sys/param.h>
25 #include <bits/pthreadtypes.h>
26 #include <ia64intrin.h>
27 #include <atomic.h>
28 #include <kernel-features.h>
30 #define __NR_futex 1230
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_PRIVATE_FLAG 128
42 /* Values for 'private' parameter of locking macros. Yes, the
43 definition seems to be backwards. But it is not. The bit will be
44 reversed before passing to the system call. */
45 #define LLL_PRIVATE 0
46 #define LLL_SHARED FUTEX_PRIVATE_FLAG
49 #if !defined NOT_IN_libc || defined IS_IN_rtld
50 /* In libc.so or ld.so all futexes are private. */
51 # ifdef __ASSUME_PRIVATE_FUTEX
52 # define __lll_private_flag(fl, private) \
53 ((fl) | FUTEX_PRIVATE_FLAG)
54 # else
55 # define __lll_private_flag(fl, private) \
56 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
57 # endif
58 #else
59 # ifdef __ASSUME_PRIVATE_FUTEX
60 # define __lll_private_flag(fl, private) \
61 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
62 # else
63 # define __lll_private_flag(fl, private) \
64 (__builtin_constant_p (private) \
65 ? ((private) == 0 \
66 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
67 : (fl)) \
68 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
69 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
70 # endif
71 #endif
74 /* Delay in spinlock loop. */
75 #define BUSY_WAIT_NOP asm ("hint @pause")
77 #define lll_futex_wait(futex, val, private) \
78 lll_futex_timed_wait (futex, val, NULL, private)
80 #define lll_futex_timed_wait(ftx, val, timespec, private) \
81 ({ \
82 DO_INLINE_SYSCALL(futex, 4, (long) (ftx), \
83 __lll_private_flag (FUTEX_WAIT, private), \
84 (int) (val), (long) (timespec)); \
85 _r10 == -1 ? -_retval : _retval; \
88 #define lll_futex_wake(ftx, nr, private) \
89 ({ \
90 DO_INLINE_SYSCALL(futex, 3, (long) (ftx), \
91 __lll_private_flag (FUTEX_WAKE, private), \
92 (int) (nr)); \
93 _r10 == -1 ? -_retval : _retval; \
96 #define lll_robust_dead(futexv, private) \
97 do \
98 { \
99 int *__futexp = &(futexv); \
100 atomic_or (__futexp, FUTEX_OWNER_DIED); \
101 DO_INLINE_SYSCALL(futex, 3, (long) __futexp, \
102 __lll_private_flag (FUTEX_WAKE, private), 1); \
104 while (0)
106 /* Returns non-zero if error happened, zero if success. */
107 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val, private) \
108 ({ \
109 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), \
110 __lll_private_flag (FUTEX_CMP_REQUEUE, private), \
111 (int) (nr_wake), (int) (nr_move), (long) (mutex), \
112 (int) val); \
113 _r10 == -1; \
116 /* Returns non-zero if error happened, zero if success. */
117 #define lll_futex_wake_unlock(ftx, nr_wake, nr_wake2, ftx2, private) \
118 ({ \
119 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), \
120 __lll_private_flag (FUTEX_WAKE_OP, private), \
121 (int) (nr_wake), (int) (nr_wake2), (long) (ftx2), \
122 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
123 _r10 == -1; \
127 #define __lll_trylock(futex) \
128 (atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0)
129 #define lll_trylock(futex) __lll_trylock (&(futex))
132 #define __lll_robust_trylock(futex, id) \
133 (atomic_compare_and_exchange_val_acq (futex, id, 0) != 0)
134 #define lll_robust_trylock(futex, id) \
135 __lll_robust_trylock (&(futex), id)
138 #define __lll_cond_trylock(futex) \
139 (atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0)
140 #define lll_cond_trylock(futex) __lll_cond_trylock (&(futex))
143 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
144 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
145 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
148 #define __lll_lock(futex, private) \
149 ((void) ({ \
150 int *__futex = (futex); \
151 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, \
152 1, 0), 0)) \
154 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
155 __lll_lock_wait_private (__futex); \
156 else \
157 __lll_lock_wait (__futex, private); \
160 #define lll_lock(futex, private) __lll_lock (&(futex), private)
163 #define __lll_robust_lock(futex, id, private) \
164 ({ \
165 int *__futex = (futex); \
166 int __val = 0; \
168 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
169 0), 0)) \
170 __val = __lll_robust_lock_wait (__futex, private); \
171 __val; \
173 #define lll_robust_lock(futex, id, private) \
174 __lll_robust_lock (&(futex), id, private)
177 #define __lll_cond_lock(futex, private) \
178 ((void) ({ \
179 int *__futex = (futex); \
180 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, 2, \
181 0), 0)) \
182 __lll_lock_wait (__futex, private); \
184 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
187 #define __lll_robust_cond_lock(futex, id, private) \
188 ({ \
189 int *__futex = (futex); \
190 int __val = 0; \
191 int __id = (id) | FUTEX_WAITERS; \
193 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, \
194 __id, 0), 0)) \
195 __val = __lll_robust_lock_wait (__futex, private); \
196 __val; \
198 #define lll_robust_cond_lock(futex, id, private) \
199 __lll_robust_cond_lock (&(futex), id, private)
202 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
203 int private) attribute_hidden;
204 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
205 int private) attribute_hidden;
208 #define __lll_timedlock(futex, abstime, private) \
209 ({ \
210 int *__futex = (futex); \
211 int __val = 0; \
213 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, 1, \
214 0), 0)) \
215 __val = __lll_timedlock_wait (__futex, abstime, private); \
216 __val; \
218 #define lll_timedlock(futex, abstime, private) \
219 __lll_timedlock (&(futex), abstime, private)
222 #define __lll_robust_timedlock(futex, abstime, id, private) \
223 ({ \
224 int *__futex = (futex); \
225 int __val = 0; \
227 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
228 0), 0)) \
229 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
230 __val; \
232 #define lll_robust_timedlock(futex, abstime, id, private) \
233 __lll_robust_timedlock (&(futex), abstime, id, private)
236 #define __lll_unlock(futex, private) \
237 ((void) ({ \
238 int *__futex = (futex); \
239 int __val = atomic_exchange_rel (__futex, 0); \
241 if (__builtin_expect (__val > 1, 0)) \
242 lll_futex_wake (__futex, 1, private); \
244 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
247 #define __lll_robust_unlock(futex, private) \
248 ((void) ({ \
249 int *__futex = (futex); \
250 int __val = atomic_exchange_rel (__futex, 0); \
252 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
253 lll_futex_wake (__futex, 1, private); \
255 #define lll_robust_unlock(futex, private) \
256 __lll_robust_unlock(&(futex), private)
259 #define lll_islocked(futex) \
260 (futex != 0)
262 /* Initializers for lock. */
263 #define LLL_LOCK_INITIALIZER (0)
264 #define LLL_LOCK_INITIALIZER_LOCKED (1)
266 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
267 wakeup when the clone terminates. The memory location contains the
268 thread ID while the clone is running and is reset to zero
269 afterwards. */
270 #define lll_wait_tid(tid) \
271 do \
273 __typeof (tid) __tid; \
274 while ((__tid = (tid)) != 0) \
275 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
277 while (0)
279 extern int __lll_timedwait_tid (int *, const struct timespec *)
280 attribute_hidden;
282 #define lll_timedwait_tid(tid, abstime) \
283 ({ \
284 int __res = 0; \
285 if ((tid) != 0) \
286 __res = __lll_timedwait_tid (&(tid), (abstime)); \
287 __res; \
290 #endif /* lowlevellock.h */