1 /* Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
23 #include <sys/param.h>
24 #include <bits/pthreadtypes.h>
29 #define FUTEX_LOCK_PI 6
30 #define FUTEX_UNLOCK_PI 7
31 #define FUTEX_TRYLOCK_PI 8
34 /* Initializer for compatibility lock. */
35 #define LLL_MUTEX_LOCK_INITIALIZER (0)
36 #define LLL_MUTEX_LOCK_INITIALIZER_LOCKED (1)
37 #define LLL_MUTEX_LOCK_INITIALIZER_WAITERS (2)
39 extern int __lll_mutex_lock_wait (int val
, int *__futex
) attribute_hidden
;
40 extern int __lll_mutex_timedlock_wait (int val
, int *__futex
,
41 const struct timespec
*abstime
)
43 extern int __lll_mutex_unlock_wake (int *__futex
) attribute_hidden
;
46 #define lll_mutex_trylock(futex) \
47 ({ unsigned char __result; \
63 "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), \
64 "r" (LLL_MUTEX_LOCK_INITIALIZER) \
65 : "r0", "r1", "r2", "t", "memory"); \
68 #define lll_robust_mutex_trylock(futex, id) \
69 ({ unsigned char __result; \
86 "r" (LLL_MUTEX_LOCK_INITIALIZER) \
87 : "r0", "r1", "r2", "t", "memory"); \
90 #define lll_mutex_cond_trylock(futex) \
91 ({ unsigned char __result; \
107 "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
108 "r" (LLL_MUTEX_LOCK_INITIALIZER) \
109 : "r0", "r1", "r2", "t", "memory"); \
112 #define lll_mutex_lock(futex) \
113 (void) ({ int __result, val, *__futex = &(futex); \
125 : "=&r" (__result) : "r" (1), "r" (__futex) \
126 : "r0", "r1", "t", "memory"); \
128 __lll_mutex_lock_wait (__result, __futex); })
130 #define lll_robust_mutex_lock(futex, id) \
131 ({ int __result, val, *__futex = &(futex); \
143 : "=&r" (__result) : "r" (id), "r" (__futex) \
144 : "r0", "r1", "t", "memory"); \
146 __result = __lll_robust_mutex_lock_wait (__result, __futex); \
149 /* Special version of lll_mutex_lock which causes the unlock function to
150 always wakeup waiters. */
151 #define lll_mutex_cond_lock(futex) \
152 (void) ({ int __result, val, *__futex = &(futex); \
164 : "=&r" (__result) : "r" (2), "r" (__futex) \
165 : "r0", "r1", "t", "memory"); \
167 __lll_mutex_lock_wait (__result, __futex); })
169 #define lll_robust_mutex_cond_lock(futex, id) \
170 ({ int __result, val, *__futex = &(futex); \
182 : "=&r" (__result) : "r" (id | FUTEX_WAITERS), "r" (__futex) \
183 : "r0", "r1", "t", "memory"); \
185 __result = __lll_robust_mutex_lock_wait (__result, __futex); \
188 #define lll_mutex_timedlock(futex, timeout) \
189 ({ int __result, val, *__futex = &(futex); \
201 : "=&r" (__result) : "r" (1), "r" (__futex) \
202 : "r0", "r1", "t", "memory"); \
204 __result = __lll_mutex_timedlock_wait (__result, __futex, timeout); \
207 #define lll_robust_mutex_timedlock(futex, timeout, id) \
208 ({ int __result, val, *__futex = &(futex); \
220 : "=&r" (__result) : "r" (id), "r" (__futex) \
221 : "r0", "r1", "t", "memory"); \
223 __result = __lll_robust_mutex_timedlock_wait (__result, __futex, \
227 #define lll_mutex_unlock(futex) \
228 (void) ({ int __result, *__futex = &(futex); \
238 : "=&r" (__result) : "r" (__futex) \
239 : "r0", "r1", "memory"); \
241 __lll_mutex_unlock_wake (__futex); })
243 #define lll_robust_mutex_unlock(futex) \
244 (void) ({ int __result, *__futex = &(futex); \
254 : "=&r" (__result) : "r" (__futex), "r" (FUTEX_TID_MASK) \
255 : "r0", "r1", "memory"); \
257 __lll_mutex_unlock_wake (__futex); })
259 #define lll_robust_mutex_dead(futex) \
260 (void) ({ int __ignore, *__futex = &(futex); \
270 : "=&r" (__ignore) : "r" (__futex), "r" (FUTEX_OWNER_DIED) \
271 : "r0", "r1", "memory"); \
272 lll_futex_wake (__futex, 1); })
274 #define lll_mutex_islocked(futex) \
278 /* We have a separate internal lock implementation which is not tied
279 to binary compatibility. */
281 /* Type for lock object. */
282 typedef int lll_lock_t
;
284 /* Initializers for lock. */
285 #define LLL_LOCK_INITIALIZER (0)
286 #define LLL_LOCK_INITIALIZER_LOCKED (1)
289 # ifdef NEED_SYSCALL_INST_PAD
290 # define SYSCALL_WITH_INST_PAD "\
291 trapa #0x14; or r0,r0; or r0,r0; or r0,r0; or r0,r0; or r0,r0"
293 # define SYSCALL_WITH_INST_PAD "\
297 #define lll_futex_wait(futex, val) \
300 register unsigned long __r3 asm ("r3") = SYS_futex; \
301 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
302 register unsigned long __r5 asm ("r5") = FUTEX_WAIT; \
303 register unsigned long __r6 asm ("r6") = (unsigned long) (val); \
304 register unsigned long __r7 asm ("r7") = 0; \
305 __asm __volatile (SYSCALL_WITH_INST_PAD \
307 : "r" (__r3), "r" (__r4), "r" (__r5), \
308 "r" (__r6), "r" (__r7) \
314 #define lll_futex_timed_wait(futex, val, timeout) \
317 register unsigned long __r3 asm ("r3") = SYS_futex; \
318 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
319 register unsigned long __r5 asm ("r5") = FUTEX_WAIT; \
320 register unsigned long __r6 asm ("r6") = (unsigned long) (val); \
321 register unsigned long __r7 asm ("r7") = (timeout); \
322 __asm __volatile (SYSCALL_WITH_INST_PAD \
324 : "r" (__r3), "r" (__r4), "r" (__r5), \
325 "r" (__r6), "r" (__r7) \
331 #define lll_futex_wake(futex, nr) \
334 register unsigned long __r3 asm ("r3") = SYS_futex; \
335 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
336 register unsigned long __r5 asm ("r5") = FUTEX_WAKE; \
337 register unsigned long __r6 asm ("r6") = (unsigned long) (nr); \
338 register unsigned long __r7 asm ("r7") = 0; \
339 __asm __volatile (SYSCALL_WITH_INST_PAD \
341 : "r" (__r3), "r" (__r4), "r" (__r5), \
342 "r" (__r6), "r" (__r7) \
347 extern int lll_unlock_wake_cb (int *__futex
) attribute_hidden
;
350 /* The states of a lock are:
352 1 - taken by one user
353 2 - taken by more users */
355 #define lll_trylock(futex) lll_mutex_trylock (futex)
356 #define lll_lock(futex) lll_mutex_lock (futex)
357 #define lll_unlock(futex) lll_mutex_unlock (futex)
359 #define lll_islocked(futex) \
360 (futex != LLL_LOCK_INITIALIZER)
363 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
364 wakeup when the clone terminates. The memory location contains the
365 thread ID while the clone is running and is reset to zero
368 extern int __lll_wait_tid (int *tid
) attribute_hidden
;
369 #define lll_wait_tid(tid) \
371 __typeof (tid) *__tid = &(tid); \
372 while (*__tid != 0) \
373 lll_futex_wait (__tid, *__tid); \
376 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
378 #define lll_timedwait_tid(tid, abstime) \
383 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
386 __result = __lll_timedwait_tid (&tid, abstime); \
391 /* Conditional variable handling. */
393 extern void __lll_cond_wait (pthread_cond_t
*cond
) attribute_hidden
;
394 extern int __lll_cond_timedwait (pthread_cond_t
*cond
,
395 const struct timespec
*abstime
)
397 extern void __lll_cond_wake (pthread_cond_t
*cond
) attribute_hidden
;
398 extern void __lll_cond_broadcast (pthread_cond_t
*cond
) attribute_hidden
;
401 #define lll_cond_wait(cond) \
402 __lll_cond_wait (cond)
403 #define lll_cond_timedwait(cond, abstime) \
404 __lll_cond_timedwait (cond, abstime)
405 #define lll_cond_wake(cond) \
406 __lll_cond_wake (cond)
407 #define lll_cond_broadcast(cond) \
408 __lll_cond_broadcast (cond)
410 #endif /* lowlevellock.h */