1 /* Copyright (C) 2003, 2004, 2006 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>
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
42 /* Initializer for compatibility lock. */
43 #define LLL_MUTEX_LOCK_INITIALIZER (0)
45 #define lll_futex_wait(futexp, val) \
47 INTERNAL_SYSCALL_DECL (__err); \
50 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
51 (futexp), FUTEX_WAIT, (val), 0); \
52 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
55 #define lll_futex_timed_wait(futexp, val, timespec) \
57 INTERNAL_SYSCALL_DECL (__err); \
60 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
61 (futexp), FUTEX_WAIT, (val), (timespec)); \
62 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
65 #define lll_futex_wake(futexp, nr) \
67 INTERNAL_SYSCALL_DECL (__err); \
70 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
71 (futexp), FUTEX_WAKE, (nr), 0); \
72 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
75 #define lll_robust_mutex_dead(futexv) \
78 INTERNAL_SYSCALL_DECL (__err); \
79 int *__futexp = &(futexv); \
81 atomic_or (__futexp, FUTEX_OWNER_DIED); \
82 INTERNAL_SYSCALL (futex, __err, 4, __futexp, FUTEX_WAKE, 1, 0); \
86 /* Returns non-zero if error happened, zero if success. */
87 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
89 INTERNAL_SYSCALL_DECL (__err); \
92 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
93 (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
94 (nr_move), (mutex), (val)); \
95 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
98 /* Returns non-zero if error happened, zero if success. */
99 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) \
101 INTERNAL_SYSCALL_DECL (__err); \
104 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
105 (futexp), FUTEX_WAKE_OP, (nr_wake), \
106 (nr_wake2), (futexp2), \
107 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
108 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
112 # define __lll_acq_instr ""
113 # define __lll_rel_instr ""
115 # define __lll_acq_instr "isync"
118 * Newer powerpc64 processors support the new "light weight" sync (lwsync)
119 * So if the build is using -mcpu=[power4,power5,power5+,970] we can
122 # define __lll_rel_instr "lwsync"
125 * Older powerpc32 processors don't support the new "light weight"
126 * sync (lwsync). So the only safe option is to use normal sync
127 * for all powerpc32 applications.
129 # define __lll_rel_instr "sync"
133 /* Set *futex to ID if it is 0, atomically. Returns the old value */
134 #define __lll_robust_trylock(futex, id) \
136 __asm __volatile ("1: lwarx %0,0,%2\n" \
139 " stwcx. %3,0,%2\n" \
141 "2: " __lll_acq_instr \
142 : "=&r" (__val), "=m" (*futex) \
143 : "r" (futex), "r" (id), "m" (*futex) \
144 : "cr0", "memory"); \
148 #define lll_robust_mutex_trylock(lock, id) __lll_robust_trylock (&(lock), id)
150 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
151 #define __lll_trylock(futex) __lll_robust_trylock (futex, 1)
153 #define lll_mutex_trylock(lock) __lll_trylock (&(lock))
155 /* Set *futex to 2 if it is 0, atomically. Returns the old value */
156 #define __lll_cond_trylock(futex) __lll_robust_trylock (futex, 2)
158 #define lll_mutex_cond_trylock(lock) __lll_cond_trylock (&(lock))
161 extern void __lll_lock_wait (int *futex) attribute_hidden;
162 extern int __lll_robust_lock_wait (int *futex) attribute_hidden;
164 #define lll_mutex_lock(lock) \
166 int *__futex = &(lock); \
167 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
169 __lll_lock_wait (__futex); \
172 #define lll_robust_mutex_lock(lock, id) \
174 int *__futex = &(lock); \
176 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
178 __val = __lll_robust_lock_wait (__futex); \
182 #define lll_mutex_cond_lock(lock) \
184 int *__futex = &(lock); \
185 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
187 __lll_lock_wait (__futex); \
190 #define lll_robust_mutex_cond_lock(lock, id) \
192 int *__futex = &(lock); \
194 int __id = id | FUTEX_WAITERS; \
195 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
197 __val = __lll_robust_lock_wait (__futex); \
202 extern int __lll_timedlock_wait
203 (int *futex, const struct timespec *) attribute_hidden;
204 extern int __lll_robust_timedlock_wait
205 (int *futex, const struct timespec *) attribute_hidden;
207 #define lll_mutex_timedlock(lock, abstime) \
209 int *__futex = &(lock); \
211 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
213 __val = __lll_timedlock_wait (__futex, abstime); \
217 #define lll_robust_mutex_timedlock(lock, abstime, id) \
219 int *__futex = &(lock); \
221 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
223 __val = __lll_robust_timedlock_wait (__futex, abstime); \
227 #define lll_mutex_unlock(lock) \
229 int *__futex = &(lock); \
230 int __val = atomic_exchange_rel (__futex, 0); \
231 if (__builtin_expect (__val > 1, 0)) \
232 lll_futex_wake (__futex, 1); \
235 #define lll_robust_mutex_unlock(lock) \
237 int *__futex = &(lock); \
238 int __val = atomic_exchange_rel (__futex, 0); \
239 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
240 lll_futex_wake (__futex, 1); \
243 #define lll_mutex_unlock_force(lock) \
245 int *__futex = &(lock); \
247 __asm __volatile (__lll_rel_instr ::: "memory"); \
248 lll_futex_wake (__futex, 1); \
251 #define lll_mutex_islocked(futex) \
255 /* Our internal lock implementation is identical to the binary-compatible
256 mutex implementation. */
258 /* Type for lock object. */
259 typedef int lll_lock_t;
261 /* Initializers for lock. */
262 #define LLL_LOCK_INITIALIZER (0)
263 #define LLL_LOCK_INITIALIZER_LOCKED (1)
265 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
267 /* The states of a lock are:
269 1 - taken by one user
270 >1 - taken by more users */
272 #define lll_trylock(lock) lll_mutex_trylock (lock)
273 #define lll_lock(lock) lll_mutex_lock (lock)
274 #define lll_unlock(lock) lll_mutex_unlock (lock)
275 #define lll_islocked(lock) lll_mutex_islocked (lock)
277 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
278 wakeup when the clone terminates. The memory location contains the
279 thread ID while the clone is running and is reset to zero
281 #define lll_wait_tid(tid) \
283 __typeof (tid) __tid; \
284 while ((__tid = (tid)) != 0) \
285 lll_futex_wait (&(tid), __tid); \
288 extern int __lll_timedwait_tid (int *, const struct timespec *)
291 #define lll_timedwait_tid(tid, abstime) \
295 __res = __lll_timedwait_tid (&(tid), (abstime)); \
300 /* Conditional variable handling. */
302 extern void __lll_cond_wait (pthread_cond_t *cond)
304 extern int __lll_cond_timedwait (pthread_cond_t *cond,
305 const struct timespec *abstime)
307 extern void __lll_cond_wake (pthread_cond_t *cond)
309 extern void __lll_cond_broadcast (pthread_cond_t *cond)
312 #define lll_cond_wait(cond) \
313 __lll_cond_wait (cond)
314 #define lll_cond_timedwait(cond, abstime) \
315 __lll_cond_timedwait (cond, abstime)
316 #define lll_cond_wake(cond) \
317 __lll_cond_wake (cond)
318 #define lll_cond_broadcast(cond) \
319 __lll_cond_broadcast (cond)
321 #endif /* lowlevellock.h */