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>
29 # define __NR_futex 221
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
42 /* Values for 'private' parameter of locking macros. Yes, the
43 definition seems to be backwards. But it is not. The bit will be
44 reversed before passing to the system call. */
46 #define LLL_SHARED FUTEX_PRIVATE_FLAG
49 /* Initializer for compatibility lock. */
50 #define LLL_MUTEX_LOCK_INITIALIZER (0)
52 #define lll_futex_wait(futexp, val, private) \
54 INTERNAL_SYSCALL_DECL (__err); \
55 long int opt_flags = (FUTEX_WAIT | LLL_SHARED) ^ private; \
58 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
59 (futexp), opt_flags, (val), 0); \
60 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
63 #define lll_futex_timed_wait(futexp, val, timespec, private) \
65 INTERNAL_SYSCALL_DECL (__err); \
66 long int opt_flags = (FUTEX_WAIT | LLL_SHARED) ^ private; \
69 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
70 (futexp), opt_flags, (val), (timespec)); \
71 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
74 #define lll_futex_wake(futexp, nr, private) \
76 INTERNAL_SYSCALL_DECL (__err); \
77 long int opt_flags = (FUTEX_WAKE | LLL_SHARED) ^ private; \
80 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
81 (futexp), opt_flags, (nr), 0); \
82 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
85 #define lll_robust_mutex_dead(futexv) \
88 INTERNAL_SYSCALL_DECL (__err); \
89 int *__futexp = &(futexv); \
91 atomic_or (__futexp, FUTEX_OWNER_DIED); \
92 INTERNAL_SYSCALL (futex, __err, 4, __futexp, FUTEX_WAKE, 1, 0); \
96 /* Returns non-zero if error happened, zero if success. */
97 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
99 INTERNAL_SYSCALL_DECL (__err); \
102 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
103 (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
104 (nr_move), (mutex), (val)); \
105 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
108 /* Returns non-zero if error happened, zero if success. */
109 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
111 INTERNAL_SYSCALL_DECL (__err); \
112 long int opt_flags = (FUTEX_WAKE_OP | LLL_SHARED) ^ private; \
113 long int opt_flag2 = (FUTEX_OP_CLEAR_WAKE_IF_GT_ONE | LLL_SHARED) \
117 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
118 (futexp), opt_flags, (nr_wake), \
119 (nr_wake2), (futexp2), \
121 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
125 #define lll_private_futex_wait(futexp, val) \
126 lll_private_futex_timed_wait (futexp, val, NULL)
129 #ifdef __ASSUME_PRIVATE_FUTEX
130 # define lll_private_futex_timed_wait(futexp, val, timeout) \
132 INTERNAL_SYSCALL_DECL (__err); \
135 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
136 (futexp), (FUTEX_WAIT | FUTEX_PRIVATE_FLAG), \
138 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
141 # define lll_private_futex_wake(futexp, val) \
143 INTERNAL_SYSCALL_DECL (__err); \
146 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
147 (futexp), (FUTEX_WAKE | FUTEX_PRIVATE_FLAG), \
149 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
152 # define lll_private_futex_timed_wait(futexp, val, timeout) \
154 INTERNAL_SYSCALL_DECL (__err); \
157 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
158 (futexp), FUTEX_WAIT, (val), (timeout)); \
159 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
162 # define lll_private_futex_wake(futexp, val) \
164 INTERNAL_SYSCALL_DECL (__err); \
167 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
168 (futexp), FUTEX_WAKE, (val), 0); \
169 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
174 # define __lll_acq_instr ""
175 # define __lll_rel_instr ""
177 # define __lll_acq_instr "isync"
180 * Newer powerpc64 processors support the new "light weight" sync (lwsync)
181 * So if the build is using -mcpu=[power4,power5,power5+,970] we can
184 # define __lll_rel_instr "lwsync"
187 * Older powerpc32 processors don't support the new "light weight"
188 * sync (lwsync). So the only safe option is to use normal sync
189 * for all powerpc32 applications.
191 # define __lll_rel_instr "sync"
195 /* Set *futex to ID if it is 0, atomically. Returns the old value */
196 #define __lll_robust_trylock(futex, id) \
198 __asm __volatile ("1: lwarx %0,0,%2" MUTEX_HINT_ACQ "\n" \
201 " stwcx. %3,0,%2\n" \
203 "2: " __lll_acq_instr \
204 : "=&r" (__val), "=m" (*futex) \
205 : "r" (futex), "r" (id), "m" (*futex) \
206 : "cr0", "memory"); \
210 #define lll_robust_mutex_trylock(lock, id) __lll_robust_trylock (&(lock), id)
212 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
213 #define __lll_trylock(futex) __lll_robust_trylock (futex, 1)
215 #define lll_mutex_trylock(lock) __lll_trylock (&(lock))
217 /* Set *futex to 2 if it is 0, atomically. Returns the old value */
218 #define __lll_cond_trylock(futex) __lll_robust_trylock (futex, 2)
220 #define lll_mutex_cond_trylock(lock) __lll_cond_trylock (&(lock))
223 extern void __lll_lock_wait (int *futex) attribute_hidden;
224 extern int __lll_robust_lock_wait (int *futex) attribute_hidden;
226 #define lll_mutex_lock(lock) \
228 int *__futex = &(lock); \
229 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
231 __lll_lock_wait (__futex); \
234 #define lll_robust_mutex_lock(lock, id) \
236 int *__futex = &(lock); \
238 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
240 __val = __lll_robust_lock_wait (__futex); \
244 #define lll_mutex_cond_lock(lock) \
246 int *__futex = &(lock); \
247 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
249 __lll_lock_wait (__futex); \
252 #define lll_robust_mutex_cond_lock(lock, id) \
254 int *__futex = &(lock); \
256 int __id = id | FUTEX_WAITERS; \
257 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
259 __val = __lll_robust_lock_wait (__futex); \
264 extern int __lll_timedlock_wait
265 (int *futex, const struct timespec *) attribute_hidden;
266 extern int __lll_robust_timedlock_wait
267 (int *futex, const struct timespec *) attribute_hidden;
269 #define lll_mutex_timedlock(lock, abstime) \
271 int *__futex = &(lock); \
273 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
275 __val = __lll_timedlock_wait (__futex, abstime); \
279 #define lll_robust_mutex_timedlock(lock, abstime, id) \
281 int *__futex = &(lock); \
283 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
285 __val = __lll_robust_timedlock_wait (__futex, abstime); \
289 #define lll_mutex_unlock(lock) \
291 int *__futex = &(lock); \
292 int __val = atomic_exchange_rel (__futex, 0); \
293 if (__builtin_expect (__val > 1, 0)) \
294 lll_futex_wake (__futex, 1, LLL_SHARED); \
297 #define lll_robust_mutex_unlock(lock) \
299 int *__futex = &(lock); \
300 int __val = atomic_exchange_rel (__futex, 0); \
301 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
302 lll_futex_wake (__futex, 1, LLL_SHARED); \
305 #define lll_mutex_unlock_force(lock) \
307 int *__futex = &(lock); \
309 __asm __volatile (__lll_rel_instr ::: "memory"); \
310 lll_futex_wake (__futex, 1, LLL_SHARED); \
313 #define lll_mutex_islocked(futex) \
317 /* Our internal lock implementation is identical to the binary-compatible
318 mutex implementation. */
320 /* Type for lock object. */
321 typedef int lll_lock_t;
323 /* Initializers for lock. */
324 #define LLL_LOCK_INITIALIZER (0)
325 #define LLL_LOCK_INITIALIZER_LOCKED (1)
327 /* The states of a lock are:
329 1 - taken by one user
330 >1 - taken by more users */
332 #define lll_trylock(lock) lll_mutex_trylock (lock)
333 #define lll_lock(lock) lll_mutex_lock (lock)
334 #define lll_unlock(lock) lll_mutex_unlock (lock)
335 #define lll_islocked(lock) lll_mutex_islocked (lock)
337 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
338 wakeup when the clone terminates. The memory location contains the
339 thread ID while the clone is running and is reset to zero
341 #define lll_wait_tid(tid) \
343 __typeof (tid) __tid; \
344 while ((__tid = (tid)) != 0) \
345 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
348 extern int __lll_timedwait_tid (int *, const struct timespec *)
351 #define lll_timedwait_tid(tid, abstime) \
355 __res = __lll_timedwait_tid (&(tid), (abstime)); \
360 /* Conditional variable handling. */
362 extern void __lll_cond_wait (pthread_cond_t *cond)
364 extern int __lll_cond_timedwait (pthread_cond_t *cond,
365 const struct timespec *abstime)
367 extern void __lll_cond_wake (pthread_cond_t *cond)
369 extern void __lll_cond_broadcast (pthread_cond_t *cond)
372 #define lll_cond_wait(cond) \
373 __lll_cond_wait (cond)
374 #define lll_cond_timedwait(cond, abstime) \
375 __lll_cond_timedwait (cond, abstime)
376 #define lll_cond_wake(cond) \
377 __lll_cond_wake (cond)
378 #define lll_cond_broadcast(cond) \
379 __lll_cond_broadcast (cond)
381 #endif /* lowlevellock.h */