1 /* Copyright (C) 2002-2004, 2006-2008, 2009 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, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
24 # include <sys/param.h>
25 # include <bits/pthreadtypes.h>
26 # include <kernel-features.h>
27 # include <tcb-offsets.h>
31 # define LOCK_INSTR /* nothing */
33 # define LOCK_INSTR "lock;"
49 #define FUTEX_CMP_REQUEUE 4
50 #define FUTEX_WAKE_OP 5
51 #define FUTEX_LOCK_PI 6
52 #define FUTEX_UNLOCK_PI 7
53 #define FUTEX_TRYLOCK_PI 8
54 #define FUTEX_WAIT_BITSET 9
55 #define FUTEX_WAKE_BITSET 10
56 #define FUTEX_WAIT_REQUEUE_PI 11
57 #define FUTEX_CMP_REQUEUE_PI 12
58 #define FUTEX_PRIVATE_FLAG 128
59 #define FUTEX_CLOCK_REALTIME 256
61 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
63 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
65 /* Values for 'private' parameter of locking macros. Yes, the
66 definition seems to be backwards. But it is not. The bit will be
67 reversed before passing to the system call. */
69 #define LLL_SHARED FUTEX_PRIVATE_FLAG
72 #if !defined NOT_IN_libc || defined IS_IN_rtld
73 /* In libc.so or ld.so all futexes are private. */
74 # ifdef __ASSUME_PRIVATE_FUTEX
75 # define __lll_private_flag(fl, private) \
76 ((fl) | FUTEX_PRIVATE_FLAG)
78 # define __lll_private_flag(fl, private) \
79 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
82 # ifdef __ASSUME_PRIVATE_FUTEX
83 # define __lll_private_flag(fl, private) \
84 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
86 # define __lll_private_flag(fl, private) \
87 (__builtin_constant_p (private) \
89 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
91 : ({ unsigned int __fl = ((private) ^ FUTEX_PRIVATE_FLAG); \
92 asm ("andl %%gs:%P1, %0" : "+r" (__fl) \
93 : "i" (offsetof (struct pthread, header.private_futex))); \
100 /* Initializer for compatibility lock. */
101 #define LLL_LOCK_INITIALIZER (0)
102 #define LLL_LOCK_INITIALIZER_LOCKED (1)
103 #define LLL_LOCK_INITIALIZER_WAITERS (2)
107 # define LLL_EBX_LOAD "xchgl %2, %%ebx\n"
108 # define LLL_EBX_REG "D"
110 # define LLL_EBX_LOAD
111 # define LLL_EBX_REG "b"
114 #ifdef I386_USE_SYSENTER
116 # define LLL_ENTER_KERNEL "call *%%gs:%P6\n\t"
118 # define LLL_ENTER_KERNEL "call *_dl_sysinfo\n\t"
121 # define LLL_ENTER_KERNEL "int $0x80\n\t"
124 /* Delay in spinlock loop. */
125 #define BUSY_WAIT_NOP asm ("rep; nop")
128 #define LLL_STUB_UNWIND_INFO_START \
129 ".section .eh_frame,\"a\",@progbits\n" \
130 "5:\t" ".long 7f-6f # Length of Common Information Entry\n" \
131 "6:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
132 ".byte 0x1 # CIE Version\n\t" \
133 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
134 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
135 ".sleb128 -4 # CIE Data Alignment Factor\n\t" \
136 ".byte 0x8 # CIE RA Column\n\t" \
137 ".uleb128 0x1 # Augmentation size\n\t" \
138 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
139 ".byte 0xc # DW_CFA_def_cfa\n\t" \
143 "7:\t" ".long 17f-8f # FDE Length\n" \
144 "8:\t" ".long 8b-5b # FDE CIE offset\n\t" \
145 ".long 1b-. # FDE initial location\n\t" \
146 ".long 4b-1b # FDE address range\n\t" \
147 ".uleb128 0x0 # Augmentation size\n\t" \
148 ".byte 0x16 # DW_CFA_val_expression\n\t" \
150 ".uleb128 10f-9f\n" \
151 "9:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
153 #define LLL_STUB_UNWIND_INFO_END \
154 ".byte 0x16 # DW_CFA_val_expression\n\t" \
156 ".uleb128 12f-11f\n" \
157 "11:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
159 "12:\t" ".byte 0x40 + (3b-2b-1) # DW_CFA_advance_loc\n\t" \
160 ".byte 0x16 # DW_CFA_val_expression\n\t" \
162 ".uleb128 16f-13f\n" \
163 "13:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
164 ".sleb128 15f-14f\n\t" \
165 ".byte 0x0d # DW_OP_const4s\n" \
166 "14:\t" ".4byte 3b-.\n\t" \
167 ".byte 0x1c # DW_OP_minus\n\t" \
168 ".byte 0x0d # DW_OP_const4s\n" \
169 "15:\t" ".4byte 18f-.\n\t" \
170 ".byte 0x22 # DW_OP_plus\n" \
171 "16:\t" ".align 4\n" \
172 "17:\t" ".previous\n"
180 #define LLL_STUB_UNWIND_INFO_3 \
181 LLL_STUB_UNWIND_INFO_START \
182 "10:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
183 LLL_STUB_UNWIND_INFO_END
192 #define LLL_STUB_UNWIND_INFO_4 \
193 LLL_STUB_UNWIND_INFO_START \
194 "10:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
195 ".byte 0x16 # DW_CFA_val_expression\n\t" \
197 ".uleb128 20f-19f\n" \
198 "19:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
200 "20:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
201 LLL_STUB_UNWIND_INFO_END
204 #define lll_futex_wait(futex, val, private) \
205 lll_futex_timed_wait (futex, val, NULL, private)
208 #define lll_futex_timed_wait(futex, val, timeout, private) \
211 register __typeof (val) _val asm ("edx") = (val); \
212 __asm __volatile (LLL_EBX_LOAD \
216 : "0" (SYS_futex), LLL_EBX_REG (futex), "S" (timeout), \
217 "c" (__lll_private_flag (FUTEX_WAIT, private)), \
218 "d" (_val), "i" (offsetof (tcbhead_t, sysinfo)) \
224 #define lll_futex_wake(futex, nr, private) \
227 register __typeof (nr) _nr asm ("edx") = (nr); \
228 __asm __volatile (LLL_EBX_LOAD \
232 : "0" (SYS_futex), LLL_EBX_REG (futex), \
233 "c" (__lll_private_flag (FUTEX_WAKE, private)), \
235 "i" (0) /* phony, to align next arg's number */, \
236 "i" (offsetof (tcbhead_t, sysinfo))); \
240 /* NB: in the lll_trylock macro we simply return the value in %eax
241 after the cmpxchg instruction. In case the operation succeded this
242 value is zero. In case the operation failed, the cmpxchg instruction
243 has loaded the current value of the memory work which is guaranteed
245 #if defined NOT_IN_libc || defined UP
246 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
248 # define __lll_trylock_asm "cmpl $0, %%gs:%P5\n\t" \
251 "0:\tcmpxchgl %2, %1"
254 #define lll_trylock(futex) \
256 __asm __volatile (__lll_trylock_asm \
257 : "=a" (ret), "=m" (futex) \
258 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
259 "0" (LLL_LOCK_INITIALIZER), \
260 "i" (MULTIPLE_THREADS_OFFSET) \
264 #define lll_robust_trylock(futex, id) \
266 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
267 : "=a" (ret), "=m" (futex) \
268 : "r" (id), "m" (futex), \
269 "0" (LLL_LOCK_INITIALIZER) \
274 #define lll_cond_trylock(futex) \
276 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
277 : "=a" (ret), "=m" (futex) \
278 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
279 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
283 #if defined NOT_IN_libc || defined UP
284 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %1, %2\n\t"
286 # define __lll_lock_asm_start "cmpl $0, %%gs:%P6\n\t" \
289 "0:\tcmpxchgl %1, %2\n\t"
292 #define lll_lock(futex, private) \
294 ({ int ignore1, ignore2; \
295 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
296 __asm __volatile (__lll_lock_asm_start \
297 "jnz _L_lock_%=\n\t" \
298 ".subsection 1\n\t" \
299 ".type _L_lock_%=,@function\n" \
301 "1:\tleal %2, %%ecx\n" \
302 "2:\tcall __lll_lock_wait_private\n" \
304 "4:\t.size _L_lock_%=, 4b-1b\n\t" \
306 LLL_STUB_UNWIND_INFO_3 \
308 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
309 : "0" (0), "1" (1), "m" (futex), \
310 "i" (MULTIPLE_THREADS_OFFSET) \
315 __asm __volatile (__lll_lock_asm_start \
316 "jnz _L_lock_%=\n\t" \
317 ".subsection 1\n\t" \
318 ".type _L_lock_%=,@function\n" \
320 "1:\tleal %2, %%edx\n" \
321 "0:\tmovl %8, %%ecx\n" \
322 "2:\tcall __lll_lock_wait\n" \
324 "4:\t.size _L_lock_%=, 4b-1b\n\t" \
326 LLL_STUB_UNWIND_INFO_4 \
328 : "=a" (ignore1), "=c" (ignore2), \
329 "=m" (futex), "=&d" (ignore3) \
330 : "1" (1), "m" (futex), \
331 "i" (MULTIPLE_THREADS_OFFSET), "0" (0), \
332 "g" ((int) (private)) \
337 #define lll_robust_lock(futex, id, private) \
338 ({ int result, ignore1, ignore2; \
339 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
340 "jnz _L_robust_lock_%=\n\t" \
341 ".subsection 1\n\t" \
342 ".type _L_robust_lock_%=,@function\n" \
343 "_L_robust_lock_%=:\n" \
344 "1:\tleal %2, %%edx\n" \
345 "0:\tmovl %7, %%ecx\n" \
346 "2:\tcall __lll_robust_lock_wait\n" \
348 "4:\t.size _L_robust_lock_%=, 4b-1b\n\t" \
350 LLL_STUB_UNWIND_INFO_4 \
352 : "=a" (result), "=c" (ignore1), "=m" (futex), \
354 : "0" (0), "1" (id), "m" (futex), "g" ((int) (private))\
359 /* Special version of lll_lock which causes the unlock function to
360 always wakeup waiters. */
361 #define lll_cond_lock(futex, private) \
363 ({ int ignore1, ignore2, ignore3; \
364 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
365 "jnz _L_cond_lock_%=\n\t" \
366 ".subsection 1\n\t" \
367 ".type _L_cond_lock_%=,@function\n" \
368 "_L_cond_lock_%=:\n" \
369 "1:\tleal %2, %%edx\n" \
370 "0:\tmovl %7, %%ecx\n" \
371 "2:\tcall __lll_lock_wait\n" \
373 "4:\t.size _L_cond_lock_%=, 4b-1b\n\t" \
375 LLL_STUB_UNWIND_INFO_4 \
377 : "=a" (ignore1), "=c" (ignore2), "=m" (futex), \
379 : "0" (0), "1" (2), "m" (futex), "g" ((int) (private))\
384 #define lll_robust_cond_lock(futex, id, private) \
385 ({ int result, ignore1, ignore2; \
386 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
387 "jnz _L_robust_cond_lock_%=\n\t" \
388 ".subsection 1\n\t" \
389 ".type _L_robust_cond_lock_%=,@function\n" \
390 "_L_robust_cond_lock_%=:\n" \
391 "1:\tleal %2, %%edx\n" \
392 "0:\tmovl %7, %%ecx\n" \
393 "2:\tcall __lll_robust_lock_wait\n" \
395 "4:\t.size _L_robust_cond_lock_%=, 4b-1b\n\t" \
397 LLL_STUB_UNWIND_INFO_4 \
399 : "=a" (result), "=c" (ignore1), "=m" (futex), \
401 : "0" (0), "1" (id | FUTEX_WAITERS), "m" (futex), \
402 "g" ((int) (private)) \
407 #define lll_timedlock(futex, timeout, private) \
408 ({ int result, ignore1, ignore2, ignore3; \
409 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
410 "jnz _L_timedlock_%=\n\t" \
411 ".subsection 1\n\t" \
412 ".type _L_timedlock_%=,@function\n" \
413 "_L_timedlock_%=:\n" \
414 "1:\tleal %3, %%ecx\n" \
415 "0:\tmovl %8, %%edx\n" \
416 "2:\tcall __lll_timedlock_wait\n" \
418 "4:\t.size _L_timedlock_%=, 4b-1b\n\t" \
420 LLL_STUB_UNWIND_INFO_4 \
422 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
423 "=m" (futex), "=S" (ignore3) \
424 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
425 "4" ((int) (private)) \
430 #define lll_robust_timedlock(futex, timeout, id, private) \
431 ({ int result, ignore1, ignore2, ignore3; \
432 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
433 "jnz _L_robust_timedlock_%=\n\t" \
434 ".subsection 1\n\t" \
435 ".type _L_robust_timedlock_%=,@function\n" \
436 "_L_robust_timedlock_%=:\n" \
437 "1:\tleal %3, %%ecx\n" \
438 "0:\tmovl %8, %%edx\n" \
439 "2:\tcall __lll_robust_timedlock_wait\n" \
441 "4:\t.size _L_robust_timedlock_%=, 4b-1b\n\t" \
443 LLL_STUB_UNWIND_INFO_4 \
445 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
446 "=m" (futex), "=S" (ignore3) \
447 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
448 "4" ((int) (private)) \
452 #if defined NOT_IN_libc || defined UP
453 # define __lll_unlock_asm LOCK_INSTR "subl $1, %0\n\t"
455 # define __lll_unlock_asm "cmpl $0, %%gs:%P3\n\t" \
461 #define lll_unlock(futex, private) \
464 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
465 __asm __volatile (__lll_unlock_asm \
466 "jne _L_unlock_%=\n\t" \
467 ".subsection 1\n\t" \
468 ".type _L_unlock_%=,@function\n" \
470 "1:\tleal %0, %%eax\n" \
471 "2:\tcall __lll_unlock_wake_private\n" \
473 "4:\t.size _L_unlock_%=, 4b-1b\n\t" \
475 LLL_STUB_UNWIND_INFO_3 \
477 : "=m" (futex), "=&a" (ignore) \
478 : "m" (futex), "i" (MULTIPLE_THREADS_OFFSET) \
483 __asm __volatile (__lll_unlock_asm \
484 "jne _L_unlock_%=\n\t" \
485 ".subsection 1\n\t" \
486 ".type _L_unlock_%=,@function\n" \
488 "1:\tleal %0, %%eax\n" \
489 "0:\tmovl %5, %%ecx\n" \
490 "2:\tcall __lll_unlock_wake\n" \
492 "4:\t.size _L_unlock_%=, 4b-1b\n\t" \
494 LLL_STUB_UNWIND_INFO_4 \
496 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
497 : "i" (MULTIPLE_THREADS_OFFSET), "m" (futex), \
498 "g" ((int) (private)) \
503 #define lll_robust_unlock(futex, private) \
505 ({ int ignore, ignore2; \
506 __asm __volatile (LOCK_INSTR "andl %3, %0\n\t" \
507 "jne _L_robust_unlock_%=\n\t" \
508 ".subsection 1\n\t" \
509 ".type _L_robust_unlock_%=,@function\n" \
510 "_L_robust_unlock_%=:\n\t" \
511 "1:\tleal %0, %%eax\n" \
512 "0:\tmovl %5, %%ecx\n" \
513 "2:\tcall __lll_unlock_wake\n" \
515 "4:\t.size _L_robust_unlock_%=, 4b-1b\n\t" \
517 LLL_STUB_UNWIND_INFO_4 \
519 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
520 : "i" (FUTEX_WAITERS), "m" (futex), \
521 "g" ((int) (private)) \
526 #define lll_robust_dead(futex, private) \
529 register int _nr asm ("edx") = 1; \
530 __asm __volatile (LOCK_INSTR "orl %5, (%2)\n\t" \
535 : "0" (SYS_futex), LLL_EBX_REG (&(futex)), \
536 "c" (__lll_private_flag (FUTEX_WAKE, private)), \
537 "d" (_nr), "i" (FUTEX_OWNER_DIED), \
538 "i" (offsetof (tcbhead_t, sysinfo))); \
541 #define lll_islocked(futex) \
542 (futex != LLL_LOCK_INITIALIZER)
544 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
545 wakeup when the clone terminates. The memory location contains the
546 thread ID while the clone is running and is reset to zero
549 The macro parameter must not have any side effect. */
550 #define lll_wait_tid(tid) \
553 register __typeof (tid) _tid asm ("edx") = (tid); \
555 __asm __volatile (LLL_EBX_LOAD \
556 "1:\tmovl %1, %%eax\n\t" \
558 "cmpl $0, (%%ebx)\n\t" \
562 : "i" (SYS_futex), LLL_EBX_REG (&tid), "S" (0), \
563 "c" (FUTEX_WAIT), "d" (_tid), \
564 "i" (offsetof (tcbhead_t, sysinfo)) \
568 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
569 __attribute__ ((regparm (2))) attribute_hidden
;
570 #define lll_timedwait_tid(tid, abstime) \
575 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
578 __result = __lll_timedwait_tid (&tid, abstime); \
582 #endif /* !__ASSEMBLER__ */
584 #endif /* lowlevellock.h */