* sysdeps/unix/sysv/linux/sh/lowlevellock.h: Add lll_robust_mutex_*
[glibc.git] / nptl / sysdeps / unix / sysv / linux / sh / lowlevellock.h
blob92f93cd5f5cbf10a0ac832acd0cacd0f84112e52
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
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)
39 attribute_hidden;
40 extern int __lll_mutex_unlock_wake (int *__futex) attribute_hidden;
43 #define lll_mutex_trylock(futex) \
44 ({ unsigned char __result; \
45 __asm __volatile ("\
46 .align 2\n\
47 mova 1f,r0\n\
48 nop\n\
49 mov r15,r1\n\
50 mov #-8,r15\n\
51 0: mov.l @%1,r2\n\
52 cmp/eq r2,%3\n\
53 bf 1f\n\
54 mov.l %2,@%1\n\
55 1: mov r1,r15\n\
56 mov #-1,%0\n\
57 negc %0,%0"\
58 : "=r" (__result) \
59 : "r" (&(futex)), \
60 "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), \
61 "r" (LLL_MUTEX_LOCK_INITIALIZER) \
62 : "r0", "r1", "r2", "t", "memory"); \
63 __result; })
65 #define lll_robust_mutex_trylock(futex, id) \
66 ({ unsigned char __result; \
67 __asm __volatile ("\
68 .align 2\n\
69 mova 1f,r0\n\
70 nop\n\
71 mov r15,r1\n\
72 mov #-8,r15\n\
73 0: mov.l @%1,r2\n\
74 cmp/eq r2,%3\n\
75 bf 1f\n\
76 mov.l %2,@%1\n\
77 1: mov r1,r15\n\
78 mov #-1,%0\n\
79 negc %0,%0"\
80 : "=r" (__result) \
81 : "r" (&(futex)), \
82 "r" (id), \
83 "r" (LLL_MUTEX_LOCK_INITIALIZER) \
84 : "r0", "r1", "r2", "t", "memory"); \
85 __result; })
87 #define lll_mutex_cond_trylock(futex) \
88 ({ unsigned char __result; \
89 __asm __volatile ("\
90 .align 2\n\
91 mova 1f,r0\n\
92 nop\n\
93 mov r15,r1\n\
94 mov #-8,r15\n\
95 0: mov.l @%1,r2\n\
96 cmp/eq r2,%3\n\
97 bf 1f\n\
98 mov.l %2,@%1\n\
99 1: mov r1,r15\n\
100 mov #-1,%0\n\
101 negc %0,%0"\
102 : "=r" (__result) \
103 : "r" (&(futex)), \
104 "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
105 "r" (LLL_MUTEX_LOCK_INITIALIZER) \
106 : "r0", "r1", "r2", "t", "memory"); \
107 __result; })
109 #define lll_mutex_lock(futex) \
110 (void) ({ int __result, val, *__futex = &(futex); \
111 __asm __volatile ("\
112 .align 2\n\
113 mova 1f,r0\n\
114 nop\n\
115 mov r15,r1\n\
116 mov #-8,r15\n\
117 0: mov.l @%2,%0\n\
118 tst %0,%0\n\
119 bf 1f\n\
120 mov.l %1,@%2\n\
121 1: mov r1,r15"\
122 : "=&r" (__result) : "r" (1), "r" (__futex) \
123 : "r0", "r1", "t", "memory"); \
124 if (__result) \
125 __lll_mutex_lock_wait (__result, __futex); })
127 #define lll_robust_mutex_lock(futex, id) \
128 ({ int __result, val, *__futex = &(futex); \
129 __asm __volatile ("\
130 .align 2\n\
131 mova 1f,r0\n\
132 nop\n\
133 mov r15,r1\n\
134 mov #-8,r15\n\
135 0: mov.l @%2,%0\n\
136 tst %0,%0\n\
137 bf 1f\n\
138 mov.l %1,@%2\n\
139 1: mov r1,r15"\
140 : "=&r" (__result) : "r" (id), "r" (__futex) \
141 : "r0", "r1", "t", "memory"); \
142 if (__result) \
143 __result = __lll_robust_mutex_lock_wait (__result, __futex); \
144 __result; })
146 /* Special version of lll_mutex_lock which causes the unlock function to
147 always wakeup waiters. */
148 #define lll_mutex_cond_lock(futex) \
149 (void) ({ int __result, val, *__futex = &(futex); \
150 __asm __volatile ("\
151 .align 2\n\
152 mova 1f,r0\n\
153 nop\n\
154 mov r15,r1\n\
155 mov #-8,r15\n\
156 0: mov.l @%2,%0\n\
157 tst %0,%0\n\
158 bf 1f\n\
159 mov.l %1,@%2\n\
160 1: mov r1,r15"\
161 : "=&r" (__result) : "r" (2), "r" (__futex) \
162 : "r0", "r1", "t", "memory"); \
163 if (__result) \
164 __lll_mutex_lock_wait (__result, __futex); })
166 #define lll_robust_mutex_cond_lock(futex, id) \
167 ({ int __result, val, *__futex = &(futex); \
168 __asm __volatile ("\
169 .align 2\n\
170 mova 1f,r0\n\
171 nop\n\
172 mov r15,r1\n\
173 mov #-8,r15\n\
174 0: mov.l @%2,%0\n\
175 tst %0,%0\n\
176 bf 1f\n\
177 mov.l %1,@%2\n\
178 1: mov r1,r15"\
179 : "=&r" (__result) : "r" (id | FUTEX_WAITERS), "r" (__futex) \
180 : "r0", "r1", "t", "memory"); \
181 if (__result) \
182 __result = __lll_robust_mutex_lock_wait (__result, __futex); \
183 __result; })
185 #define lll_mutex_timedlock(futex, timeout) \
186 ({ int __result, val, *__futex = &(futex); \
187 __asm __volatile ("\
188 .align 2\n\
189 mova 1f,r0\n\
190 nop\n\
191 mov r15,r1\n\
192 mov #-8,r15\n\
193 0: mov.l @%2,%0\n\
194 tst %0,%0\n\
195 bf 1f\n\
196 mov.l %1,@%2\n\
197 1: mov r1,r15"\
198 : "=&r" (__result) : "r" (1), "r" (__futex) \
199 : "r0", "r1", "t", "memory"); \
200 if (__result) \
201 __result = __lll_mutex_timedlock_wait (__result, __futex, timeout); \
202 __result; })
204 #define lll_robust_mutex_timedlock(futex, timeout, id) \
205 ({ int __result, val, *__futex = &(futex); \
206 __asm __volatile ("\
207 .align 2\n\
208 mova 1f,r0\n\
209 nop\n\
210 mov r15,r1\n\
211 mov #-8,r15\n\
212 0: mov.l @%2,%0\n\
213 tst %0,%0\n\
214 bf 1f\n\
215 mov.l %1,@%2\n\
216 1: mov r1,r15"\
217 : "=&r" (__result) : "r" (id), "r" (__futex) \
218 : "r0", "r1", "t", "memory"); \
219 if (__result) \
220 __result = __lll_robust_mutex_timedlock_wait (__result, __futex, \
221 timeout); \
222 __result; })
224 #define lll_mutex_unlock(futex) \
225 (void) ({ int __result, *__futex = &(futex); \
226 __asm __volatile ("\
227 .align 2\n\
228 mova 1f,r0\n\
229 mov r15,r1\n\
230 mov #-6,r15\n\
231 0: mov.l @%1,%0\n\
232 add #-1,%0\n\
233 mov.l %0,@%1\n\
234 1: mov r1,r15"\
235 : "=&r" (__result) : "r" (__futex) \
236 : "r0", "r1", "memory"); \
237 if (__result) \
238 __lll_mutex_unlock_wake (__futex); })
240 #define lll_robust_mutex_unlock(futex) \
241 (void) ({ int __result, *__futex = &(futex); \
242 __asm __volatile ("\
243 .align 2\n\
244 mova 1f,r0\n\
245 mov r15,r1\n\
246 mov #-6,r15\n\
247 0: mov.l @%1,%0\n\
248 and %2,%0\n\
249 mov.l %0,@%1\n\
250 1: mov r1,r15"\
251 : "=&r" (__result) : "r" (__futex), "r" (FUTEX_TID_MASK) \
252 : "r0", "r1", "memory"); \
253 if (__result) \
254 __lll_mutex_unlock_wake (__futex); })
256 #define lll_robust_mutex_dead(futex) \
257 (void) ({ int __ignore, *__futex = &(futex); \
258 __asm __volatile ("\
259 .align 2\n\
260 mova 1f,r0\n\
261 mov r15,r1\n\
262 mov #-6,r15\n\
263 0: mov.l @%1,%0\n\
264 or %2,%0\n\
265 mov.l %0,@%1\n\
266 1: mov r1,r15"\
267 : "=&r" (__ignore) : "r" (__futex), "r" (FUTEX_OWNER_DIED) \
268 : "r0", "r1", "memory"); \
269 lll_futex_wake (__futex, 1); })
271 #define lll_mutex_islocked(futex) \
272 (futex != 0)
275 /* We have a separate internal lock implementation which is not tied
276 to binary compatibility. */
278 /* Type for lock object. */
279 typedef int lll_lock_t;
281 /* Initializers for lock. */
282 #define LLL_LOCK_INITIALIZER (0)
283 #define LLL_LOCK_INITIALIZER_LOCKED (1)
286 # ifdef NEED_SYSCALL_INST_PAD
287 # define SYSCALL_WITH_INST_PAD "\
288 trapa #0x14; or r0,r0; or r0,r0; or r0,r0; or r0,r0; or r0,r0"
289 # else
290 # define SYSCALL_WITH_INST_PAD "\
291 trapa #0x14"
292 # endif
294 #define lll_futex_wait(futex, val) \
295 ({ \
296 int __status; \
297 register unsigned long __r3 asm ("r3") = SYS_futex; \
298 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
299 register unsigned long __r5 asm ("r5") = FUTEX_WAIT; \
300 register unsigned long __r6 asm ("r6") = (unsigned long) (val); \
301 register unsigned long __r7 asm ("r7") = 0; \
302 __asm __volatile (SYSCALL_WITH_INST_PAD \
303 : "=z" (__status) \
304 : "r" (__r3), "r" (__r4), "r" (__r5), \
305 "r" (__r6), "r" (__r7) \
306 : "memory", "t"); \
307 __status; \
311 #define lll_futex_timed_wait(futex, val, timeout) \
312 ({ \
313 int __status; \
314 register unsigned long __r3 asm ("r3") = SYS_futex; \
315 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
316 register unsigned long __r5 asm ("r5") = FUTEX_WAIT; \
317 register unsigned long __r6 asm ("r6") = (unsigned long) (val); \
318 register unsigned long __r7 asm ("r7") = (timeout); \
319 __asm __volatile (SYSCALL_WITH_INST_PAD \
320 : "=z" (__status) \
321 : "r" (__r3), "r" (__r4), "r" (__r5), \
322 "r" (__r6), "r" (__r7) \
323 : "memory", "t"); \
324 __status; \
328 #define lll_futex_wake(futex, nr) \
329 do { \
330 int __ignore; \
331 register unsigned long __r3 asm ("r3") = SYS_futex; \
332 register unsigned long __r4 asm ("r4") = (unsigned long) (futex); \
333 register unsigned long __r5 asm ("r5") = FUTEX_WAKE; \
334 register unsigned long __r6 asm ("r6") = (unsigned long) (nr); \
335 register unsigned long __r7 asm ("r7") = 0; \
336 __asm __volatile (SYSCALL_WITH_INST_PAD \
337 : "=z" (__ignore) \
338 : "r" (__r3), "r" (__r4), "r" (__r5), \
339 "r" (__r6), "r" (__r7) \
340 : "memory", "t"); \
341 } while (0)
344 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
347 /* The states of a lock are:
348 0 - untaken
349 1 - taken by one user
350 2 - taken by more users */
352 #define lll_trylock(futex) lll_mutex_trylock (futex)
353 #define lll_lock(futex) lll_mutex_lock (futex)
354 #define lll_unlock(futex) lll_mutex_unlock (futex)
356 #define lll_islocked(futex) \
357 (futex != LLL_LOCK_INITIALIZER)
360 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
361 wakeup when the clone terminates. The memory location contains the
362 thread ID while the clone is running and is reset to zero
363 afterwards. */
365 extern int __lll_wait_tid (int *tid) attribute_hidden;
366 #define lll_wait_tid(tid) \
367 do { \
368 __typeof (tid) *__tid = &(tid); \
369 while (*__tid != 0) \
370 lll_futex_wait (__tid, *__tid); \
371 } while (0)
373 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
374 attribute_hidden;
375 #define lll_timedwait_tid(tid, abstime) \
376 ({ \
377 int __result = 0; \
378 if (tid != 0) \
380 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
381 __result = EINVAL; \
382 else \
383 __result = __lll_timedwait_tid (&tid, abstime); \
385 __result; })
388 /* Conditional variable handling. */
390 extern void __lll_cond_wait (pthread_cond_t *cond) attribute_hidden;
391 extern int __lll_cond_timedwait (pthread_cond_t *cond,
392 const struct timespec *abstime)
393 attribute_hidden;
394 extern void __lll_cond_wake (pthread_cond_t *cond) attribute_hidden;
395 extern void __lll_cond_broadcast (pthread_cond_t *cond) attribute_hidden;
398 #define lll_cond_wait(cond) \
399 __lll_cond_wait (cond)
400 #define lll_cond_timedwait(cond, abstime) \
401 __lll_cond_timedwait (cond, abstime)
402 #define lll_cond_wake(cond) \
403 __lll_cond_wake (cond)
404 #define lll_cond_broadcast(cond) \
405 __lll_cond_broadcast (cond)
407 #endif /* lowlevellock.h */