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 Libr \ary; 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>
31 #define FUTEX_REQUEUE 3
32 #define FUTEX_CMP_REQUEUE 4
33 #define FUTEX_WAKE_OP 5
34 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
36 /* Initializer for compatibility lock. */
37 #define LLL_MUTEX_LOCK_INITIALIZER (0)
39 #define lll_futex_wait(futexp, val) \
41 INTERNAL_SYSCALL_DECL (__err); \
44 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
45 (futexp), FUTEX_WAIT, (val), 0); \
49 #define lll_futex_timed_wait(futexp, val, timespec) \
51 INTERNAL_SYSCALL_DECL (__err); \
54 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
55 (futexp), FUTEX_WAIT, (val), (timespec)); \
59 #define lll_futex_wake(futexp, nr) \
61 INTERNAL_SYSCALL_DECL (__err); \
64 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
65 (futexp), FUTEX_WAKE, (nr), 0); \
69 /* Returns non-zero if error happened, zero if success. */
70 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
72 INTERNAL_SYSCALL_DECL (__err); \
75 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
76 (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
77 (nr_move), (mutex), (val)); \
78 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
81 #define lll_robust_mutex_dead(futexv) \
84 int *__futexp = &(futexv); \
85 atomic_or (__futexp, FUTEX_OWNER_DIED); \
86 lll_futex_wake (__futexp, 1); \
90 /* Returns non-zero if error happened, zero if success. */
91 #ifdef __sparc32_atomic_do_lock
92 /* Avoid FUTEX_WAKE_OP if supporting pre-v9 CPUs. */
93 # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) 1
95 # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) \
97 INTERNAL_SYSCALL_DECL (__err); \
100 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
101 (futexp), FUTEX_WAKE_OP, (nr_wake), \
102 (nr_wake2), (futexp2), \
103 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
104 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
109 __attribute__ ((always_inline
))
110 __lll_mutex_trylock (int *futex
)
112 return atomic_compare_and_exchange_val_24_acq (futex
, 1, 0) != 0;
114 #define lll_mutex_trylock(futex) __lll_mutex_trylock (&(futex))
117 __attribute__ ((always_inline
))
118 __lll_mutex_cond_trylock (int *futex
)
120 return atomic_compare_and_exchange_val_24_acq (futex
, 2, 0) != 0;
122 #define lll_mutex_cond_trylock(futex) __lll_mutex_cond_trylock (&(futex))
125 __attribute__ ((always_inline
))
126 __lll_robust_mutex_trylock (int *futex
, int id
)
128 return atomic_compare_and_exchange_val_acq (futex
, id
, 0) != 0;
130 #define lll_robust_mutex_trylock(futex, id) \
131 __lll_robust_mutex_trylock (&(futex), id)
134 extern void __lll_lock_wait (int *futex
) attribute_hidden
;
135 extern int __lll_robust_lock_wait (int *futex
) attribute_hidden
;
138 __attribute__ ((always_inline
))
139 __lll_mutex_lock (int *futex
)
141 int val
= atomic_compare_and_exchange_val_24_acq (futex
, 1, 0);
143 if (__builtin_expect (val
!= 0, 0))
144 __lll_lock_wait (futex
);
146 #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
149 __attribute__ ((always_inline
))
150 __lll_robust_mutex_lock (int *futex
, int id
)
153 if (atomic_compare_and_exchange_bool_acq (futex
, id
, 0) != 0)
154 result
= __lll_robust_lock_wait (futex
);
157 #define lll_robust_mutex_lock(futex, id) \
158 __lll_robust_mutex_lock (&(futex), id)
161 __attribute__ ((always_inline
))
162 __lll_mutex_cond_lock (int *futex
)
164 int val
= atomic_compare_and_exchange_val_24_acq (futex
, 2, 0);
166 if (__builtin_expect (val
!= 0, 0))
167 __lll_lock_wait (futex
);
169 #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
171 #define lll_robust_mutex_cond_lock(futex, id) \
172 __lll_robust_mutex_lock (&(futex), (id) | FUTEX_WAITERS)
175 extern int __lll_timedlock_wait (int *futex
, const struct timespec
*)
177 extern int __lll_robust_timedlock_wait (int *futex
, const struct timespec
*)
181 __attribute__ ((always_inline
))
182 __lll_mutex_timedlock (int *futex
, const struct timespec
*abstime
)
184 int val
= atomic_compare_and_exchange_val_24_acq (futex
, 1, 0);
187 if (__builtin_expect (val
!= 0, 0))
188 result
= __lll_timedlock_wait (futex
, abstime
);
191 #define lll_mutex_timedlock(futex, abstime) \
192 __lll_mutex_timedlock (&(futex), abstime)
195 __attribute__ ((always_inline
))
196 __lll_robust_mutex_timedlock (int *futex
, const struct timespec
*abstime
,
200 if (atomic_compare_and_exchange_bool_acq (futex
, id
, 0) != 0)
201 result
= __lll_robust_timedlock_wait (futex
, abstime
);
204 #define lll_robust_mutex_timedlock(futex, abstime, id) \
205 __lll_robust_mutex_timedlock (&(futex), abstime, id)
207 #define lll_mutex_unlock(lock) \
209 int *__futex = &(lock); \
210 int __val = atomic_exchange_24_rel (__futex, 0); \
211 if (__builtin_expect (__val > 1, 0)) \
212 lll_futex_wake (__futex, 1); \
215 #define lll_robust_mutex_unlock(lock) \
217 int *__futex = &(lock); \
218 int __val = atomic_exchange_rel (__futex, 0); \
219 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
220 lll_futex_wake (__futex, 1); \
223 #define lll_mutex_unlock_force(lock) \
225 int *__futex = &(lock); \
226 (void) atomic_exchange_24_rel (__futex, 0); \
227 lll_futex_wake (__futex, 1); \
230 #define lll_mutex_islocked(futex) \
234 /* We have a separate internal lock implementation which is not tied
235 to binary compatibility. We can use the lll_mutex_*. */
237 /* Type for lock object. */
238 typedef int lll_lock_t
;
240 extern int lll_unlock_wake_cb (int *__futex
) attribute_hidden
;
242 /* Initializers for lock. */
243 #define LLL_LOCK_INITIALIZER (0)
244 #define LLL_LOCK_INITIALIZER_LOCKED (1)
246 #define lll_trylock(futex) lll_mutex_trylock (futex)
247 #define lll_lock(futex) lll_mutex_lock (futex)
248 #define lll_unlock(futex) lll_mutex_unlock (futex)
249 #define lll_islocked(futex) lll_mutex_islocked (futex)
252 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
253 wakeup when the clone terminates. The memory location contains the
254 thread ID while the clone is running and is reset to zero
256 #define lll_wait_tid(tid) \
259 __typeof (tid) __tid; \
260 while ((__tid = (tid)) != 0) \
261 lll_futex_wait (&(tid), __tid); \
265 extern int __lll_timedwait_tid (int *, const struct timespec
*)
268 #define lll_timedwait_tid(tid, abstime) \
272 __res = __lll_timedwait_tid (&(tid), (abstime)); \
277 /* Conditional variable handling. */
279 extern void __lll_cond_wait (pthread_cond_t
*cond
)
281 extern int __lll_cond_timedwait (pthread_cond_t
*cond
,
282 const struct timespec
*abstime
)
284 extern void __lll_cond_wake (pthread_cond_t
*cond
)
286 extern void __lll_cond_broadcast (pthread_cond_t
*cond
)
289 #define lll_cond_wait(cond) \
290 __lll_cond_wait (cond)
291 #define lll_cond_timedwait(cond, abstime) \
292 __lll_cond_timedwait (cond, abstime)
293 #define lll_cond_wake(cond) \
294 __lll_cond_wake (cond)
295 #define lll_cond_broadcast(cond) \
296 __lll_cond_broadcast (cond)
298 #endif /* lowlevellock.h */