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
20 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
24 #include <sys/param.h>
25 #include <bits/pthreadtypes.h>
26 #include <ia64intrin.h>
29 #define __NR_futex 1230
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) \
47 DO_INLINE_SYSCALL(futex, 4, (long) (ftx), FUTEX_WAIT, (int) (val), \
49 _r10 == -1 ? -_retval : _retval; \
52 #define lll_futex_wake(ftx, nr) \
54 DO_INLINE_SYSCALL(futex, 3, (long) (ftx), FUTEX_WAKE, (int) (nr)); \
55 _r10 == -1 ? -_retval : _retval; \
58 #define lll_robust_mutex_dead(futexv) \
61 int *__futexp = &(futexv); \
62 atomic_or (__futexp, FUTEX_OWNER_DIED); \
63 DO_INLINE_SYSCALL(futex, 3, (long) __futexp, FUTEX_WAKE, 1); \
67 /* Returns non-zero if error happened, zero if success. */
68 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val) \
70 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), FUTEX_CMP_REQUEUE, \
71 (int) (nr_wake), (int) (nr_move), (long) (mutex), \
76 /* Returns non-zero if error happened, zero if success. */
77 #define lll_futex_wake_unlock(ftx, nr_wake, nr_wake2, ftx2) \
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); \
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) \
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) \
117 int *__futex = (futex); \
120 if (atomic_compare_and_exchange_bool_acq (__futex, id, 0) != 0) \
121 __val = __lll_robust_lock_wait (__futex); \
124 #define lll_robust_mutex_lock(futex, id) __lll_robust_mutex_lock (&(futex), id)
127 #define __lll_mutex_cond_lock(futex) \
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) \
138 int *__futex = (futex); \
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); \
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
*)
152 extern int __lll_robust_timedlock_wait (int *futex
, const struct timespec
*)
156 #define __lll_mutex_timedlock(futex, abstime) \
158 int *__futex = (futex); \
161 if (atomic_compare_and_exchange_bool_acq (__futex, 1, 0) != 0) \
162 __val = __lll_timedlock_wait (__futex, abstime); \
165 #define lll_mutex_timedlock(futex, abstime) \
166 __lll_mutex_timedlock (&(futex), abstime)
169 #define __lll_robust_mutex_timedlock(futex, abstime, id) \
171 int *__futex = (futex); \
174 if (atomic_compare_and_exchange_bool_acq (__futex, id, 0) != 0) \
175 __val = __lll_robust_timedlock_wait (__futex, abstime); \
178 #define lll_robust_mutex_timedlock(futex, abstime, id) \
179 __lll_robust_mutex_timedlock (&(futex), abstime, id)
182 #define __lll_mutex_unlock(futex) \
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) \
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) \
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) \
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
242 #define lll_wait_tid(tid) \
245 __typeof (tid) __tid; \
246 while ((__tid = (tid)) != 0) \
247 lll_futex_wait (&(tid), __tid); \
251 extern int __lll_timedwait_tid (int *, const struct timespec
*)
254 #define lll_timedwait_tid(tid, abstime) \
258 __res = __lll_timedwait_tid (&(tid), (abstime)); \
263 /* Conditional variable handling. */
265 extern void __lll_cond_wait (pthread_cond_t
*cond
)
267 extern int __lll_cond_timedwait (pthread_cond_t
*cond
,
268 const struct timespec
*abstime
)
270 extern void __lll_cond_wake (pthread_cond_t
*cond
)
272 extern void __lll_cond_broadcast (pthread_cond_t
*cond
)
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 */