1 /* Copyright (C) 2003, 2004, 2006-2008, 2009 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>
27 #include <bits/kernel-features.h>
31 # define __NR_futex 221
35 #define FUTEX_REQUEUE 3
36 #define FUTEX_CMP_REQUEUE 4
37 #define FUTEX_WAKE_OP 5
38 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
39 #define FUTEX_LOCK_PI 6
40 #define FUTEX_UNLOCK_PI 7
41 #define FUTEX_TRYLOCK_PI 8
42 #define FUTEX_WAIT_BITSET 9
43 #define FUTEX_WAKE_BITSET 10
44 #define FUTEX_PRIVATE_FLAG 128
45 #define FUTEX_CLOCK_REALTIME 256
47 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
49 /* Values for 'private' parameter of locking macros. Yes, the
50 definition seems to be backwards. But it is not. The bit will be
51 reversed before passing to the system call. */
53 #define LLL_SHARED FUTEX_PRIVATE_FLAG
55 #if !defined NOT_IN_libc || defined IS_IN_rtld
56 /* In libc.so or ld.so all futexes are private. */
57 # ifdef __ASSUME_PRIVATE_FUTEX
58 # define __lll_private_flag(fl, private) \
59 ((fl) | FUTEX_PRIVATE_FLAG)
61 # define __lll_private_flag(fl, private) \
62 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
65 # ifdef __ASSUME_PRIVATE_FUTEX
66 # define __lll_private_flag(fl, private) \
67 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
69 # define __lll_private_flag(fl, private) \
70 (__builtin_constant_p (private) \
72 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
74 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
75 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
79 #define lll_futex_wait(futexp, val, private) \
80 lll_futex_timed_wait (futexp, val, NULL, private)
82 #define lll_futex_timed_wait(futexp, val, timespec, private) \
84 INTERNAL_SYSCALL_DECL (__err); \
87 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
88 __lll_private_flag (FUTEX_WAIT, private), \
90 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
93 #define lll_futex_wake(futexp, nr, private) \
95 INTERNAL_SYSCALL_DECL (__err); \
98 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
99 __lll_private_flag (FUTEX_WAKE, private), \
101 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
104 #define lll_robust_dead(futexv, private) \
107 INTERNAL_SYSCALL_DECL (__err); \
108 int *__futexp = &(futexv); \
110 atomic_or (__futexp, FUTEX_OWNER_DIED); \
111 INTERNAL_SYSCALL (futex, __err, 4, __futexp, \
112 __lll_private_flag (FUTEX_WAKE, private), 1, 0); \
116 /* Returns non-zero if error happened, zero if success. */
117 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
119 INTERNAL_SYSCALL_DECL (__err); \
122 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
123 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
124 (nr_wake), (nr_move), (mutex), (val)); \
125 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
128 /* Returns non-zero if error happened, zero if success. */
129 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
131 INTERNAL_SYSCALL_DECL (__err); \
134 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
135 __lll_private_flag (FUTEX_WAKE_OP, private), \
136 (nr_wake), (nr_wake2), (futexp2), \
137 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
138 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
143 # define __lll_acq_instr ""
144 # define __lll_rel_instr ""
146 # define __lll_acq_instr "isync"
149 * Newer powerpc64 processors support the new "light weight" sync (lwsync)
150 * So if the build is using -mcpu=[power4,power5,power5+,970] we can
153 # define __lll_rel_instr "lwsync"
156 * Older powerpc32 processors don't support the new "light weight"
157 * sync (lwsync). So the only safe option is to use normal sync
158 * for all powerpc32 applications.
160 # define __lll_rel_instr "sync"
164 /* Set *futex to ID if it is 0, atomically. Returns the old value */
165 #define __lll_robust_trylock(futex, id) \
167 __asm__ __volatile__ ("1: lwarx %0,0,%2" MUTEX_HINT_ACQ "\n" \
170 " stwcx. %3,0,%2\n" \
172 "2: " __lll_acq_instr \
173 : "=&r" (__val), "=m" (*futex) \
174 : "r" (futex), "r" (id), "m" (*futex) \
175 : "cr0", "memory"); \
179 #define lll_robust_trylock(lock, id) __lll_robust_trylock (&(lock), id)
181 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
182 #define __lll_trylock(futex) __lll_robust_trylock (futex, 1)
184 #define lll_trylock(lock) __lll_trylock (&(lock))
186 /* Set *futex to 2 if it is 0, atomically. Returns the old value */
187 #define __lll_cond_trylock(futex) __lll_robust_trylock (futex, 2)
189 #define lll_cond_trylock(lock) __lll_cond_trylock (&(lock))
192 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
193 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
194 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
196 #define lll_lock(lock, private) \
198 int *__futex = &(lock); \
199 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
202 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
203 __lll_lock_wait_private (__futex); \
205 __lll_lock_wait (__futex, private); \
209 #define lll_robust_lock(lock, id, private) \
211 int *__futex = &(lock); \
213 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
215 __val = __lll_robust_lock_wait (__futex, private); \
219 #define lll_cond_lock(lock, private) \
221 int *__futex = &(lock); \
222 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
224 __lll_lock_wait (__futex, private); \
227 #define lll_robust_cond_lock(lock, id, private) \
229 int *__futex = &(lock); \
231 int __id = id | FUTEX_WAITERS; \
232 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
234 __val = __lll_robust_lock_wait (__futex, private); \
239 extern int __lll_timedlock_wait
240 (int *futex, const struct timespec *, int private) attribute_hidden;
241 extern int __lll_robust_timedlock_wait
242 (int *futex, const struct timespec *, int private) attribute_hidden;
244 #define lll_timedlock(lock, abstime, private) \
246 int *__futex = &(lock); \
248 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
250 __val = __lll_timedlock_wait (__futex, abstime, private); \
254 #define lll_robust_timedlock(lock, abstime, id, private) \
256 int *__futex = &(lock); \
258 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
260 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
264 #define lll_unlock(lock, private) \
266 int *__futex = &(lock); \
267 int __val = atomic_exchange_rel (__futex, 0); \
268 if (__builtin_expect (__val > 1, 0)) \
269 lll_futex_wake (__futex, 1, private); \
272 #define lll_robust_unlock(lock, private) \
274 int *__futex = &(lock); \
275 int __val = atomic_exchange_rel (__futex, 0); \
276 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
277 lll_futex_wake (__futex, 1, private); \
280 #define lll_islocked(futex) \
284 /* Initializers for lock. */
285 #define LLL_LOCK_INITIALIZER (0)
286 #define LLL_LOCK_INITIALIZER_LOCKED (1)
288 /* The states of a lock are:
290 1 - taken by one user
291 >1 - taken by more users */
293 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
294 wakeup when the clone terminates. The memory location contains the
295 thread ID while the clone is running and is reset to zero
297 #define lll_wait_tid(tid) \
299 __typeof (tid) __tid; \
300 while ((__tid = (tid)) != 0) \
301 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
304 extern int __lll_timedwait_tid (int *, const struct timespec *)
307 #define lll_timedwait_tid(tid, abstime) \
311 __res = __lll_timedwait_tid (&(tid), (abstime)); \
315 #endif /* lowlevellock.h */