1 /* Copyright (C) 2003, 2004, 2006 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>
30 # define __NR_futex 221
34 #define FUTEX_REQUEUE 3
35 #define FUTEX_CMP_REQUEUE 4
36 #define FUTEX_WAKE_OP 5
37 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
39 /* Initializer for compatibility lock. */
40 #define LLL_MUTEX_LOCK_INITIALIZER (0)
42 #define lll_futex_wait(futexp, val) \
44 INTERNAL_SYSCALL_DECL (__err); \
47 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
48 (futexp), FUTEX_WAIT, (val), 0); \
49 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
52 #define lll_futex_timed_wait(futexp, val, timespec) \
54 INTERNAL_SYSCALL_DECL (__err); \
57 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
58 (futexp), FUTEX_WAIT, (val), (timespec)); \
59 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
62 #define lll_futex_wake(futexp, nr) \
64 INTERNAL_SYSCALL_DECL (__err); \
67 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
68 (futexp), FUTEX_WAKE, (nr), 0); \
69 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
72 #define lll_robust_mutex_dead(futexv) \
75 INTERNAL_SYSCALL_DECL (__err); \
76 int *__futexp = &(futexv); \
78 atomic_or (__futexp, FUTEX_OWNER_DIED); \
79 INTERNAL_SYSCALL (futex, __err, 4, __futexp, FUTEX_WAKE, 1, 0); \
83 /* Returns non-zero if error happened, zero if success. */
84 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
86 INTERNAL_SYSCALL_DECL (__err); \
89 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
90 (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
91 (nr_move), (mutex), (val)); \
92 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
95 /* Returns non-zero if error happened, zero if success. */
96 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) \
98 INTERNAL_SYSCALL_DECL (__err); \
101 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
102 (futexp), FUTEX_WAKE_OP, (nr_wake), \
103 (nr_wake2), (futexp2), \
104 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
105 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
109 # define __lll_acq_instr ""
110 # define __lll_rel_instr ""
112 # define __lll_acq_instr "isync"
115 * Newer powerpc64 processors support the new "light weight" sync (lwsync)
116 * So if the build is using -mcpu=[power4,power5,power5+,970] we can
119 # define __lll_rel_instr "lwsync"
122 * Older powerpc32 processors don't support the new "light weight"
123 * sync (lwsync). So the only safe option is to use normal sync
124 * for all powerpc32 applications.
126 # define __lll_rel_instr "sync"
130 /* Set *futex to ID if it is 0, atomically. Returns the old value */
131 #define __lll_robust_trylock(futex, id) \
133 __asm __volatile ("1: lwarx %0,0,%2\n" \
136 " stwcx. %3,0,%2\n" \
138 "2: " __lll_acq_instr \
139 : "=&r" (__val), "=m" (*futex) \
140 : "r" (futex), "r" (id), "m" (*futex) \
141 : "cr0", "memory"); \
145 #define lll_robust_mutex_trylock(lock, id) __lll_robust_trylock (&(lock), id)
147 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
148 #define __lll_trylock(futex) __lll_robust_trylock (futex, 1)
150 #define lll_mutex_trylock(lock) __lll_trylock (&(lock))
152 /* Set *futex to 2 if it is 0, atomically. Returns the old value */
153 #define __lll_cond_trylock(futex) __lll_robust_trylock (futex, 2)
155 #define lll_mutex_cond_trylock(lock) __lll_cond_trylock (&(lock))
158 extern void __lll_lock_wait (int *futex) attribute_hidden;
159 extern int __lll_robust_lock_wait (int *futex) attribute_hidden;
161 #define lll_mutex_lock(lock) \
163 int *__futex = &(lock); \
164 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
166 __lll_lock_wait (__futex); \
169 #define lll_robust_mutex_lock(lock, id) \
171 int *__futex = &(lock); \
173 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
175 __val = __lll_robust_lock_wait (__futex); \
179 #define lll_mutex_cond_lock(lock) \
181 int *__futex = &(lock); \
182 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
184 __lll_lock_wait (__futex); \
187 #define lll_robust_mutex_cond_lock(lock, id) \
189 int *__futex = &(lock); \
191 int __id = id | FUTEX_WAITERS; \
192 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
194 __val = __lll_robust_lock_wait (__futex); \
199 extern int __lll_timedlock_wait
200 (int *futex, const struct timespec *) attribute_hidden;
201 extern int __lll_robust_timedlock_wait
202 (int *futex, const struct timespec *) attribute_hidden;
204 #define lll_mutex_timedlock(lock, abstime) \
206 int *__futex = &(lock); \
208 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
210 __val = __lll_timedlock_wait (__futex, abstime); \
214 #define lll_robust_mutex_timedlock(lock, abstime, id) \
216 int *__futex = &(lock); \
218 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
220 __val = __lll_robust_timedlock_wait (__futex, abstime); \
224 #define lll_mutex_unlock(lock) \
226 int *__futex = &(lock); \
227 int __val = atomic_exchange_rel (__futex, 0); \
228 if (__builtin_expect (__val > 1, 0)) \
229 lll_futex_wake (__futex, 1); \
232 #define lll_robust_mutex_unlock(lock) \
234 int *__futex = &(lock); \
235 int __val = atomic_exchange_rel (__futex, 0); \
236 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
237 lll_futex_wake (__futex, 1); \
240 #define lll_mutex_unlock_force(lock) \
242 int *__futex = &(lock); \
244 __asm __volatile (__lll_rel_instr ::: "memory"); \
245 lll_futex_wake (__futex, 1); \
248 #define lll_mutex_islocked(futex) \
252 /* Our internal lock implementation is identical to the binary-compatible
253 mutex implementation. */
255 /* Type for lock object. */
256 typedef int lll_lock_t;
258 /* Initializers for lock. */
259 #define LLL_LOCK_INITIALIZER (0)
260 #define LLL_LOCK_INITIALIZER_LOCKED (1)
262 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
264 /* The states of a lock are:
266 1 - taken by one user
267 >1 - taken by more users */
269 #define lll_trylock(lock) lll_mutex_trylock (lock)
270 #define lll_lock(lock) lll_mutex_lock (lock)
271 #define lll_unlock(lock) lll_mutex_unlock (lock)
272 #define lll_islocked(lock) lll_mutex_islocked (lock)
274 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
275 wakeup when the clone terminates. The memory location contains the
276 thread ID while the clone is running and is reset to zero
278 #define lll_wait_tid(tid) \
280 __typeof (tid) __tid; \
281 while ((__tid = (tid)) != 0) \
282 lll_futex_wait (&(tid), __tid); \
285 extern int __lll_timedwait_tid (int *, const struct timespec *)
288 #define lll_timedwait_tid(tid, abstime) \
292 __res = __lll_timedwait_tid (&(tid), (abstime)); \
297 /* Conditional variable handling. */
299 extern void __lll_cond_wait (pthread_cond_t *cond)
301 extern int __lll_cond_timedwait (pthread_cond_t *cond,
302 const struct timespec *abstime)
304 extern void __lll_cond_wake (pthread_cond_t *cond)
306 extern void __lll_cond_broadcast (pthread_cond_t *cond)
309 #define lll_cond_wait(cond) \
310 __lll_cond_wait (cond)
311 #define lll_cond_timedwait(cond, abstime) \
312 __lll_cond_timedwait (cond, abstime)
313 #define lll_cond_wake(cond) \
314 __lll_cond_wake (cond)
315 #define lll_cond_broadcast(cond) \
316 __lll_cond_broadcast (cond)
318 #endif /* lowlevellock.h */