1 /* Copyright (C) 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Paul Mackerras <paulus@au.ibm.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>
29 # define __NR_futex 221
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_PRIVATE_FLAG 128
43 /* Initializer for compatibility lock. */
44 #define LLL_MUTEX_LOCK_INITIALIZER (0)
46 #define lll_futex_wait(futexp, val) \
48 INTERNAL_SYSCALL_DECL (__err); \
51 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
52 (futexp), FUTEX_WAIT, (val), 0); \
53 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
56 #define lll_futex_timed_wait(futexp, val, timespec) \
58 INTERNAL_SYSCALL_DECL (__err); \
61 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
62 (futexp), FUTEX_WAIT, (val), (timespec)); \
63 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
66 #define lll_futex_wake(futexp, nr) \
68 INTERNAL_SYSCALL_DECL (__err); \
71 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
72 (futexp), FUTEX_WAKE, (nr), 0); \
73 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
76 #define lll_robust_mutex_dead(futexv) \
79 INTERNAL_SYSCALL_DECL (__err); \
80 int *__futexp = &(futexv); \
82 atomic_or (__futexp, FUTEX_OWNER_DIED); \
83 INTERNAL_SYSCALL (futex, __err, 4, __futexp, FUTEX_WAKE, 1, 0); \
87 /* Returns non-zero if error happened, zero if success. */
88 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
90 INTERNAL_SYSCALL_DECL (__err); \
93 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
94 (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
95 (nr_move), (mutex), (val)); \
96 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
99 /* Returns non-zero if error happened, zero if success. */
100 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) \
102 INTERNAL_SYSCALL_DECL (__err); \
105 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
106 (futexp), FUTEX_WAKE_OP, (nr_wake), \
107 (nr_wake2), (futexp2), \
108 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
109 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
113 # define __lll_acq_instr ""
114 # define __lll_rel_instr ""
116 # define __lll_acq_instr "isync"
119 * Newer powerpc64 processors support the new "light weight" sync (lwsync)
120 * So if the build is using -mcpu=[power4,power5,power5+,970] we can
123 # define __lll_rel_instr "lwsync"
126 * Older powerpc32 processors don't support the new "light weight"
127 * sync (lwsync). So the only safe option is to use normal sync
128 * for all powerpc32 applications.
130 # define __lll_rel_instr "sync"
134 /* Set *futex to ID if it is 0, atomically. Returns the old value */
135 #define __lll_robust_trylock(futex, id) \
137 __asm __volatile ("1: lwarx %0,0,%2" MUTEX_HINT_ACQ "\n" \
140 " stwcx. %3,0,%2\n" \
142 "2: " __lll_acq_instr \
143 : "=&r" (__val), "=m" (*futex) \
144 : "r" (futex), "r" (id), "m" (*futex) \
145 : "cr0", "memory"); \
149 #define lll_robust_mutex_trylock(lock, id) __lll_robust_trylock (&(lock), id)
151 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
152 #define __lll_trylock(futex) __lll_robust_trylock (futex, 1)
154 #define lll_mutex_trylock(lock) __lll_trylock (&(lock))
156 /* Set *futex to 2 if it is 0, atomically. Returns the old value */
157 #define __lll_cond_trylock(futex) __lll_robust_trylock (futex, 2)
159 #define lll_mutex_cond_trylock(lock) __lll_cond_trylock (&(lock))
162 extern void __lll_lock_wait (int *futex
) attribute_hidden
;
163 extern int __lll_robust_lock_wait (int *futex
) attribute_hidden
;
165 #define lll_mutex_lock(lock) \
167 int *__futex = &(lock); \
168 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
170 __lll_lock_wait (__futex); \
173 #define lll_robust_mutex_lock(lock, id) \
175 int *__futex = &(lock); \
177 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
179 __val = __lll_robust_lock_wait (__futex); \
183 #define lll_mutex_cond_lock(lock) \
185 int *__futex = &(lock); \
186 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
188 __lll_lock_wait (__futex); \
191 #define lll_robust_mutex_cond_lock(lock, id) \
193 int *__futex = &(lock); \
195 int __id = id | FUTEX_WAITERS; \
196 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
198 __val = __lll_robust_lock_wait (__futex); \
203 extern int __lll_timedlock_wait
204 (int *futex
, const struct timespec
*) attribute_hidden
;
205 extern int __lll_robust_timedlock_wait
206 (int *futex
, const struct timespec
*) attribute_hidden
;
208 #define lll_mutex_timedlock(lock, abstime) \
210 int *__futex = &(lock); \
212 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
214 __val = __lll_timedlock_wait (__futex, abstime); \
218 #define lll_robust_mutex_timedlock(lock, abstime, id) \
220 int *__futex = &(lock); \
222 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
224 __val = __lll_robust_timedlock_wait (__futex, abstime); \
228 #define lll_mutex_unlock(lock) \
230 int *__futex = &(lock); \
231 int __val = atomic_exchange_rel (__futex, 0); \
232 if (__builtin_expect (__val > 1, 0)) \
233 lll_futex_wake (__futex, 1); \
236 #define lll_robust_mutex_unlock(lock) \
238 int *__futex = &(lock); \
239 int __val = atomic_exchange_rel (__futex, 0); \
240 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
241 lll_futex_wake (__futex, 1); \
244 #define lll_mutex_unlock_force(lock) \
246 int *__futex = &(lock); \
248 __asm __volatile (__lll_rel_instr ::: "memory"); \
249 lll_futex_wake (__futex, 1); \
252 #define lll_mutex_islocked(futex) \
256 /* Our internal lock implementation is identical to the binary-compatible
257 mutex implementation. */
259 /* Type for lock object. */
260 typedef int lll_lock_t
;
262 /* Initializers for lock. */
263 #define LLL_LOCK_INITIALIZER (0)
264 #define LLL_LOCK_INITIALIZER_LOCKED (1)
266 extern int lll_unlock_wake_cb (int *__futex
) attribute_hidden
;
268 /* The states of a lock are:
270 1 - taken by one user
271 >1 - taken by more users */
273 #define lll_trylock(lock) lll_mutex_trylock (lock)
274 #define lll_lock(lock) lll_mutex_lock (lock)
275 #define lll_unlock(lock) lll_mutex_unlock (lock)
276 #define lll_islocked(lock) lll_mutex_islocked (lock)
278 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
279 wakeup when the clone terminates. The memory location contains the
280 thread ID while the clone is running and is reset to zero
282 #define lll_wait_tid(tid) \
284 __typeof (tid) __tid; \
285 while ((__tid = (tid)) != 0) \
286 lll_futex_wait (&(tid), __tid); \
289 extern int __lll_timedwait_tid (int *, const struct timespec
*)
292 #define lll_timedwait_tid(tid, abstime) \
296 __res = __lll_timedwait_tid (&(tid), (abstime)); \
301 /* Conditional variable handling. */
303 extern void __lll_cond_wait (pthread_cond_t
*cond
)
305 extern int __lll_cond_timedwait (pthread_cond_t
*cond
,
306 const struct timespec
*abstime
)
308 extern void __lll_cond_wake (pthread_cond_t
*cond
)
310 extern void __lll_cond_broadcast (pthread_cond_t
*cond
)
313 #define lll_cond_wait(cond) \
314 __lll_cond_wait (cond)
315 #define lll_cond_timedwait(cond, abstime) \
316 __lll_cond_timedwait (cond, abstime)
317 #define lll_cond_wake(cond) \
318 __lll_cond_wake (cond)
319 #define lll_cond_broadcast(cond) \
320 __lll_cond_broadcast (cond)
322 #endif /* lowlevellock.h */