Add Changelog ...
[glibc.git] / sysdeps / unix / sysv / linux / ia64 / nptl / lowlevellock.h
blob24ed47e2264ebfda24921d9cc68657531d3314a1
1 /* Copyright (C) 2003, 2004, 2006-2008, 2009 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_PRIVATE_FLAG 128
42 #define FUTEX_CLOCK_REALTIME 256
44 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
46 /* Values for 'private' parameter of locking macros. Yes, the
47 definition seems to be backwards. But it is not. The bit will be
48 reversed before passing to the system call. */
49 #define LLL_PRIVATE 0
50 #define LLL_SHARED FUTEX_PRIVATE_FLAG
53 #if !defined NOT_IN_libc || defined IS_IN_rtld
54 /* In libc.so or ld.so all futexes are private. */
55 # ifdef __ASSUME_PRIVATE_FUTEX
56 # define __lll_private_flag(fl, private) \
57 ((fl) | FUTEX_PRIVATE_FLAG)
58 # else
59 # define __lll_private_flag(fl, private) \
60 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
61 # endif
62 #else
63 # ifdef __ASSUME_PRIVATE_FUTEX
64 # define __lll_private_flag(fl, private) \
65 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
66 # else
67 # define __lll_private_flag(fl, private) \
68 (__builtin_constant_p (private) \
69 ? ((private) == 0 \
70 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
71 : (fl)) \
72 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
73 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
74 # endif
75 #endif
78 /* Delay in spinlock loop. */
79 #define BUSY_WAIT_NOP asm ("hint @pause")
81 #define lll_futex_wait(futex, val, private) \
82 lll_futex_timed_wait (futex, val, NULL, private)
84 #define lll_futex_timed_wait(ftx, val, timespec, private) \
85 ({ \
86 DO_INLINE_SYSCALL(futex, 4, (long) (ftx), \
87 __lll_private_flag (FUTEX_WAIT, private), \
88 (int) (val), (long) (timespec)); \
89 _r10 == -1 ? -_retval : _retval; \
92 #define lll_futex_wake(ftx, nr, private) \
93 ({ \
94 DO_INLINE_SYSCALL(futex, 3, (long) (ftx), \
95 __lll_private_flag (FUTEX_WAKE, private), \
96 (int) (nr)); \
97 _r10 == -1 ? -_retval : _retval; \
100 #define lll_robust_dead(futexv, private) \
101 do \
103 int *__futexp = &(futexv); \
104 atomic_or (__futexp, FUTEX_OWNER_DIED); \
105 DO_INLINE_SYSCALL(futex, 3, (long) __futexp, \
106 __lll_private_flag (FUTEX_WAKE, private), 1); \
108 while (0)
110 /* Returns non-zero if error happened, zero if success. */
111 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val, private) \
112 ({ \
113 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), \
114 __lll_private_flag (FUTEX_CMP_REQUEUE, private), \
115 (int) (nr_wake), (int) (nr_move), (long) (mutex), \
116 (int) val); \
117 _r10 == -1; \
120 /* Returns non-zero if error happened, zero if success. */
121 #define lll_futex_wake_unlock(ftx, nr_wake, nr_wake2, ftx2, private) \
122 ({ \
123 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), \
124 __lll_private_flag (FUTEX_WAKE_OP, private), \
125 (int) (nr_wake), (int) (nr_wake2), (long) (ftx2), \
126 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
127 _r10 == -1; \
131 #define __lll_trylock(futex) \
132 (atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0)
133 #define lll_trylock(futex) __lll_trylock (&(futex))
136 #define __lll_robust_trylock(futex, id) \
137 (atomic_compare_and_exchange_val_acq (futex, id, 0) != 0)
138 #define lll_robust_trylock(futex, id) \
139 __lll_robust_trylock (&(futex), id)
142 #define __lll_cond_trylock(futex) \
143 (atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0)
144 #define lll_cond_trylock(futex) __lll_cond_trylock (&(futex))
147 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
148 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
149 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
152 #define __lll_lock(futex, private) \
153 ((void) ({ \
154 int *__futex = (futex); \
155 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, \
156 1, 0), 0)) \
158 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
159 __lll_lock_wait_private (__futex); \
160 else \
161 __lll_lock_wait (__futex, private); \
164 #define lll_lock(futex, private) __lll_lock (&(futex), private)
167 #define __lll_robust_lock(futex, id, private) \
168 ({ \
169 int *__futex = (futex); \
170 int __val = 0; \
172 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
173 0), 0)) \
174 __val = __lll_robust_lock_wait (__futex, private); \
175 __val; \
177 #define lll_robust_lock(futex, id, private) \
178 __lll_robust_lock (&(futex), id, private)
181 #define __lll_cond_lock(futex, private) \
182 ((void) ({ \
183 int *__futex = (futex); \
184 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, 2, \
185 0), 0)) \
186 __lll_lock_wait (__futex, private); \
188 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
191 #define __lll_robust_cond_lock(futex, id, private) \
192 ({ \
193 int *__futex = (futex); \
194 int __val = 0; \
195 int __id = (id) | FUTEX_WAITERS; \
197 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, \
198 __id, 0), 0)) \
199 __val = __lll_robust_lock_wait (__futex, private); \
200 __val; \
202 #define lll_robust_cond_lock(futex, id, private) \
203 __lll_robust_cond_lock (&(futex), id, private)
206 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
207 int private) attribute_hidden;
208 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
209 int private) attribute_hidden;
212 #define __lll_timedlock(futex, abstime, private) \
213 ({ \
214 int *__futex = (futex); \
215 int __val = 0; \
217 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, 1, \
218 0), 0)) \
219 __val = __lll_timedlock_wait (__futex, abstime, private); \
220 __val; \
222 #define lll_timedlock(futex, abstime, private) \
223 __lll_timedlock (&(futex), abstime, private)
226 #define __lll_robust_timedlock(futex, abstime, id, private) \
227 ({ \
228 int *__futex = (futex); \
229 int __val = 0; \
231 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
232 0), 0)) \
233 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
234 __val; \
236 #define lll_robust_timedlock(futex, abstime, id, private) \
237 __lll_robust_timedlock (&(futex), abstime, id, private)
240 #define __lll_unlock(futex, private) \
241 ((void) ({ \
242 int *__futex = (futex); \
243 int __val = atomic_exchange_rel (__futex, 0); \
245 if (__builtin_expect (__val > 1, 0)) \
246 lll_futex_wake (__futex, 1, private); \
248 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
251 #define __lll_robust_unlock(futex, private) \
252 ((void) ({ \
253 int *__futex = (futex); \
254 int __val = atomic_exchange_rel (__futex, 0); \
256 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
257 lll_futex_wake (__futex, 1, private); \
259 #define lll_robust_unlock(futex, private) \
260 __lll_robust_unlock(&(futex), private)
263 #define lll_islocked(futex) \
264 (futex != 0)
266 /* Initializers for lock. */
267 #define LLL_LOCK_INITIALIZER (0)
268 #define LLL_LOCK_INITIALIZER_LOCKED (1)
270 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
271 wakeup when the clone terminates. The memory location contains the
272 thread ID while the clone is running and is reset to zero
273 afterwards. */
274 #define lll_wait_tid(tid) \
275 do \
277 __typeof (tid) __tid; \
278 while ((__tid = (tid)) != 0) \
279 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
281 while (0)
283 extern int __lll_timedwait_tid (int *, const struct timespec *)
284 attribute_hidden;
286 #define lll_timedwait_tid(tid, abstime) \
287 ({ \
288 int __res = 0; \
289 if ((tid) != 0) \
290 __res = __lll_timedwait_tid (&(tid), (abstime)); \
291 __res; \
294 #endif /* lowlevellock.h */