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>
27 #include <kernel-features.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)
38 #define FUTEX_LOCK_PI 6
39 #define FUTEX_UNLOCK_PI 7
40 #define FUTEX_TRYLOCK_PI 8
41 #define FUTEX_PRIVATE_FLAG 128
43 /* Values for 'private' parameter of locking macros. Yes, the
44 definition seems to be backwards. But it is not. The bit will be
45 reversed before passing to the system call. */
47 #define LLL_SHARED FUTEX_PRIVATE_FLAG
49 #if !defined NOT_IN_libc || defined IS_IN_rtld
50 /* In libc.so or ld.so all futexes are private. */
51 # ifdef __ASSUME_PRIVATE_FUTEX
52 # define __lll_private_flag(fl, private) \
53 ((fl) | FUTEX_PRIVATE_FLAG)
55 # define __lll_private_flag(fl, private) \
56 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
59 # ifdef __ASSUME_PRIVATE_FUTEX
60 # define __lll_private_flag(fl, private) \
61 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
63 # define __lll_private_flag(fl, private) \
64 (__builtin_constant_p (private) \
66 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
68 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
69 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
73 #define lll_futex_wait(futexp, val, private) \
74 lll_futex_timed_wait (futexp, val, NULL, private)
76 #define lll_futex_timed_wait(futexp, val, timespec, private) \
78 INTERNAL_SYSCALL_DECL (__err); \
81 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
82 __lll_private_flag (FUTEX_WAIT, private), \
84 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
87 #define lll_futex_wake(futexp, nr, private) \
89 INTERNAL_SYSCALL_DECL (__err); \
92 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
93 __lll_private_flag (FUTEX_WAKE, private), \
95 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
98 #define lll_robust_dead(futexv, private) \
101 INTERNAL_SYSCALL_DECL (__err); \
102 int *__futexp = &(futexv); \
104 atomic_or (__futexp, FUTEX_OWNER_DIED); \
105 INTERNAL_SYSCALL (futex, __err, 4, __futexp, \
106 __lll_private_flag (FUTEX_WAKE, private), 1, 0); \
110 /* Returns non-zero if error happened, zero if success. */
111 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
113 INTERNAL_SYSCALL_DECL (__err); \
116 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
117 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
118 (nr_wake), (nr_move), (mutex), (val)); \
119 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
122 /* Returns non-zero if error happened, zero if success. */
123 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
125 INTERNAL_SYSCALL_DECL (__err); \
128 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
129 __lll_private_flag (FUTEX_WAKE_OP, private), \
130 (nr_wake), (nr_wake2), (futexp2), \
131 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
132 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
137 # define __lll_acq_instr ""
138 # define __lll_rel_instr ""
140 # define __lll_acq_instr "isync"
143 * Newer powerpc64 processors support the new "light weight" sync (lwsync)
144 * So if the build is using -mcpu=[power4,power5,power5+,970] we can
147 # define __lll_rel_instr "lwsync"
150 * Older powerpc32 processors don't support the new "light weight"
151 * sync (lwsync). So the only safe option is to use normal sync
152 * for all powerpc32 applications.
154 # define __lll_rel_instr "sync"
158 /* Set *futex to ID if it is 0, atomically. Returns the old value */
159 #define __lll_robust_trylock(futex, id) \
161 __asm __volatile ("1: lwarx %0,0,%2" MUTEX_HINT_ACQ "\n" \
164 " stwcx. %3,0,%2\n" \
166 "2: " __lll_acq_instr \
167 : "=&r" (__val), "=m" (*futex) \
168 : "r" (futex), "r" (id), "m" (*futex) \
169 : "cr0", "memory"); \
173 #define lll_robust_trylock(lock, id) __lll_robust_trylock (&(lock), id)
175 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
176 #define __lll_trylock(futex) __lll_robust_trylock (futex, 1)
178 #define lll_trylock(lock) __lll_trylock (&(lock))
180 /* Set *futex to 2 if it is 0, atomically. Returns the old value */
181 #define __lll_cond_trylock(futex) __lll_robust_trylock (futex, 2)
183 #define lll_cond_trylock(lock) __lll_cond_trylock (&(lock))
186 extern void __lll_lock_wait_private (int *futex
) attribute_hidden
;
187 extern void __lll_lock_wait (int *futex
, int private) attribute_hidden
;
188 extern int __lll_robust_lock_wait (int *futex
, int private) attribute_hidden
;
190 #define lll_lock(lock, private) \
192 int *__futex = &(lock); \
193 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
196 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
197 __lll_lock_wait_private (__futex); \
199 __lll_lock_wait (__futex, private); \
203 #define lll_robust_lock(lock, id, private) \
205 int *__futex = &(lock); \
207 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
209 __val = __lll_robust_lock_wait (__futex, private); \
213 #define lll_cond_lock(lock, private) \
215 int *__futex = &(lock); \
216 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
218 __lll_lock_wait (__futex, private); \
221 #define lll_robust_cond_lock(lock, id, private) \
223 int *__futex = &(lock); \
225 int __id = id | FUTEX_WAITERS; \
226 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
228 __val = __lll_robust_lock_wait (__futex, private); \
233 extern int __lll_timedlock_wait
234 (int *futex
, const struct timespec
*, int private) attribute_hidden
;
235 extern int __lll_robust_timedlock_wait
236 (int *futex
, const struct timespec
*, int private) attribute_hidden
;
238 #define lll_timedlock(lock, abstime, private) \
240 int *__futex = &(lock); \
242 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
244 __val = __lll_timedlock_wait (__futex, abstime, private); \
248 #define lll_robust_timedlock(lock, abstime, id, private) \
250 int *__futex = &(lock); \
252 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
254 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
258 #define lll_unlock(lock, private) \
260 int *__futex = &(lock); \
261 int __val = atomic_exchange_rel (__futex, 0); \
262 if (__builtin_expect (__val > 1, 0)) \
263 lll_futex_wake (__futex, 1, private); \
266 #define lll_robust_unlock(lock, private) \
268 int *__futex = &(lock); \
269 int __val = atomic_exchange_rel (__futex, 0); \
270 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
271 lll_futex_wake (__futex, 1, private); \
274 #define lll_islocked(futex) \
278 /* Initializers for lock. */
279 #define LLL_LOCK_INITIALIZER (0)
280 #define LLL_LOCK_INITIALIZER_LOCKED (1)
282 /* The states of a lock are:
284 1 - taken by one user
285 >1 - taken by more users */
287 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
288 wakeup when the clone terminates. The memory location contains the
289 thread ID while the clone is running and is reset to zero
291 #define lll_wait_tid(tid) \
293 __typeof (tid) __tid; \
294 while ((__tid = (tid)) != 0) \
295 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
298 extern int __lll_timedwait_tid (int *, const struct timespec
*)
301 #define lll_timedwait_tid(tid, abstime) \
305 __res = __lll_timedwait_tid (&(tid), (abstime)); \
309 #endif /* lowlevellock.h */