2.5-18.1
[glibc.git] / nptl / sysdeps / unix / sysv / linux / sh / lowlevellock.h
blob0eb1f0114ce3d6b94bdf235174587fe89620fabf
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
17 02111-1307 USA. */
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
22 #include <time.h>
23 #include <sys/param.h>
24 #include <bits/pthreadtypes.h>
26 #define SYS_futex 240
27 #define FUTEX_WAIT 0
28 #define FUTEX_WAKE 1
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)
42 attribute_hidden;
43 extern int __lll_mutex_unlock_wake (int *__futex) attribute_hidden;
46 #define lll_mutex_trylock(futex) \
47 ({ unsigned char __result; \
48 __asm __volatile ("\
49 .align 2\n\
50 mova 1f,r0\n\
51 nop\n\
52 mov r15,r1\n\
53 mov #-8,r15\n\
54 0: mov.l @%1,r2\n\
55 cmp/eq r2,%3\n\
56 bf 1f\n\
57 mov.l %2,@%1\n\
58 1: mov r1,r15\n\
59 mov #-1,%0\n\
60 negc %0,%0"\
61 : "=r" (__result) \
62 : "r" (&(futex)), \
63 "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), \
64 "r" (LLL_MUTEX_LOCK_INITIALIZER) \
65 : "r0", "r1", "r2", "t", "memory"); \
66 __result; })
68 #define lll_robust_mutex_trylock(futex, id) \
69 ({ unsigned char __result; \
70 __asm __volatile ("\
71 .align 2\n\
72 mova 1f,r0\n\
73 nop\n\
74 mov r15,r1\n\
75 mov #-8,r15\n\
76 0: mov.l @%1,r2\n\
77 cmp/eq r2,%3\n\
78 bf 1f\n\
79 mov.l %2,@%1\n\
80 1: mov r1,r15\n\
81 mov #-1,%0\n\
82 negc %0,%0"\
83 : "=r" (__result) \
84 : "r" (&(futex)), \
85 "r" (id), \
86 "r" (LLL_MUTEX_LOCK_INITIALIZER) \
87 : "r0", "r1", "r2", "t", "memory"); \
88 __result; })
90 #define lll_mutex_cond_trylock(futex) \
91 ({ unsigned char __result; \
92 __asm __volatile ("\
93 .align 2\n\
94 mova 1f,r0\n\
95 nop\n\
96 mov r15,r1\n\
97 mov #-8,r15\n\
98 0: mov.l @%1,r2\n\
99 cmp/eq r2,%3\n\
100 bf 1f\n\
101 mov.l %2,@%1\n\
102 1: mov r1,r15\n\
103 mov #-1,%0\n\
104 negc %0,%0"\
105 : "=r" (__result) \
106 : "r" (&(futex)), \
107 "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
108 "r" (LLL_MUTEX_LOCK_INITIALIZER) \
109 : "r0", "r1", "r2", "t", "memory"); \
110 __result; })
112 #define lll_mutex_lock(futex) \
113 (void) ({ int __result, val, *__futex = &(futex); \
114 __asm __volatile ("\
115 .align 2\n\
116 mova 1f,r0\n\
117 nop\n\
118 mov r15,r1\n\
119 mov #-8,r15\n\
120 0: mov.l @%2,%0\n\
121 tst %0,%0\n\
122 bf 1f\n\
123 mov.l %1,@%2\n\
124 1: mov r1,r15"\
125 : "=&r" (__result) : "r" (1), "r" (__futex) \
126 : "r0", "r1", "t", "memory"); \
127 if (__result) \
128 __lll_mutex_lock_wait (__result, __futex); })
130 #define lll_robust_mutex_lock(futex, id) \
131 ({ int __result, val, *__futex = &(futex); \
132 __asm __volatile ("\
133 .align 2\n\
134 mova 1f,r0\n\
135 nop\n\
136 mov r15,r1\n\
137 mov #-8,r15\n\
138 0: mov.l @%2,%0\n\
139 tst %0,%0\n\
140 bf 1f\n\
141 mov.l %1,@%2\n\
142 1: mov r1,r15"\
143 : "=&r" (__result) : "r" (id), "r" (__futex) \
144 : "r0", "r1", "t", "memory"); \
145 if (__result) \
146 __result = __lll_robust_mutex_lock_wait (__result, __futex); \
147 __result; })
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); \
153 __asm __volatile ("\
154 .align 2\n\
155 mova 1f,r0\n\
156 nop\n\
157 mov r15,r1\n\
158 mov #-8,r15\n\
159 0: mov.l @%2,%0\n\
160 tst %0,%0\n\
161 bf 1f\n\
162 mov.l %1,@%2\n\
163 1: mov r1,r15"\
164 : "=&r" (__result) : "r" (2), "r" (__futex) \
165 : "r0", "r1", "t", "memory"); \
166 if (__result) \
167 __lll_mutex_lock_wait (__result, __futex); })
169 #define lll_robust_mutex_cond_lock(futex, id) \
170 ({ int __result, val, *__futex = &(futex); \
171 __asm __volatile ("\
172 .align 2\n\
173 mova 1f,r0\n\
174 nop\n\
175 mov r15,r1\n\
176 mov #-8,r15\n\
177 0: mov.l @%2,%0\n\
178 tst %0,%0\n\
179 bf 1f\n\
180 mov.l %1,@%2\n\
181 1: mov r1,r15"\
182 : "=&r" (__result) : "r" (id | FUTEX_WAITERS), "r" (__futex) \
183 : "r0", "r1", "t", "memory"); \
184 if (__result) \
185 __result = __lll_robust_mutex_lock_wait (__result, __futex); \
186 __result; })
188 #define lll_mutex_timedlock(futex, timeout) \
189 ({ int __result, val, *__futex = &(futex); \
190 __asm __volatile ("\
191 .align 2\n\
192 mova 1f,r0\n\
193 nop\n\
194 mov r15,r1\n\
195 mov #-8,r15\n\
196 0: mov.l @%2,%0\n\
197 tst %0,%0\n\
198 bf 1f\n\
199 mov.l %1,@%2\n\
200 1: mov r1,r15"\
201 : "=&r" (__result) : "r" (1), "r" (__futex) \
202 : "r0", "r1", "t", "memory"); \
203 if (__result) \
204 __result = __lll_mutex_timedlock_wait (__result, __futex, timeout); \
205 __result; })
207 #define lll_robust_mutex_timedlock(futex, timeout, id) \
208 ({ int __result, val, *__futex = &(futex); \
209 __asm __volatile ("\
210 .align 2\n\
211 mova 1f,r0\n\
212 nop\n\
213 mov r15,r1\n\
214 mov #-8,r15\n\
215 0: mov.l @%2,%0\n\
216 tst %0,%0\n\
217 bf 1f\n\
218 mov.l %1,@%2\n\
219 1: mov r1,r15"\
220 : "=&r" (__result) : "r" (id), "r" (__futex) \
221 : "r0", "r1", "t", "memory"); \
222 if (__result) \
223 __result = __lll_robust_mutex_timedlock_wait (__result, __futex, \
224 timeout); \
225 __result; })
227 #define lll_mutex_unlock(futex) \
228 (void) ({ int __result, *__futex = &(futex); \
229 __asm __volatile ("\
230 .align 2\n\
231 mova 1f,r0\n\
232 mov r15,r1\n\
233 mov #-6,r15\n\
234 0: mov.l @%1,%0\n\
235 add #-1,%0\n\
236 mov.l %0,@%1\n\
237 1: mov r1,r15"\
238 : "=&r" (__result) : "r" (__futex) \
239 : "r0", "r1", "memory"); \
240 if (__result) \
241 __lll_mutex_unlock_wake (__futex); })
243 #define lll_robust_mutex_unlock(futex) \
244 (void) ({ int __result, *__futex = &(futex); \
245 __asm __volatile ("\
246 .align 2\n\
247 mova 1f,r0\n\
248 mov r15,r1\n\
249 mov #-6,r15\n\
250 0: mov.l @%1,%0\n\
251 and %2,%0\n\
252 mov.l %0,@%1\n\
253 1: mov r1,r15"\
254 : "=&r" (__result) : "r" (__futex), "r" (FUTEX_TID_MASK) \
255 : "r0", "r1", "memory"); \
256 if (__result) \
257 __lll_mutex_unlock_wake (__futex); })
259 #define lll_robust_mutex_dead(futex) \
260 (void) ({ int __ignore, *__futex = &(futex); \
261 __asm __volatile ("\
262 .align 2\n\
263 mova 1f,r0\n\
264 mov r15,r1\n\
265 mov #-6,r15\n\
266 0: mov.l @%1,%0\n\
267 or %2,%0\n\
268 mov.l %0,@%1\n\
269 1: mov r1,r15"\
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) \
275 (futex != 0)
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"
292 # else
293 # define SYSCALL_WITH_INST_PAD "\
294 trapa #0x14"
295 # endif
297 #define lll_futex_wait(futex, val) \
298 ({ \
299 int __status; \
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 \
306 : "=z" (__status) \
307 : "r" (__r3), "r" (__r4), "r" (__r5), \
308 "r" (__r6), "r" (__r7) \
309 : "memory", "t"); \
310 __status; \
314 #define lll_futex_timed_wait(futex, val, timeout) \
315 ({ \
316 int __status; \
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 \
323 : "=z" (__status) \
324 : "r" (__r3), "r" (__r4), "r" (__r5), \
325 "r" (__r6), "r" (__r7) \
326 : "memory", "t"); \
327 __status; \
331 #define lll_futex_wake(futex, nr) \
332 do { \
333 int __ignore; \
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 \
340 : "=z" (__ignore) \
341 : "r" (__r3), "r" (__r4), "r" (__r5), \
342 "r" (__r6), "r" (__r7) \
343 : "memory", "t"); \
344 } while (0)
347 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
350 /* The states of a lock are:
351 0 - untaken
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
366 afterwards. */
368 extern int __lll_wait_tid (int *tid) attribute_hidden;
369 #define lll_wait_tid(tid) \
370 do { \
371 __typeof (tid) *__tid = &(tid); \
372 while (*__tid != 0) \
373 lll_futex_wait (__tid, *__tid); \
374 } while (0)
376 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
377 attribute_hidden;
378 #define lll_timedwait_tid(tid, abstime) \
379 ({ \
380 int __result = 0; \
381 if (tid != 0) \
383 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
384 __result = EINVAL; \
385 else \
386 __result = __lll_timedwait_tid (&tid, abstime); \
388 __result; })
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)
396 attribute_hidden;
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 */