* init.c: Make it compile with older kernel headers.
[glibc.git] / nptl / sysdeps / unix / sysv / linux / powerpc / lowlevellock.h
blob71fede838487397b77ce9976b8c3400024913d15
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>
28 #ifndef __NR_futex
29 # define __NR_futex 221
30 #endif
31 #define FUTEX_WAIT 0
32 #define FUTEX_WAKE 1
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
43 /* Initializer for compatibility lock. */
44 #define LLL_MUTEX_LOCK_INITIALIZER (0)
46 #define lll_futex_wait(futexp, val) \
47 ({ \
48 INTERNAL_SYSCALL_DECL (__err); \
49 long int __ret; \
51 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
52 (futexp), FUTEX_WAIT, (val), 0); \
53 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
56 #define lll_futex_timed_wait(futexp, val, timespec) \
57 ({ \
58 INTERNAL_SYSCALL_DECL (__err); \
59 long int __ret; \
61 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
62 (futexp), FUTEX_WAIT, (val), (timespec)); \
63 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
66 #define lll_futex_wake(futexp, nr) \
67 ({ \
68 INTERNAL_SYSCALL_DECL (__err); \
69 long int __ret; \
71 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
72 (futexp), FUTEX_WAKE, (nr), 0); \
73 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
76 #define lll_robust_mutex_dead(futexv) \
77 do \
78 { \
79 INTERNAL_SYSCALL_DECL (__err); \
80 int *__futexp = &(futexv); \
82 atomic_or (__futexp, FUTEX_OWNER_DIED); \
83 INTERNAL_SYSCALL (futex, __err, 4, __futexp, FUTEX_WAKE, 1, 0); \
84 } \
85 while (0)
87 /* Returns non-zero if error happened, zero if success. */
88 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
89 ({ \
90 INTERNAL_SYSCALL_DECL (__err); \
91 long int __ret; \
93 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
94 (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
95 (nr_move), (mutex), (val)); \
96 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
99 /* Returns non-zero if error happened, zero if success. */
100 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) \
101 ({ \
102 INTERNAL_SYSCALL_DECL (__err); \
103 long int __ret; \
105 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
106 (futexp), FUTEX_WAKE_OP, (nr_wake), \
107 (nr_wake2), (futexp2), \
108 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
109 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
112 #ifdef UP
113 # define __lll_acq_instr ""
114 # define __lll_rel_instr ""
115 #else
116 # define __lll_acq_instr "isync"
117 # ifdef _ARCH_PWR4
119 * Newer powerpc64 processors support the new "light weight" sync (lwsync)
120 * So if the build is using -mcpu=[power4,power5,power5+,970] we can
121 * safely use lwsync.
123 # define __lll_rel_instr "lwsync"
124 # else
126 * Older powerpc32 processors don't support the new "light weight"
127 * sync (lwsync). So the only safe option is to use normal sync
128 * for all powerpc32 applications.
130 # define __lll_rel_instr "sync"
131 # endif
132 #endif
134 /* Set *futex to ID if it is 0, atomically. Returns the old value */
135 #define __lll_robust_trylock(futex, id) \
136 ({ int __val; \
137 __asm __volatile ("1: lwarx %0,0,%2" MUTEX_HINT_ACQ "\n" \
138 " cmpwi 0,%0,0\n" \
139 " bne 2f\n" \
140 " stwcx. %3,0,%2\n" \
141 " bne- 1b\n" \
142 "2: " __lll_acq_instr \
143 : "=&r" (__val), "=m" (*futex) \
144 : "r" (futex), "r" (id), "m" (*futex) \
145 : "cr0", "memory"); \
146 __val; \
149 #define lll_robust_mutex_trylock(lock, id) __lll_robust_trylock (&(lock), id)
151 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
152 #define __lll_trylock(futex) __lll_robust_trylock (futex, 1)
154 #define lll_mutex_trylock(lock) __lll_trylock (&(lock))
156 /* Set *futex to 2 if it is 0, atomically. Returns the old value */
157 #define __lll_cond_trylock(futex) __lll_robust_trylock (futex, 2)
159 #define lll_mutex_cond_trylock(lock) __lll_cond_trylock (&(lock))
162 extern void __lll_lock_wait (int *futex) attribute_hidden;
163 extern int __lll_robust_lock_wait (int *futex) attribute_hidden;
165 #define lll_mutex_lock(lock) \
166 (void) ({ \
167 int *__futex = &(lock); \
168 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
169 0) != 0) \
170 __lll_lock_wait (__futex); \
173 #define lll_robust_mutex_lock(lock, id) \
174 ({ \
175 int *__futex = &(lock); \
176 int __val = 0; \
177 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
178 0), 0)) \
179 __val = __lll_robust_lock_wait (__futex); \
180 __val; \
183 #define lll_mutex_cond_lock(lock) \
184 (void) ({ \
185 int *__futex = &(lock); \
186 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
187 0) != 0) \
188 __lll_lock_wait (__futex); \
191 #define lll_robust_mutex_cond_lock(lock, id) \
192 ({ \
193 int *__futex = &(lock); \
194 int __val = 0; \
195 int __id = id | FUTEX_WAITERS; \
196 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
197 0), 0)) \
198 __val = __lll_robust_lock_wait (__futex); \
199 __val; \
203 extern int __lll_timedlock_wait
204 (int *futex, const struct timespec *) attribute_hidden;
205 extern int __lll_robust_timedlock_wait
206 (int *futex, const struct timespec *) attribute_hidden;
208 #define lll_mutex_timedlock(lock, abstime) \
209 ({ \
210 int *__futex = &(lock); \
211 int __val = 0; \
212 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
213 0) != 0) \
214 __val = __lll_timedlock_wait (__futex, abstime); \
215 __val; \
218 #define lll_robust_mutex_timedlock(lock, abstime, id) \
219 ({ \
220 int *__futex = &(lock); \
221 int __val = 0; \
222 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
223 0), 0)) \
224 __val = __lll_robust_timedlock_wait (__futex, abstime); \
225 __val; \
228 #define lll_mutex_unlock(lock) \
229 ((void) ({ \
230 int *__futex = &(lock); \
231 int __val = atomic_exchange_rel (__futex, 0); \
232 if (__builtin_expect (__val > 1, 0)) \
233 lll_futex_wake (__futex, 1); \
236 #define lll_robust_mutex_unlock(lock) \
237 ((void) ({ \
238 int *__futex = &(lock); \
239 int __val = atomic_exchange_rel (__futex, 0); \
240 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
241 lll_futex_wake (__futex, 1); \
244 #define lll_mutex_unlock_force(lock) \
245 ((void) ({ \
246 int *__futex = &(lock); \
247 *__futex = 0; \
248 __asm __volatile (__lll_rel_instr ::: "memory"); \
249 lll_futex_wake (__futex, 1); \
252 #define lll_mutex_islocked(futex) \
253 (futex != 0)
256 /* Our internal lock implementation is identical to the binary-compatible
257 mutex implementation. */
259 /* Type for lock object. */
260 typedef int lll_lock_t;
262 /* Initializers for lock. */
263 #define LLL_LOCK_INITIALIZER (0)
264 #define LLL_LOCK_INITIALIZER_LOCKED (1)
266 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
268 /* The states of a lock are:
269 0 - untaken
270 1 - taken by one user
271 >1 - taken by more users */
273 #define lll_trylock(lock) lll_mutex_trylock (lock)
274 #define lll_lock(lock) lll_mutex_lock (lock)
275 #define lll_unlock(lock) lll_mutex_unlock (lock)
276 #define lll_islocked(lock) lll_mutex_islocked (lock)
278 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
279 wakeup when the clone terminates. The memory location contains the
280 thread ID while the clone is running and is reset to zero
281 afterwards. */
282 #define lll_wait_tid(tid) \
283 do { \
284 __typeof (tid) __tid; \
285 while ((__tid = (tid)) != 0) \
286 lll_futex_wait (&(tid), __tid); \
287 } while (0)
289 extern int __lll_timedwait_tid (int *, const struct timespec *)
290 attribute_hidden;
292 #define lll_timedwait_tid(tid, abstime) \
293 ({ \
294 int __res = 0; \
295 if ((tid) != 0) \
296 __res = __lll_timedwait_tid (&(tid), (abstime)); \
297 __res; \
301 /* Conditional variable handling. */
303 extern void __lll_cond_wait (pthread_cond_t *cond)
304 attribute_hidden;
305 extern int __lll_cond_timedwait (pthread_cond_t *cond,
306 const struct timespec *abstime)
307 attribute_hidden;
308 extern void __lll_cond_wake (pthread_cond_t *cond)
309 attribute_hidden;
310 extern void __lll_cond_broadcast (pthread_cond_t *cond)
311 attribute_hidden;
313 #define lll_cond_wait(cond) \
314 __lll_cond_wait (cond)
315 #define lll_cond_timedwait(cond, abstime) \
316 __lll_cond_timedwait (cond, abstime)
317 #define lll_cond_wake(cond) \
318 __lll_cond_wake (cond)
319 #define lll_cond_broadcast(cond) \
320 __lll_cond_broadcast (cond)
322 #endif /* lowlevellock.h */