1 /* Copyright (C) 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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>
29 # define LOCK_INSTR /* nothing */
31 # define LOCK_INSTR "lock;"
40 /* Initializer for compatibility lock. */
41 #define LLL_MUTEX_LOCK_INITIALIZER (0)
42 #define LLL_MUTEX_LOCK_INITIALIZER_LOCKED (1)
43 #define LLL_MUTEX_LOCK_INITIALIZER_WAITERS (2)
47 # define LLL_EBX_LOAD "xchgl %2, %%ebx\n"
48 # define LLL_EBX_REG "D"
51 # define LLL_EBX_REG "b"
54 #ifdef I386_USE_SYSENTER
56 # define LLL_ENTER_KERNEL "call *%%gs:%P6\n\t"
58 # define LLL_ENTER_KERNEL "call *_dl_sysinfo\n\t"
61 # define LLL_ENTER_KERNEL "int $0x80\n\t"
64 /* Delay in spinlock loop. */
65 #define BUSY_WAIT_NOP asm ("rep; nop")
68 #ifdef USE_CFA_VAL_EXPRESSION
69 # define LLL_STUB_UNWIND_INFO_START \
70 ".section .eh_frame,\"a\",@progbits\n" \
71 "5:\t" ".long 7f-6f # Length of Common Information Entry\n" \
72 "6:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
73 ".byte 0x1 # CIE Version\n\t" \
74 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
75 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
76 ".sleb128 -4 # CIE Data Alignment Factor\n\t" \
77 ".byte 0x8 # CIE RA Column\n\t" \
78 ".uleb128 0x1 # Augmentation size\n\t" \
79 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
80 ".byte 0xc # DW_CFA_def_cfa\n\t" \
84 "7:\t" ".long 17f-8f # FDE Length\n" \
85 "8:\t" ".long 8b-5b # FDE CIE offset\n\t" \
86 ".long 1b-. # FDE initial location\n\t" \
87 ".long 4b-1b # FDE address range\n\t" \
88 ".uleb128 0x0 # Augmentation size\n\t" \
89 ".byte 0x16 # DW_CFA_val_expression\n\t" \
92 "9:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
94 # define LLL_STUB_UNWIND_INFO_END \
95 ".byte 0x16 # DW_CFA_val_expression\n\t" \
97 ".uleb128 12f-11f\n" \
98 "11:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
100 "12:\t" ".byte 0x40 + (3b-2b-1) # DW_CFA_advance_loc\n\t" \
101 ".byte 0x16 # DW_CFA_val_expression\n\t" \
103 ".uleb128 16f-13f\n" \
104 "13:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
105 ".sleb128 15f-14f\n\t" \
106 ".byte 0x0d # DW_OP_const4s\n" \
107 "14:\t" ".4byte 3b-.\n\t" \
108 ".byte 0x1c # DW_OP_minus\n\t" \
109 ".byte 0x0d # DW_OP_const4s\n" \
110 "15:\t" ".4byte 18f-.\n\t" \
111 ".byte 0x22 # DW_OP_plus\n" \
112 "16:\t" ".align 4\n" \
113 "17:\t" ".previous\n"
121 # define LLL_STUB_UNWIND_INFO_3 \
122 LLL_STUB_UNWIND_INFO_START \
123 "10:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
124 LLL_STUB_UNWIND_INFO_END
133 # define LLL_STUB_UNWIND_INFO_4 \
134 LLL_STUB_UNWIND_INFO_START \
135 "10:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
136 ".byte 0x16 # DW_CFA_val_expression\n\t" \
138 ".uleb128 20f-19f\n" \
139 "19:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
141 "20:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
142 LLL_STUB_UNWIND_INFO_END
145 # define LLL_STUB_UNWIND_INFO_3
146 # define LLL_STUB_UNWIND_INFO_4
150 #define lll_futex_wait(futex, val) \
153 register __typeof (val) _val asm ("edx") = (val); \
154 __asm __volatile (LLL_EBX_LOAD \
158 : "0" (SYS_futex), LLL_EBX_REG (futex), "S" (0), \
159 "c" (FUTEX_WAIT), "d" (_val), \
160 "i" (offsetof (tcbhead_t, sysinfo)) \
166 #define lll_futex_timed_wait(futex, val, timeout) \
169 register __typeof (val) _val asm ("edx") = (val); \
170 __asm __volatile (LLL_EBX_LOAD \
174 : "0" (SYS_futex), LLL_EBX_REG (futex), "S" (timeout), \
175 "c" (FUTEX_WAIT), "d" (_val), \
176 "i" (offsetof (tcbhead_t, sysinfo)) \
182 #define lll_futex_wake(futex, nr) \
185 register __typeof (nr) _nr asm ("edx") = (nr); \
186 __asm __volatile (LLL_EBX_LOAD \
190 : "0" (SYS_futex), LLL_EBX_REG (futex), \
191 "c" (FUTEX_WAKE), "d" (_nr), \
192 "i" (0) /* phony, to align next arg's number */, \
193 "i" (offsetof (tcbhead_t, sysinfo))); \
197 /* Does not preserve %eax and %ecx. */
198 extern int __lll_mutex_lock_wait (int val
, int *__futex
)
199 __attribute ((regparm (2))) attribute_hidden
;
200 /* Does not preserve %eax, %ecx, and %edx. */
201 extern int __lll_mutex_timedlock_wait (int val
, int *__futex
,
202 const struct timespec
*abstime
)
203 __attribute ((regparm (3))) attribute_hidden
;
204 /* Preserves all registers but %eax. */
205 extern int __lll_mutex_unlock_wake (int *__futex
)
206 __attribute ((regparm (1))) attribute_hidden
;
209 /* NB: in the lll_mutex_trylock macro we simply return the value in %eax
210 after the cmpxchg instruction. In case the operation succeded this
211 value is zero. In case the operation failed, the cmpxchg instruction
212 has loaded the current value of the memory work which is guaranteed
214 #define lll_mutex_trylock(futex) \
216 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
217 : "=a" (ret), "=m" (futex) \
218 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
219 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
224 #define lll_robust_mutex_trylock(futex, id) \
226 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
227 : "=a" (ret), "=m" (futex) \
228 : "r" (id), "m" (futex), \
229 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
234 #define lll_mutex_cond_trylock(futex) \
236 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
237 : "=a" (ret), "=m" (futex) \
238 : "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
239 "m" (futex), "0" (LLL_MUTEX_LOCK_INITIALIZER) \
244 #define lll_mutex_lock(futex) \
245 (void) ({ int ignore1, ignore2; \
246 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
247 "jnz _L_mutex_lock_%=\n\t" \
248 ".subsection 1\n\t" \
249 ".type _L_mutex_lock_%=,@function\n" \
250 "_L_mutex_lock_%=:\n" \
251 "1:\tleal %2, %%ecx\n" \
252 "2:\tcall __lll_mutex_lock_wait\n" \
254 "4:\t.size _L_mutex_lock_%=, 4b-1b\n\t" \
256 LLL_STUB_UNWIND_INFO_3 \
258 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
259 : "0" (0), "1" (1), "m" (futex) \
263 #define lll_robust_mutex_lock(futex, id) \
264 ({ int result, ignore; \
265 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
266 "jnz _L_robust_mutex_lock_%=\n\t" \
267 ".subsection 1\n\t" \
268 ".type _L_robust_mutex_lock_%=,@function\n" \
269 "_L_robust_mutex_lock_%=:\n" \
270 "1:\tleal %2, %%ecx\n" \
271 "2:\tcall __lll_robust_mutex_lock_wait\n" \
273 "4:\t.size _L_robust_mutex_lock_%=, 4b-1b\n\t" \
275 LLL_STUB_UNWIND_INFO_3 \
277 : "=a" (result), "=c" (ignore), "=m" (futex) \
278 : "0" (0), "1" (id), "m" (futex) \
283 /* Special version of lll_mutex_lock which causes the unlock function to
284 always wakeup waiters. */
285 #define lll_mutex_cond_lock(futex) \
286 (void) ({ int ignore1, ignore2; \
287 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
288 "jnz _L_mutex_cond_lock_%=\n\t" \
289 ".subsection 1\n\t" \
290 ".type _L_mutex_cond_lock_%=,@function\n" \
291 "_L_mutex_cond_lock_%=:\n" \
292 "1:\tleal %2, %%ecx\n" \
293 "2:\tcall __lll_mutex_lock_wait\n" \
295 "4:\t.size _L_mutex_cond_lock_%=, 4b-1b\n\t" \
297 LLL_STUB_UNWIND_INFO_3 \
299 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
300 : "0" (0), "1" (2), "m" (futex) \
304 #define lll_robust_mutex_cond_lock(futex, id) \
305 ({ int result, ignore; \
306 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
307 "jnz _L_robust_mutex_cond_lock_%=\n\t" \
308 ".subsection 1\n\t" \
309 ".type _L_robust_mutex_cond_lock_%=,@function\n" \
310 "_L_robust_mutex_cond_lock_%=:\n" \
311 "1:\tleal %2, %%ecx\n" \
312 "2:\tcall __lll_robust_mutex_lock_wait\n" \
314 "4:\t.size _L_robust_mutex_cond_lock_%=, 4b-1b\n\t" \
316 LLL_STUB_UNWIND_INFO_3 \
318 : "=a" (result), "=c" (ignore), "=m" (futex) \
319 : "0" (0), "1" (id | FUTEX_WAITERS), "m" (futex) \
324 #define lll_mutex_timedlock(futex, timeout) \
325 ({ int result, ignore1, ignore2; \
326 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
327 "jnz _L_mutex_timedlock_%=\n\t" \
328 ".subsection 1\n\t" \
329 ".type _L_mutex_timedlock_%=,@function\n" \
330 "_L_mutex_timedlock_%=:\n" \
331 "1:\tleal %3, %%ecx\n" \
332 "0:\tmovl %7, %%edx\n" \
333 "2:\tcall __lll_mutex_timedlock_wait\n" \
335 "4:\t.size _L_mutex_timedlock_%=, 4b-1b\n\t" \
337 LLL_STUB_UNWIND_INFO_4 \
339 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
341 : "0" (0), "1" (1), "m" (futex), "m" (timeout) \
346 #define lll_robust_mutex_timedlock(futex, timeout, id) \
347 ({ int result, ignore1, ignore2; \
348 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
349 "jnz _L_robust_mutex_timedlock_%=\n\t" \
350 ".subsection 1\n\t" \
351 ".type _L_robust_mutex_timedlock_%=,@function\n" \
352 "_L_robust_mutex_timedlock_%=:\n" \
353 "1:\tleal %3, %%ecx\n" \
354 "0:\tmovl %7, %%edx\n" \
355 "2:\tcall __lll_robust_mutex_timedlock_wait\n" \
357 "4:\t.size _L_robust_mutex_timedlock_%=, 4b-1b\n\t" \
359 LLL_STUB_UNWIND_INFO_4 \
361 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
363 : "0" (0), "1" (id), "m" (futex), "m" (timeout) \
368 #define lll_mutex_unlock(futex) \
369 (void) ({ int ignore; \
370 __asm __volatile (LOCK_INSTR "subl $1, %0\n\t" \
371 "jne _L_mutex_unlock_%=\n\t" \
372 ".subsection 1\n\t" \
373 ".type _L_mutex_unlock_%=,@function\n" \
374 "_L_mutex_unlock_%=:\n" \
375 "1:\tleal %0, %%eax\n" \
376 "2:\tcall __lll_mutex_unlock_wake\n" \
378 "4:\t.size _L_mutex_unlock_%=, 4b-1b\n\t" \
380 LLL_STUB_UNWIND_INFO_3 \
382 : "=m" (futex), "=&a" (ignore) \
387 #define lll_robust_mutex_unlock(futex) \
388 (void) ({ int ignore; \
389 __asm __volatile (LOCK_INSTR "andl %2, %0\n\t" \
390 "jne _L_robust_mutex_unlock_%=\n\t" \
391 ".subsection 1\n\t" \
392 ".type _L_robust_mutex_unlock_%=,@function\n" \
393 "_L_robust_mutex_unlock_%=:\n\t" \
394 "1:\tleal %0, %%eax\n" \
395 "2:\tcall __lll_mutex_unlock_wake\n" \
397 "4:\t.size _L_robust_mutex_unlock_%=, 4b-1b\n\t"\
399 LLL_STUB_UNWIND_INFO_3 \
401 : "=m" (futex), "=&a" (ignore) \
402 : "i" (FUTEX_WAITERS), "m" (futex) \
406 #define lll_robust_mutex_dead(futex) \
407 (void) ({ int __ignore; \
408 register int _nr asm ("edx") = 1; \
409 __asm __volatile (LOCK_INSTR "orl %5, (%2)\n\t" \
414 : "0" (SYS_futex), LLL_EBX_REG (&(futex)), \
415 "c" (FUTEX_WAKE), "d" (_nr), \
416 "i" (FUTEX_OWNER_DIED), \
417 "i" (offsetof (tcbhead_t, sysinfo))); })
420 #define lll_futex_wake(futex, nr) \
423 register __typeof (nr) _nr asm ("edx") = (nr); \
424 __asm __volatile (LLL_EBX_LOAD \
428 : "0" (SYS_futex), LLL_EBX_REG (futex), \
429 "c" (FUTEX_WAKE), "d" (_nr), \
430 "i" (0) /* phony, to align next arg's number */, \
431 "i" (offsetof (tcbhead_t, sysinfo))); \
435 #define lll_mutex_islocked(futex) \
439 /* We have a separate internal lock implementation which is not tied
440 to binary compatibility. */
442 /* Type for lock object. */
443 typedef int lll_lock_t
;
445 /* Initializers for lock. */
446 #define LLL_LOCK_INITIALIZER (0)
447 #define LLL_LOCK_INITIALIZER_LOCKED (1)
450 extern int __lll_lock_wait (int val
, int *__futex
)
451 __attribute ((regparm (2))) attribute_hidden
;
452 extern int __lll_unlock_wake (int *__futex
)
453 __attribute ((regparm (1))) attribute_hidden
;
454 extern int lll_unlock_wake_cb (int *__futex
) attribute_hidden
;
457 /* The states of a lock are:
459 1 - taken by one user
460 2 - taken by more users */
463 #if defined NOT_IN_libc || defined UP
464 # define lll_trylock(futex) lll_mutex_trylock (futex)
465 # define lll_lock(futex) lll_mutex_lock (futex)
466 # define lll_unlock(futex) lll_mutex_unlock (futex)
468 /* Special versions of the macros for use in libc itself. They avoid
469 the lock prefix when the thread library is not used.
471 XXX In future we might even want to avoid it on UP machines. */
474 # define lll_trylock(futex) \
475 ({ unsigned char ret; \
476 __asm __volatile ("cmpl $0, %%gs:%P5\n\t" \
479 "0:\tcmpxchgl %2, %1; setne %0" \
480 : "=a" (ret), "=m" (futex) \
481 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
482 "0" (LLL_MUTEX_LOCK_INITIALIZER), \
483 "i" (offsetof (tcbhead_t, multiple_threads)) \
488 # define lll_lock(futex) \
489 (void) ({ int ignore1, ignore2; \
490 __asm __volatile ("cmpl $0, %%gs:%P6\n\t" \
493 "0:\tcmpxchgl %1, %2\n\t" \
494 "jnz _L_lock_%=\n\t" \
495 ".subsection 1\n\t" \
496 ".type _L_lock_%=,@function\n" \
498 "1:\tleal %2, %%ecx\n" \
499 "2:\tcall __lll_mutex_lock_wait\n" \
501 "4:\t.size _L_lock_%=, 4b-1b\n\t" \
503 LLL_STUB_UNWIND_INFO_3 \
505 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
506 : "0" (0), "1" (1), "m" (futex), \
507 "i" (offsetof (tcbhead_t, multiple_threads)) \
511 # define lll_unlock(futex) \
512 (void) ({ int ignore; \
513 __asm __volatile ("cmpl $0, %%gs:%P3\n\t" \
516 "0:\tsubl $1,%0\n\t" \
517 "jne _L_unlock_%=\n\t" \
518 ".subsection 1\n\t" \
519 ".type _L_unlock_%=,@function\n" \
521 "1:\tleal %0, %%eax\n" \
522 "2:\tcall __lll_mutex_unlock_wake\n" \
524 "4:\t.size _L_unlock_%=, 4b-1b\n\t" \
526 LLL_STUB_UNWIND_INFO_3 \
528 : "=m" (futex), "=&a" (ignore) \
530 "i" (offsetof (tcbhead_t, multiple_threads)) \
535 #define lll_islocked(futex) \
536 (futex != LLL_LOCK_INITIALIZER)
539 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
540 wakeup when the clone terminates. The memory location contains the
541 thread ID while the clone is running and is reset to zero
544 The macro parameter must not have any side effect. */
545 #define lll_wait_tid(tid) \
548 register __typeof (tid) _tid asm ("edx") = (tid); \
550 __asm __volatile (LLL_EBX_LOAD \
551 "1:\tmovl %1, %%eax\n\t" \
553 "cmpl $0, (%%ebx)\n\t" \
557 : "i" (SYS_futex), LLL_EBX_REG (&tid), "S" (0), \
558 "c" (FUTEX_WAIT), "d" (_tid), \
559 "i" (offsetof (tcbhead_t, sysinfo)) \
563 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
564 __attribute__ ((regparm (2))) attribute_hidden
;
565 #define lll_timedwait_tid(tid, abstime) \
570 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
573 __result = __lll_timedwait_tid (&tid, abstime); \
578 /* Conditional variable handling. */
580 extern void __lll_cond_wait (pthread_cond_t
*cond
)
581 __attribute ((regparm (1))) attribute_hidden
;
582 extern int __lll_cond_timedwait (pthread_cond_t
*cond
,
583 const struct timespec
*abstime
)
584 __attribute ((regparm (2))) attribute_hidden
;
585 extern void __lll_cond_wake (pthread_cond_t
*cond
)
586 __attribute ((regparm (1))) attribute_hidden
;
587 extern void __lll_cond_broadcast (pthread_cond_t
*cond
)
588 __attribute ((regparm (1))) attribute_hidden
;
591 #define lll_cond_wait(cond) \
592 __lll_cond_wait (cond)
593 #define lll_cond_timedwait(cond, abstime) \
594 __lll_cond_timedwait (cond, abstime)
595 #define lll_cond_wake(cond) \
596 __lll_cond_wake (cond)
597 #define lll_cond_broadcast(cond) \
598 __lll_cond_broadcast (cond)
601 #endif /* lowlevellock.h */