1 /* Copyright (C) 2003, 2004 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>
31 /* Initializer for compatibility lock. */
32 #define LLL_MUTEX_LOCK_INITIALIZER (0)
33 #define LLL_MUTEX_LOCK_INITIALIZER_LOCKED (1)
34 #define LLL_MUTEX_LOCK_INITIALIZER_WAITERS (2)
36 extern int __lll_mutex_lock_wait (int val
, int *__futex
) attribute_hidden
;
37 extern int __lll_mutex_timedlock_wait (int val
, int *__futex
,
38 const struct timespec
*abstime
)
40 extern int __lll_mutex_unlock_wake (int *__futex
) attribute_hidden
;
43 #define lll_mutex_trylock(futex) \
44 ({ unsigned char __result; \
60 "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), \
61 "r" (LLL_MUTEX_LOCK_INITIALIZER) \
62 : "r0", "r1", "r2", "t", "memory"); \
65 #define lll_mutex_cond_trylock(futex) \
66 ({ unsigned char __result; \
82 "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
83 "r" (LLL_MUTEX_LOCK_INITIALIZER) \
84 : "r0", "r1", "r2", "t", "memory"); \
87 #define lll_mutex_lock(futex) \
88 (void) ({ int __result, val, *__futex = &(futex); \
100 : "=&r" (__result) : "r" (1), "r" (__futex) \
101 : "r0", "r1", "t", "memory"); \
103 __lll_mutex_lock_wait (__result, __futex); })
105 /* Special version of lll_mutex_lock which causes the unlock function to
106 always wakeup waiters. */
107 #define lll_mutex_cond_lock(futex) \
108 (void) ({ int __result, val, *__futex = &(futex); \
120 : "=&r" (__result) : "r" (2), "r" (__futex) \
121 : "r0", "r1", "t", "memory"); \
123 __lll_mutex_lock_wait (__result, __futex); })
125 #define lll_mutex_timedlock(futex, timeout) \
126 ({ int __result, val, *__futex = &(futex); \
138 : "=&r" (__result) : "r" (1), "r" (__futex) \
139 : "r0", "r1", "t", "memory"); \
141 __result = __lll_mutex_timedlock_wait (__result, __futex, timeout); \
144 #define lll_mutex_unlock(futex) \
145 (void) ({ int __result, *__futex = &(futex); \
155 : "=&r" (__result) : "r" (__futex) \
156 : "r0", "r1", "memory"); \
158 __lll_mutex_unlock_wake (__futex); })
160 #define lll_mutex_islocked(futex) \
164 /* We have a separate internal lock implementation which is not tied
165 to binary compatibility. */
167 /* Type for lock object. */
168 typedef int lll_lock_t
;
170 /* Initializers for lock. */
171 #define LLL_LOCK_INITIALIZER (0)
172 #define LLL_LOCK_INITIALIZER_LOCKED (1)
175 # ifdef NEED_SYSCALL_INST_PAD
176 # define SYSCALL_WITH_INST_PAD "\
177 trapa #0x14; or r0,r0; or r0,r0; or r0,r0; or r0,r0; or r0,r0"
179 # define SYSCALL_WITH_INST_PAD "\
183 #define lll_futex_wait(futex, val) \
186 register unsigned long __r3 asm ("r3") = SYS_futex; \
187 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
188 register unsigned long __r5 asm ("r5") = FUTEX_WAIT; \
189 register unsigned long __r6 asm ("r6") = (unsigned long) (val); \
190 register unsigned long __r7 asm ("r7") = 0; \
191 __asm __volatile (SYSCALL_WITH_INST_PAD \
193 : "r" (__r3), "r" (__r4), "r" (__r5), \
194 "r" (__r6), "r" (__r7) \
199 #define lll_futex_wake(futex, nr) \
202 register unsigned long __r3 asm ("r3") = SYS_futex; \
203 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
204 register unsigned long __r5 asm ("r5") = FUTEX_WAKE; \
205 register unsigned long __r6 asm ("r6") = (unsigned long) (nr); \
206 register unsigned long __r7 asm ("r7") = 0; \
207 __asm __volatile (SYSCALL_WITH_INST_PAD \
209 : "r" (__r3), "r" (__r4), "r" (__r5), \
210 "r" (__r6), "r" (__r7) \
215 extern int lll_unlock_wake_cb (int *__futex
) attribute_hidden
;
218 /* The states of a lock are:
220 1 - taken by one user
221 2 - taken by more users */
223 #define lll_trylock(futex) lll_mutex_trylock (futex)
224 #define lll_lock(futex) lll_mutex_lock (futex)
225 #define lll_unlock(futex) lll_mutex_unlock (futex)
227 #define lll_islocked(futex) \
228 (futex != LLL_LOCK_INITIALIZER)
231 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
232 wakeup when the clone terminates. The memory location contains the
233 thread ID while the clone is running and is reset to zero
236 extern int __lll_wait_tid (int *tid
) attribute_hidden
;
237 #define lll_wait_tid(tid) \
239 __typeof (tid) *__tid = &(tid); \
240 while (*__tid != 0) \
241 lll_futex_wait (__tid, *__tid); \
244 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
246 #define lll_timedwait_tid(tid, abstime) \
251 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
254 __result = __lll_timedwait_tid (&tid, abstime); \
259 /* Conditional variable handling. */
261 extern void __lll_cond_wait (pthread_cond_t
*cond
) attribute_hidden
;
262 extern int __lll_cond_timedwait (pthread_cond_t
*cond
,
263 const struct timespec
*abstime
)
265 extern void __lll_cond_wake (pthread_cond_t
*cond
) attribute_hidden
;
266 extern void __lll_cond_broadcast (pthread_cond_t
*cond
) attribute_hidden
;
269 #define lll_cond_wait(cond) \
270 __lll_cond_wait (cond)
271 #define lll_cond_timedwait(cond, abstime) \
272 __lll_cond_timedwait (cond, abstime)
273 #define lll_cond_wake(cond) \
274 __lll_cond_wake (cond)
275 #define lll_cond_broadcast(cond) \
276 __lll_cond_broadcast (cond)
278 #endif /* lowlevellock.h */