* include/atomic.h (atomic_and, atomic_or): Define.
[glibc.git] / nptl / sysdeps / unix / sysv / linux / ia64 / lowlevellock.h
blobece9a7fc729cf0cc008609d35ba45997b0e353f9
1 /* Copyright (C) 2003, 2004, 2006 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>
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)
37 /* Delay in spinlock loop. */
38 #define BUSY_WAIT_NOP asm ("hint @pause")
40 /* Initializer for compatibility lock. */
41 #define LLL_MUTEX_LOCK_INITIALIZER (0)
43 #define lll_futex_wait(futex, val) lll_futex_timed_wait (futex, val, 0)
45 #define lll_futex_timed_wait(ftx, val, timespec) \
46 ({ \
47 DO_INLINE_SYSCALL(futex, 4, (long) (ftx), FUTEX_WAIT, (int) (val), \
48 (long) (timespec)); \
49 _r10 == -1 ? -_retval : _retval; \
52 #define lll_futex_wake(ftx, nr) \
53 ({ \
54 DO_INLINE_SYSCALL(futex, 3, (long) (ftx), FUTEX_WAKE, (int) (nr)); \
55 _r10 == -1 ? -_retval : _retval; \
58 #define lll_robust_mutex_dead(futexv) \
59 do \
60 { \
61 int *__futexp = &(futexv); \
62 atomic_or (__futexp, FUTEX_OWNER_DIED); \
63 DO_INLINE_SYSCALL(futex, 3, (long) __futexp, FUTEX_WAKE, 1); \
64 } \
65 while (0)
67 /* Returns non-zero if error happened, zero if success. */
68 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val) \
69 ({ \
70 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), FUTEX_CMP_REQUEUE, \
71 (int) (nr_wake), (int) (nr_move), (long) (mutex), \
72 (int) val); \
73 _r10 == -1; \
76 /* Returns non-zero if error happened, zero if success. */
77 #define lll_futex_wake_unlock(ftx, nr_wake, nr_wake2, ftx2) \
78 ({ \
79 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), FUTEX_WAKE_OP, \
80 (int) (nr_wake), (int) (nr_wake2), (long) (ftx2), \
81 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
82 _r10 == -1; \
86 #define __lll_mutex_trylock(futex) \
87 (atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0)
88 #define lll_mutex_trylock(futex) __lll_mutex_trylock (&(futex))
91 #define __lll_robust_mutex_trylock(futex, id) \
92 (atomic_compare_and_exchange_val_acq (futex, id, 0) != 0)
93 #define lll_robust_mutex_trylock(futex, id) \
94 __lll_robust_mutex_trylock (&(futex), id)
97 #define __lll_mutex_cond_trylock(futex) \
98 (atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0)
99 #define lll_mutex_cond_trylock(futex) __lll_mutex_cond_trylock (&(futex))
102 extern void __lll_lock_wait (int *futex) attribute_hidden;
103 extern int __lll_robust_lock_wait (int *futex) attribute_hidden;
106 #define __lll_mutex_lock(futex) \
107 ((void) ({ \
108 int *__futex = (futex); \
109 if (atomic_compare_and_exchange_bool_acq (__futex, 1, 0) != 0) \
110 __lll_lock_wait (__futex); \
112 #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
115 #define __lll_robust_mutex_lock(futex, id) \
116 ({ \
117 int *__futex = (futex); \
118 int __val = 0; \
120 if (atomic_compare_and_exchange_bool_acq (__futex, id, 0) != 0) \
121 __val = __lll_robust_lock_wait (__futex); \
122 __val; \
124 #define lll_robust_mutex_lock(futex, id) __lll_robust_mutex_lock (&(futex), id)
127 #define __lll_mutex_cond_lock(futex) \
128 ((void) ({ \
129 int *__futex = (futex); \
130 if (atomic_compare_and_exchange_bool_acq (__futex, 2, 0) != 0) \
131 __lll_lock_wait (__futex); \
133 #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
136 #define __lll_robust_mutex_cond_lock(futex, id) \
137 ({ \
138 int *__futex = (futex); \
139 int __val = 0; \
140 int __id = (id) | FUTEX_WAITERS; \
142 if (atomic_compare_and_exchange_bool_acq (__futex, __id, 0) != 0) \
143 __val = __lll_robust_lock_wait (__futex); \
144 __val; \
146 #define lll_robust_mutex_cond_lock(futex, id) \
147 __lll_robust_mutex_cond_lock (&(futex), id)
150 extern int __lll_timedlock_wait (int *futex, const struct timespec *)
151 attribute_hidden;
152 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *)
153 attribute_hidden;
156 #define __lll_mutex_timedlock(futex, abstime) \
157 ({ \
158 int *__futex = (futex); \
159 int __val = 0; \
161 if (atomic_compare_and_exchange_bool_acq (__futex, 1, 0) != 0) \
162 __val = __lll_timedlock_wait (__futex, abstime); \
163 __val; \
165 #define lll_mutex_timedlock(futex, abstime) \
166 __lll_mutex_timedlock (&(futex), abstime)
169 #define __lll_robust_mutex_timedlock(futex, abstime, id) \
170 ({ \
171 int *__futex = (futex); \
172 int __val = 0; \
174 if (atomic_compare_and_exchange_bool_acq (__futex, id, 0) != 0) \
175 __val = __lll_robust_timedlock_wait (__futex, abstime); \
176 __val; \
178 #define lll_robust_mutex_timedlock(futex, abstime, id) \
179 __lll_robust_mutex_timedlock (&(futex), abstime, id)
182 #define __lll_mutex_unlock(futex) \
183 ((void) ({ \
184 int *__futex = (futex); \
185 int __val = atomic_exchange_rel (__futex, 0); \
187 if (__builtin_expect (__val > 1, 0)) \
188 lll_futex_wake (__futex, 1); \
190 #define lll_mutex_unlock(futex) \
191 __lll_mutex_unlock(&(futex))
194 #define __lll_robust_mutex_unlock(futex) \
195 ((void) ({ \
196 int *__futex = (futex); \
197 int __val = atomic_exchange_rel (__futex, 0); \
199 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
200 lll_futex_wake (__futex, 1); \
202 #define lll_robust_mutex_unlock(futex) \
203 __lll_robust_mutex_unlock(&(futex))
206 #define __lll_mutex_unlock_force(futex) \
207 ((void) ({ \
208 int *__futex = (futex); \
209 (void) atomic_exchange_rel (__futex, 0); \
210 lll_futex_wake (__futex, 1); \
212 #define lll_mutex_unlock_force(futex) \
213 __lll_mutex_unlock_force(&(futex))
216 #define lll_mutex_islocked(futex) \
217 (futex != 0)
220 /* We have a separate internal lock implementation which is not tied
221 to binary compatibility. We can use the lll_mutex_*. */
223 /* Type for lock object. */
224 typedef int lll_lock_t;
226 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
228 /* Initializers for lock. */
229 #define LLL_LOCK_INITIALIZER (0)
230 #define LLL_LOCK_INITIALIZER_LOCKED (1)
232 #define lll_trylock(futex) lll_mutex_trylock (futex)
233 #define lll_lock(futex) lll_mutex_lock (futex)
234 #define lll_unlock(futex) lll_mutex_unlock (futex)
235 #define lll_islocked(futex) lll_mutex_islocked (futex)
238 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
239 wakeup when the clone terminates. The memory location contains the
240 thread ID while the clone is running and is reset to zero
241 afterwards. */
242 #define lll_wait_tid(tid) \
243 do \
245 __typeof (tid) __tid; \
246 while ((__tid = (tid)) != 0) \
247 lll_futex_wait (&(tid), __tid); \
249 while (0)
251 extern int __lll_timedwait_tid (int *, const struct timespec *)
252 attribute_hidden;
254 #define lll_timedwait_tid(tid, abstime) \
255 ({ \
256 int __res = 0; \
257 if ((tid) != 0) \
258 __res = __lll_timedwait_tid (&(tid), (abstime)); \
259 __res; \
263 /* Conditional variable handling. */
265 extern void __lll_cond_wait (pthread_cond_t *cond)
266 attribute_hidden;
267 extern int __lll_cond_timedwait (pthread_cond_t *cond,
268 const struct timespec *abstime)
269 attribute_hidden;
270 extern void __lll_cond_wake (pthread_cond_t *cond)
271 attribute_hidden;
272 extern void __lll_cond_broadcast (pthread_cond_t *cond)
273 attribute_hidden;
275 #define lll_cond_wait(cond) \
276 __lll_cond_wait (cond)
277 #define lll_cond_timedwait(cond, abstime) \
278 __lll_cond_timedwait (cond, abstime)
279 #define lll_cond_wake(cond) \
280 __lll_cond_wake (cond)
281 #define lll_cond_broadcast(cond) \
282 __lll_cond_broadcast (cond)
284 #endif /* lowlevellock.h */