2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / unix / sysv / linux / powerpc / lowlevellock.h
blob80b9a4369e6c6f4778ca8105fa481e8bd5de4dfb
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
18 02111-1307 USA. */
20 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
23 #include <time.h>
24 #include <sys/param.h>
25 #include <bits/pthreadtypes.h>
26 #include <atomic.h>
27 #include <kernel-features.h>
29 #ifndef __NR_futex
30 # define __NR_futex 221
31 #endif
32 #define FUTEX_WAIT 0
33 #define FUTEX_WAKE 1
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. */
46 #define LLL_PRIVATE 0
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)
54 # else
55 # define __lll_private_flag(fl, private) \
56 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
57 # endif
58 #else
59 # ifdef __ASSUME_PRIVATE_FUTEX
60 # define __lll_private_flag(fl, private) \
61 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
62 # else
63 # define __lll_private_flag(fl, private) \
64 (__builtin_constant_p (private) \
65 ? ((private) == 0 \
66 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
67 : (fl)) \
68 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
69 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
70 # endif
71 #endif
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) \
77 ({ \
78 INTERNAL_SYSCALL_DECL (__err); \
79 long int __ret; \
81 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
82 __lll_private_flag (FUTEX_WAIT, private), \
83 (val), (timespec)); \
84 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
87 #define lll_futex_wake(futexp, nr, private) \
88 ({ \
89 INTERNAL_SYSCALL_DECL (__err); \
90 long int __ret; \
92 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
93 __lll_private_flag (FUTEX_WAKE, private), \
94 (nr), 0); \
95 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
98 #define lll_robust_dead(futexv, private) \
99 do \
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); \
108 while (0)
110 /* Returns non-zero if error happened, zero if success. */
111 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
112 ({ \
113 INTERNAL_SYSCALL_DECL (__err); \
114 long int __ret; \
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) \
124 ({ \
125 INTERNAL_SYSCALL_DECL (__err); \
126 long int __ret; \
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); \
136 #ifdef UP
137 # define __lll_acq_instr ""
138 # define __lll_rel_instr ""
139 #else
140 # define __lll_acq_instr "isync"
141 # ifdef _ARCH_PWR4
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
145 * safely use lwsync.
147 # define __lll_rel_instr "lwsync"
148 # else
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"
155 # endif
156 #endif
158 /* Set *futex to ID if it is 0, atomically. Returns the old value */
159 #define __lll_robust_trylock(futex, id) \
160 ({ int __val; \
161 __asm __volatile ("1: lwarx %0,0,%2" MUTEX_HINT_ACQ "\n" \
162 " cmpwi 0,%0,0\n" \
163 " bne 2f\n" \
164 " stwcx. %3,0,%2\n" \
165 " bne- 1b\n" \
166 "2: " __lll_acq_instr \
167 : "=&r" (__val), "=m" (*futex) \
168 : "r" (futex), "r" (id), "m" (*futex) \
169 : "cr0", "memory"); \
170 __val; \
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) \
191 (void) ({ \
192 int *__futex = &(lock); \
193 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
194 0) != 0) \
196 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
197 __lll_lock_wait_private (__futex); \
198 else \
199 __lll_lock_wait (__futex, private); \
203 #define lll_robust_lock(lock, id, private) \
204 ({ \
205 int *__futex = &(lock); \
206 int __val = 0; \
207 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
208 0), 0)) \
209 __val = __lll_robust_lock_wait (__futex, private); \
210 __val; \
213 #define lll_cond_lock(lock, private) \
214 (void) ({ \
215 int *__futex = &(lock); \
216 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
217 0) != 0) \
218 __lll_lock_wait (__futex, private); \
221 #define lll_robust_cond_lock(lock, id, private) \
222 ({ \
223 int *__futex = &(lock); \
224 int __val = 0; \
225 int __id = id | FUTEX_WAITERS; \
226 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
227 0), 0)) \
228 __val = __lll_robust_lock_wait (__futex, private); \
229 __val; \
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) \
239 ({ \
240 int *__futex = &(lock); \
241 int __val = 0; \
242 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
243 0) != 0) \
244 __val = __lll_timedlock_wait (__futex, abstime, private); \
245 __val; \
248 #define lll_robust_timedlock(lock, abstime, id, private) \
249 ({ \
250 int *__futex = &(lock); \
251 int __val = 0; \
252 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
253 0), 0)) \
254 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
255 __val; \
258 #define lll_unlock(lock, private) \
259 ((void) ({ \
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) \
267 ((void) ({ \
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) \
275 (futex != 0)
278 /* Initializers for lock. */
279 #define LLL_LOCK_INITIALIZER (0)
280 #define LLL_LOCK_INITIALIZER_LOCKED (1)
282 /* The states of a lock are:
283 0 - untaken
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
290 afterwards. */
291 #define lll_wait_tid(tid) \
292 do { \
293 __typeof (tid) __tid; \
294 while ((__tid = (tid)) != 0) \
295 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
296 } while (0)
298 extern int __lll_timedwait_tid (int *, const struct timespec *)
299 attribute_hidden;
301 #define lll_timedwait_tid(tid, abstime) \
302 ({ \
303 int __res = 0; \
304 if ((tid) != 0) \
305 __res = __lll_timedwait_tid (&(tid), (abstime)); \
306 __res; \
309 #endif /* lowlevellock.h */