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
42 /* Values for 'private' parameter of locking macros. Yes, the
43 definition seems to be backwards. But it is not. The bit will be
44 reversed before passing to the system call. */
46 #define LLL_SHARED FUTEX_PRIVATE_FLAG
48 #if !defined NOT_IN_libc || defined IS_IN_rtld
49 /* In libc.so or ld.so all futexes are private. */
50 # ifdef __ASSUME_PRIVATE_FUTEX
51 # define __lll_private_flag(fl, private) \
52 ((fl) | FUTEX_PRIVATE_FLAG)
54 # define __lll_private_flag(fl, private) \
55 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
58 # ifdef __ASSUME_PRIVATE_FUTEX
59 # define __lll_private_flag(fl, private) \
60 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
62 # define __lll_private_flag(fl, private) \
63 (__builtin_constant_p (private) \
65 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
67 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
68 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
72 /* Initializer for compatibility lock. */
73 #define LLL_MUTEX_LOCK_INITIALIZER (0)
75 #define lll_futex_wait(futexp, val, private) \
76 lll_futex_timed_wait (futexp, val, NULL, private)
78 #define lll_futex_timed_wait(futexp, val, timespec, private) \
80 INTERNAL_SYSCALL_DECL (__err); \
83 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
84 __lll_private_flag (FUTEX_WAIT, private), \
86 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
89 #define lll_futex_wake(futexp, nr, private) \
91 INTERNAL_SYSCALL_DECL (__err); \
94 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
95 __lll_private_flag (FUTEX_WAKE, private), \
97 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
100 #define lll_robust_mutex_dead(futexv) \
103 INTERNAL_SYSCALL_DECL (__err); \
104 int *__futexp = &(futexv); \
106 atomic_or (__futexp, FUTEX_OWNER_DIED); \
107 INTERNAL_SYSCALL (futex, __err, 4, __futexp, FUTEX_WAKE, 1, 0); \
111 /* Returns non-zero if error happened, zero if success. */
112 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
114 INTERNAL_SYSCALL_DECL (__err); \
117 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
118 (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
119 (nr_move), (mutex), (val)); \
120 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
123 /* Returns non-zero if error happened, zero if success. */
124 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
126 INTERNAL_SYSCALL_DECL (__err); \
129 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
130 __lll_private_flag (FUTEX_WAKE_OP, private), \
131 (nr_wake), (nr_wake2), (futexp2), \
132 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
133 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
137 #define lll_private_futex_wait(futexp, val) \
138 lll_futex_timed_wait (futexp, val, NULL, LLL_PRIVATE)
140 #define lll_private_futex_timed_wait(futexp, val, timeout) \
141 lll_futex_timed_wait (futexp, val, timeout, LLL_PRIVATE)
143 #define lll_private_futex_wake(futexp, val) \
144 lll_futex_wake (futexp, val, LLL_PRIVATE)
147 # define __lll_acq_instr ""
148 # define __lll_rel_instr ""
150 # define __lll_acq_instr "isync"
153 * Newer powerpc64 processors support the new "light weight" sync (lwsync)
154 * So if the build is using -mcpu=[power4,power5,power5+,970] we can
157 # define __lll_rel_instr "lwsync"
160 * Older powerpc32 processors don't support the new "light weight"
161 * sync (lwsync). So the only safe option is to use normal sync
162 * for all powerpc32 applications.
164 # define __lll_rel_instr "sync"
168 /* Set *futex to ID if it is 0, atomically. Returns the old value */
169 #define __lll_robust_trylock(futex, id) \
171 __asm __volatile ("1: lwarx %0,0,%2" MUTEX_HINT_ACQ "\n" \
174 " stwcx. %3,0,%2\n" \
176 "2: " __lll_acq_instr \
177 : "=&r" (__val), "=m" (*futex) \
178 : "r" (futex), "r" (id), "m" (*futex) \
179 : "cr0", "memory"); \
183 #define lll_robust_mutex_trylock(lock, id) __lll_robust_trylock (&(lock), id)
185 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
186 #define __lll_trylock(futex) __lll_robust_trylock (futex, 1)
188 #define lll_mutex_trylock(lock) __lll_trylock (&(lock))
190 /* Set *futex to 2 if it is 0, atomically. Returns the old value */
191 #define __lll_cond_trylock(futex) __lll_robust_trylock (futex, 2)
193 #define lll_mutex_cond_trylock(lock) __lll_cond_trylock (&(lock))
196 extern void __lll_lock_wait (int *futex) attribute_hidden;
197 extern int __lll_robust_lock_wait (int *futex) attribute_hidden;
199 #define lll_mutex_lock(lock) \
201 int *__futex = &(lock); \
202 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
204 __lll_lock_wait (__futex); \
207 #define lll_robust_mutex_lock(lock, id) \
209 int *__futex = &(lock); \
211 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
213 __val = __lll_robust_lock_wait (__futex); \
217 #define lll_mutex_cond_lock(lock) \
219 int *__futex = &(lock); \
220 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
222 __lll_lock_wait (__futex); \
225 #define lll_robust_mutex_cond_lock(lock, id) \
227 int *__futex = &(lock); \
229 int __id = id | FUTEX_WAITERS; \
230 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
232 __val = __lll_robust_lock_wait (__futex); \
237 extern int __lll_timedlock_wait
238 (int *futex, const struct timespec *) attribute_hidden;
239 extern int __lll_robust_timedlock_wait
240 (int *futex, const struct timespec *) attribute_hidden;
242 #define lll_mutex_timedlock(lock, abstime) \
244 int *__futex = &(lock); \
246 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
248 __val = __lll_timedlock_wait (__futex, abstime); \
252 #define lll_robust_mutex_timedlock(lock, abstime, id) \
254 int *__futex = &(lock); \
256 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
258 __val = __lll_robust_timedlock_wait (__futex, abstime); \
262 #define lll_mutex_unlock(lock) \
264 int *__futex = &(lock); \
265 int __val = atomic_exchange_rel (__futex, 0); \
266 if (__builtin_expect (__val > 1, 0)) \
267 lll_futex_wake (__futex, 1, LLL_SHARED); \
270 #define lll_robust_mutex_unlock(lock) \
272 int *__futex = &(lock); \
273 int __val = atomic_exchange_rel (__futex, 0); \
274 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
275 lll_futex_wake (__futex, 1, LLL_SHARED); \
278 #define lll_mutex_unlock_force(lock) \
280 int *__futex = &(lock); \
282 __asm __volatile (__lll_rel_instr ::: "memory"); \
283 lll_futex_wake (__futex, 1, LLL_SHARED); \
286 #define lll_mutex_islocked(futex) \
290 /* Our internal lock implementation is identical to the binary-compatible
291 mutex implementation. */
293 /* Type for lock object. */
294 typedef int lll_lock_t;
296 /* Initializers for lock. */
297 #define LLL_LOCK_INITIALIZER (0)
298 #define LLL_LOCK_INITIALIZER_LOCKED (1)
300 /* The states of a lock are:
302 1 - taken by one user
303 >1 - taken by more users */
305 #define lll_trylock(lock) lll_mutex_trylock (lock)
306 #define lll_lock(lock) lll_mutex_lock (lock)
307 #define lll_unlock(lock) lll_mutex_unlock (lock)
308 #define lll_islocked(lock) lll_mutex_islocked (lock)
310 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
311 wakeup when the clone terminates. The memory location contains the
312 thread ID while the clone is running and is reset to zero
314 #define lll_wait_tid(tid) \
316 __typeof (tid) __tid; \
317 while ((__tid = (tid)) != 0) \
318 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
321 extern int __lll_timedwait_tid (int *, const struct timespec *)
324 #define lll_timedwait_tid(tid, abstime) \
328 __res = __lll_timedwait_tid (&(tid), (abstime)); \
333 /* Conditional variable handling. */
335 extern void __lll_cond_wait (pthread_cond_t *cond)
337 extern int __lll_cond_timedwait (pthread_cond_t *cond,
338 const struct timespec *abstime)
340 extern void __lll_cond_wake (pthread_cond_t *cond)
342 extern void __lll_cond_broadcast (pthread_cond_t *cond)
345 #define lll_cond_wait(cond) \
346 __lll_cond_wait (cond)
347 #define lll_cond_timedwait(cond, abstime) \
348 __lll_cond_timedwait (cond, abstime)
349 #define lll_cond_wake(cond) \
350 __lll_cond_wake (cond)
351 #define lll_cond_broadcast(cond) \
352 __lll_cond_broadcast (cond)
354 #endif /* lowlevellock.h */