* resolv/nss_dns/dns-host.c (_nss_dns_gethostbyname3_r): Recognize
[glibc.git] / nptl / sysdeps / unix / sysv / linux / ia64 / lowlevellock.h
blob9c4d16f453e6daafd42b4c3654944385b399ef6d
1 /* Copyright (C) 2003, 2004, 2006, 2007, 2008 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_WAIT_BITSET 9
41 #define FUTEX_WAKE_BITSET 10
42 #define FUTEX_PRIVATE_FLAG 128
44 /* Values for 'private' parameter of locking macros. Yes, the
45 definition seems to be backwards. But it is not. The bit will be
46 reversed before passing to the system call. */
47 #define LLL_PRIVATE 0
48 #define LLL_SHARED FUTEX_PRIVATE_FLAG
51 #if !defined NOT_IN_libc || defined IS_IN_rtld
52 /* In libc.so or ld.so all futexes are private. */
53 # ifdef __ASSUME_PRIVATE_FUTEX
54 # define __lll_private_flag(fl, private) \
55 ((fl) | FUTEX_PRIVATE_FLAG)
56 # else
57 # define __lll_private_flag(fl, private) \
58 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
59 # endif
60 #else
61 # ifdef __ASSUME_PRIVATE_FUTEX
62 # define __lll_private_flag(fl, private) \
63 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
64 # else
65 # define __lll_private_flag(fl, private) \
66 (__builtin_constant_p (private) \
67 ? ((private) == 0 \
68 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
69 : (fl)) \
70 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
71 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
72 # endif
73 #endif
76 /* Delay in spinlock loop. */
77 #define BUSY_WAIT_NOP asm ("hint @pause")
79 #define lll_futex_wait(futex, val, private) \
80 lll_futex_timed_wait (futex, val, NULL, private)
82 #define lll_futex_timed_wait(ftx, val, timespec, private) \
83 ({ \
84 DO_INLINE_SYSCALL(futex, 4, (long) (ftx), \
85 __lll_private_flag (FUTEX_WAIT, private), \
86 (int) (val), (long) (timespec)); \
87 _r10 == -1 ? -_retval : _retval; \
90 #define lll_futex_wake(ftx, nr, private) \
91 ({ \
92 DO_INLINE_SYSCALL(futex, 3, (long) (ftx), \
93 __lll_private_flag (FUTEX_WAKE, private), \
94 (int) (nr)); \
95 _r10 == -1 ? -_retval : _retval; \
98 #define lll_robust_dead(futexv, private) \
99 do \
101 int *__futexp = &(futexv); \
102 atomic_or (__futexp, FUTEX_OWNER_DIED); \
103 DO_INLINE_SYSCALL(futex, 3, (long) __futexp, \
104 __lll_private_flag (FUTEX_WAKE, private), 1); \
106 while (0)
108 /* Returns non-zero if error happened, zero if success. */
109 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val, private) \
110 ({ \
111 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), \
112 __lll_private_flag (FUTEX_CMP_REQUEUE, private), \
113 (int) (nr_wake), (int) (nr_move), (long) (mutex), \
114 (int) val); \
115 _r10 == -1; \
118 /* Returns non-zero if error happened, zero if success. */
119 #define lll_futex_wake_unlock(ftx, nr_wake, nr_wake2, ftx2, private) \
120 ({ \
121 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), \
122 __lll_private_flag (FUTEX_WAKE_OP, private), \
123 (int) (nr_wake), (int) (nr_wake2), (long) (ftx2), \
124 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
125 _r10 == -1; \
129 #define __lll_trylock(futex) \
130 (atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0)
131 #define lll_trylock(futex) __lll_trylock (&(futex))
134 #define __lll_robust_trylock(futex, id) \
135 (atomic_compare_and_exchange_val_acq (futex, id, 0) != 0)
136 #define lll_robust_trylock(futex, id) \
137 __lll_robust_trylock (&(futex), id)
140 #define __lll_cond_trylock(futex) \
141 (atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0)
142 #define lll_cond_trylock(futex) __lll_cond_trylock (&(futex))
145 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
146 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
147 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
150 #define __lll_lock(futex, private) \
151 ((void) ({ \
152 int *__futex = (futex); \
153 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, \
154 1, 0), 0)) \
156 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
157 __lll_lock_wait_private (__futex); \
158 else \
159 __lll_lock_wait (__futex, private); \
162 #define lll_lock(futex, private) __lll_lock (&(futex), private)
165 #define __lll_robust_lock(futex, id, private) \
166 ({ \
167 int *__futex = (futex); \
168 int __val = 0; \
170 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
171 0), 0)) \
172 __val = __lll_robust_lock_wait (__futex, private); \
173 __val; \
175 #define lll_robust_lock(futex, id, private) \
176 __lll_robust_lock (&(futex), id, private)
179 #define __lll_cond_lock(futex, private) \
180 ((void) ({ \
181 int *__futex = (futex); \
182 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, 2, \
183 0), 0)) \
184 __lll_lock_wait (__futex, private); \
186 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
189 #define __lll_robust_cond_lock(futex, id, private) \
190 ({ \
191 int *__futex = (futex); \
192 int __val = 0; \
193 int __id = (id) | FUTEX_WAITERS; \
195 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, \
196 __id, 0), 0)) \
197 __val = __lll_robust_lock_wait (__futex, private); \
198 __val; \
200 #define lll_robust_cond_lock(futex, id, private) \
201 __lll_robust_cond_lock (&(futex), id, private)
204 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
205 int private) attribute_hidden;
206 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
207 int private) attribute_hidden;
210 #define __lll_timedlock(futex, abstime, private) \
211 ({ \
212 int *__futex = (futex); \
213 int __val = 0; \
215 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, 1, \
216 0), 0)) \
217 __val = __lll_timedlock_wait (__futex, abstime, private); \
218 __val; \
220 #define lll_timedlock(futex, abstime, private) \
221 __lll_timedlock (&(futex), abstime, private)
224 #define __lll_robust_timedlock(futex, abstime, id, private) \
225 ({ \
226 int *__futex = (futex); \
227 int __val = 0; \
229 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
230 0), 0)) \
231 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
232 __val; \
234 #define lll_robust_timedlock(futex, abstime, id, private) \
235 __lll_robust_timedlock (&(futex), abstime, id, private)
238 #define __lll_unlock(futex, private) \
239 ((void) ({ \
240 int *__futex = (futex); \
241 int __val = atomic_exchange_rel (__futex, 0); \
243 if (__builtin_expect (__val > 1, 0)) \
244 lll_futex_wake (__futex, 1, private); \
246 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
249 #define __lll_robust_unlock(futex, private) \
250 ((void) ({ \
251 int *__futex = (futex); \
252 int __val = atomic_exchange_rel (__futex, 0); \
254 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
255 lll_futex_wake (__futex, 1, private); \
257 #define lll_robust_unlock(futex, private) \
258 __lll_robust_unlock(&(futex), private)
261 #define lll_islocked(futex) \
262 (futex != 0)
264 /* Initializers for lock. */
265 #define LLL_LOCK_INITIALIZER (0)
266 #define LLL_LOCK_INITIALIZER_LOCKED (1)
268 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
269 wakeup when the clone terminates. The memory location contains the
270 thread ID while the clone is running and is reset to zero
271 afterwards. */
272 #define lll_wait_tid(tid) \
273 do \
275 __typeof (tid) __tid; \
276 while ((__tid = (tid)) != 0) \
277 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
279 while (0)
281 extern int __lll_timedwait_tid (int *, const struct timespec *)
282 attribute_hidden;
284 #define lll_timedwait_tid(tid, abstime) \
285 ({ \
286 int __res = 0; \
287 if ((tid) != 0) \
288 __res = __lll_timedwait_tid (&(tid), (abstime)); \
289 __res; \
292 #endif /* lowlevellock.h */