1 /* Copyright (C) 2003, 2004, 2006, 2007, 2008 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_WAIT_BITSET 9
42 #define FUTEX_WAKE_BITSET 10
43 #define FUTEX_PRIVATE_FLAG 128
45 /* Values for 'private' parameter of locking macros. Yes, the
46 definition seems to be backwards. But it is not. The bit will be
47 reversed before passing to the system call. */
49 #define LLL_SHARED FUTEX_PRIVATE_FLAG
51 #if !defined NOT_IN_libc || defined IS_IN_rtld
52 /* In libc.so or ld.so all futexes are private. */
53 # ifdef __ASSUME_PRIVATE_FUTEX
54 # define __lll_private_flag(fl, private) \
55 ((fl) | FUTEX_PRIVATE_FLAG)
57 # define __lll_private_flag(fl, private) \
58 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
61 # ifdef __ASSUME_PRIVATE_FUTEX
62 # define __lll_private_flag(fl, private) \
63 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
65 # define __lll_private_flag(fl, private) \
66 (__builtin_constant_p (private) \
68 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
70 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
71 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
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_dead(futexv, private) \
103 INTERNAL_SYSCALL_DECL (__err); \
104 int *__futexp = &(futexv); \
106 atomic_or (__futexp, FUTEX_OWNER_DIED); \
107 INTERNAL_SYSCALL (futex, __err, 4, __futexp, \
108 __lll_private_flag (FUTEX_WAKE, private), 1, 0); \
112 /* Returns non-zero if error happened, zero if success. */
113 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
115 INTERNAL_SYSCALL_DECL (__err); \
118 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
119 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
120 (nr_wake), (nr_move), (mutex), (val)); \
121 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
124 /* Returns non-zero if error happened, zero if success. */
125 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
127 INTERNAL_SYSCALL_DECL (__err); \
130 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
131 __lll_private_flag (FUTEX_WAKE_OP, private), \
132 (nr_wake), (nr_wake2), (futexp2), \
133 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
134 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
139 # define __lll_acq_instr ""
140 # define __lll_rel_instr ""
142 # define __lll_acq_instr "isync"
145 * Newer powerpc64 processors support the new "light weight" sync (lwsync)
146 * So if the build is using -mcpu=[power4,power5,power5+,970] we can
149 # define __lll_rel_instr "lwsync"
152 * Older powerpc32 processors don't support the new "light weight"
153 * sync (lwsync). So the only safe option is to use normal sync
154 * for all powerpc32 applications.
156 # define __lll_rel_instr "sync"
160 /* Set *futex to ID if it is 0, atomically. Returns the old value */
161 #define __lll_robust_trylock(futex, id) \
163 __asm __volatile ("1: lwarx %0,0,%2" MUTEX_HINT_ACQ "\n" \
166 " stwcx. %3,0,%2\n" \
168 "2: " __lll_acq_instr \
169 : "=&r" (__val), "=m" (*futex) \
170 : "r" (futex), "r" (id), "m" (*futex) \
171 : "cr0", "memory"); \
175 #define lll_robust_trylock(lock, id) __lll_robust_trylock (&(lock), id)
177 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
178 #define __lll_trylock(futex) __lll_robust_trylock (futex, 1)
180 #define lll_trylock(lock) __lll_trylock (&(lock))
182 /* Set *futex to 2 if it is 0, atomically. Returns the old value */
183 #define __lll_cond_trylock(futex) __lll_robust_trylock (futex, 2)
185 #define lll_cond_trylock(lock) __lll_cond_trylock (&(lock))
188 extern void __lll_lock_wait_private (int *futex
) attribute_hidden
;
189 extern void __lll_lock_wait (int *futex
, int private) attribute_hidden
;
190 extern int __lll_robust_lock_wait (int *futex
, int private) attribute_hidden
;
192 #define lll_lock(lock, private) \
194 int *__futex = &(lock); \
195 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
198 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
199 __lll_lock_wait_private (__futex); \
201 __lll_lock_wait (__futex, private); \
205 #define lll_robust_lock(lock, id, private) \
207 int *__futex = &(lock); \
209 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
211 __val = __lll_robust_lock_wait (__futex, private); \
215 #define lll_cond_lock(lock, private) \
217 int *__futex = &(lock); \
218 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
220 __lll_lock_wait (__futex, private); \
223 #define lll_robust_cond_lock(lock, id, private) \
225 int *__futex = &(lock); \
227 int __id = id | FUTEX_WAITERS; \
228 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
230 __val = __lll_robust_lock_wait (__futex, private); \
235 extern int __lll_timedlock_wait
236 (int *futex
, const struct timespec
*, int private) attribute_hidden
;
237 extern int __lll_robust_timedlock_wait
238 (int *futex
, const struct timespec
*, int private) attribute_hidden
;
240 #define lll_timedlock(lock, abstime, private) \
242 int *__futex = &(lock); \
244 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
246 __val = __lll_timedlock_wait (__futex, abstime, private); \
250 #define lll_robust_timedlock(lock, abstime, id, private) \
252 int *__futex = &(lock); \
254 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
256 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
260 #define lll_unlock(lock, private) \
262 int *__futex = &(lock); \
263 int __val = atomic_exchange_rel (__futex, 0); \
264 if (__builtin_expect (__val > 1, 0)) \
265 lll_futex_wake (__futex, 1, private); \
268 #define lll_robust_unlock(lock, private) \
270 int *__futex = &(lock); \
271 int __val = atomic_exchange_rel (__futex, 0); \
272 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
273 lll_futex_wake (__futex, 1, private); \
276 #define lll_islocked(futex) \
280 /* Initializers for lock. */
281 #define LLL_LOCK_INITIALIZER (0)
282 #define LLL_LOCK_INITIALIZER_LOCKED (1)
284 /* The states of a lock are:
286 1 - taken by one user
287 >1 - taken by more users */
289 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
290 wakeup when the clone terminates. The memory location contains the
291 thread ID while the clone is running and is reset to zero
293 #define lll_wait_tid(tid) \
295 __typeof (tid) __tid; \
296 while ((__tid = (tid)) != 0) \
297 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
300 extern int __lll_timedwait_tid (int *, const struct timespec
*)
303 #define lll_timedwait_tid(tid, abstime) \
307 __res = __lll_timedwait_tid (&(tid), (abstime)); \
311 #endif /* lowlevellock.h */