1 /* Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008 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
25 # include <sys/param.h>
26 # include <bits/pthreadtypes.h>
27 # include <kernel-features.h>
28 # include <tcb-offsets.h>
32 # define LOCK_INSTR /* nothing */
34 # define LOCK_INSTR "lock;"
50 #define FUTEX_CMP_REQUEUE 4
51 #define FUTEX_WAKE_OP 5
52 #define FUTEX_LOCK_PI 6
53 #define FUTEX_UNLOCK_PI 7
54 #define FUTEX_TRYLOCK_PI 8
55 #define FUTEX_PRIVATE_FLAG 128
57 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
59 /* Values for 'private' parameter of locking macros. Yes, the
60 definition seems to be backwards. But it is not. The bit will be
61 reversed before passing to the system call. */
63 #define LLL_SHARED FUTEX_PRIVATE_FLAG
66 #if !defined NOT_IN_libc || defined IS_IN_rtld
67 /* In libc.so or ld.so all futexes are private. */
68 # ifdef __ASSUME_PRIVATE_FUTEX
69 # define __lll_private_flag(fl, private) \
70 ((fl) | FUTEX_PRIVATE_FLAG)
72 # define __lll_private_flag(fl, private) \
73 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
76 # ifdef __ASSUME_PRIVATE_FUTEX
77 # define __lll_private_flag(fl, private) \
78 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
80 # define __lll_private_flag(fl, private) \
81 (__builtin_constant_p (private) \
83 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
85 : ({ unsigned int __fl = ((private) ^ FUTEX_PRIVATE_FLAG); \
86 asm ("andl %%gs:%P1, %0" : "+r" (__fl) \
87 : "i" (offsetof (struct pthread, header.private_futex))); \
94 /* Initializer for compatibility lock. */
95 #define LLL_LOCK_INITIALIZER (0)
96 #define LLL_LOCK_INITIALIZER_LOCKED (1)
97 #define LLL_LOCK_INITIALIZER_WAITERS (2)
101 # define LLL_EBX_LOAD "xchgl %2, %%ebx\n"
102 # define LLL_EBX_REG "D"
104 # define LLL_EBX_LOAD
105 # define LLL_EBX_REG "b"
108 #ifdef I386_USE_SYSENTER
110 # define LLL_ENTER_KERNEL "call *%%gs:%P6\n\t"
112 # define LLL_ENTER_KERNEL "call *_dl_sysinfo\n\t"
115 # define LLL_ENTER_KERNEL "int $0x80\n\t"
118 /* Delay in spinlock loop. */
119 #define BUSY_WAIT_NOP asm ("rep; nop")
122 #define LLL_STUB_UNWIND_INFO_START \
123 ".section .eh_frame,\"a\",@progbits\n" \
124 "5:\t" ".long 7f-6f # Length of Common Information Entry\n" \
125 "6:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
126 ".byte 0x1 # CIE Version\n\t" \
127 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
128 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
129 ".sleb128 -4 # CIE Data Alignment Factor\n\t" \
130 ".byte 0x8 # CIE RA Column\n\t" \
131 ".uleb128 0x1 # Augmentation size\n\t" \
132 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
133 ".byte 0xc # DW_CFA_def_cfa\n\t" \
137 "7:\t" ".long 17f-8f # FDE Length\n" \
138 "8:\t" ".long 8b-5b # FDE CIE offset\n\t" \
139 ".long 1b-. # FDE initial location\n\t" \
140 ".long 4b-1b # FDE address range\n\t" \
141 ".uleb128 0x0 # Augmentation size\n\t" \
142 ".byte 0x16 # DW_CFA_val_expression\n\t" \
144 ".uleb128 10f-9f\n" \
145 "9:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
147 #define LLL_STUB_UNWIND_INFO_END \
148 ".byte 0x16 # DW_CFA_val_expression\n\t" \
150 ".uleb128 12f-11f\n" \
151 "11:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
153 "12:\t" ".byte 0x40 + (3b-2b-1) # DW_CFA_advance_loc\n\t" \
154 ".byte 0x16 # DW_CFA_val_expression\n\t" \
156 ".uleb128 16f-13f\n" \
157 "13:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
158 ".sleb128 15f-14f\n\t" \
159 ".byte 0x0d # DW_OP_const4s\n" \
160 "14:\t" ".4byte 3b-.\n\t" \
161 ".byte 0x1c # DW_OP_minus\n\t" \
162 ".byte 0x0d # DW_OP_const4s\n" \
163 "15:\t" ".4byte 18f-.\n\t" \
164 ".byte 0x22 # DW_OP_plus\n" \
165 "16:\t" ".align 4\n" \
166 "17:\t" ".previous\n"
174 #define LLL_STUB_UNWIND_INFO_3 \
175 LLL_STUB_UNWIND_INFO_START \
176 "10:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
177 LLL_STUB_UNWIND_INFO_END
186 #define LLL_STUB_UNWIND_INFO_4 \
187 LLL_STUB_UNWIND_INFO_START \
188 "10:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
189 ".byte 0x16 # DW_CFA_val_expression\n\t" \
191 ".uleb128 20f-19f\n" \
192 "19:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
194 "20:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
195 LLL_STUB_UNWIND_INFO_END
198 #define lll_futex_wait(futex, val, private) \
199 lll_futex_timed_wait (futex, val, NULL, private)
202 #define lll_futex_timed_wait(futex, val, timeout, private) \
205 register __typeof (val) _val asm ("edx") = (val); \
206 __asm __volatile (LLL_EBX_LOAD \
210 : "0" (SYS_futex), LLL_EBX_REG (futex), "S" (timeout), \
211 "c" (__lll_private_flag (FUTEX_WAIT, private)), \
212 "d" (_val), "i" (offsetof (tcbhead_t, sysinfo)) \
218 #define lll_futex_wake(futex, nr, private) \
221 register __typeof (nr) _nr asm ("edx") = (nr); \
222 __asm __volatile (LLL_EBX_LOAD \
226 : "0" (SYS_futex), LLL_EBX_REG (futex), \
227 "c" (__lll_private_flag (FUTEX_WAKE, private)), \
229 "i" (0) /* phony, to align next arg's number */, \
230 "i" (offsetof (tcbhead_t, sysinfo))); \
234 /* NB: in the lll_trylock macro we simply return the value in %eax
235 after the cmpxchg instruction. In case the operation succeded this
236 value is zero. In case the operation failed, the cmpxchg instruction
237 has loaded the current value of the memory work which is guaranteed
239 #if defined NOT_IN_libc || defined UP
240 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
242 # define __lll_trylock_asm "cmpl $0, %%gs:%P5\n\t" \
245 "0:\tcmpxchgl %2, %1"
248 #define lll_trylock(futex) \
250 __asm __volatile (__lll_trylock_asm \
251 : "=a" (ret), "=m" (futex) \
252 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
253 "0" (LLL_LOCK_INITIALIZER), \
254 "i" (MULTIPLE_THREADS_OFFSET) \
258 #define lll_robust_trylock(futex, id) \
260 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
261 : "=a" (ret), "=m" (futex) \
262 : "r" (id), "m" (futex), \
263 "0" (LLL_LOCK_INITIALIZER) \
268 #define lll_cond_trylock(futex) \
270 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
271 : "=a" (ret), "=m" (futex) \
272 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
273 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
277 #if defined NOT_IN_libc || defined UP
278 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %1, %2\n\t"
280 # define __lll_lock_asm_start "cmpl $0, %%gs:%P6\n\t" \
283 "0:\tcmpxchgl %1, %2\n\t"
286 #define lll_lock(futex, private) \
288 ({ int ignore1, ignore2; \
289 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
290 __asm __volatile (__lll_lock_asm_start \
291 "jnz _L_lock_%=\n\t" \
292 ".subsection 1\n\t" \
293 ".type _L_lock_%=,@function\n" \
295 "1:\tleal %2, %%ecx\n" \
296 "2:\tcall __lll_lock_wait_private\n" \
298 "4:\t.size _L_lock_%=, 4b-1b\n\t" \
300 LLL_STUB_UNWIND_INFO_3 \
302 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
303 : "0" (0), "1" (1), "m" (futex), \
304 "i" (MULTIPLE_THREADS_OFFSET) \
309 __asm __volatile (__lll_lock_asm_start \
310 "jnz _L_lock_%=\n\t" \
311 ".subsection 1\n\t" \
312 ".type _L_lock_%=,@function\n" \
314 "1:\tleal %2, %%edx\n" \
315 "0:\tmovl %8, %%ecx\n" \
316 "2:\tcall __lll_lock_wait\n" \
318 "4:\t.size _L_lock_%=, 4b-1b\n\t" \
320 LLL_STUB_UNWIND_INFO_4 \
322 : "=a" (ignore1), "=c" (ignore2), \
323 "=m" (futex), "=&d" (ignore3) \
324 : "1" (1), "m" (futex), \
325 "i" (MULTIPLE_THREADS_OFFSET), "0" (0), \
326 "g" ((int) (private)) \
331 #define lll_robust_lock(futex, id, private) \
332 ({ int result, ignore1, ignore2; \
333 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
334 "jnz _L_robust_lock_%=\n\t" \
335 ".subsection 1\n\t" \
336 ".type _L_robust_lock_%=,@function\n" \
337 "_L_robust_lock_%=:\n" \
338 "1:\tleal %2, %%edx\n" \
339 "0:\tmovl %7, %%ecx\n" \
340 "2:\tcall __lll_robust_lock_wait\n" \
342 "4:\t.size _L_robust_lock_%=, 4b-1b\n\t" \
344 LLL_STUB_UNWIND_INFO_4 \
346 : "=a" (result), "=c" (ignore1), "=m" (futex), \
348 : "0" (0), "1" (id), "m" (futex), "g" ((int) (private))\
353 /* Special version of lll_lock which causes the unlock function to
354 always wakeup waiters. */
355 #define lll_cond_lock(futex, private) \
357 ({ int ignore1, ignore2, ignore3; \
358 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
359 "jnz _L_cond_lock_%=\n\t" \
360 ".subsection 1\n\t" \
361 ".type _L_cond_lock_%=,@function\n" \
362 "_L_cond_lock_%=:\n" \
363 "1:\tleal %2, %%edx\n" \
364 "0:\tmovl %7, %%ecx\n" \
365 "2:\tcall __lll_lock_wait\n" \
367 "4:\t.size _L_cond_lock_%=, 4b-1b\n\t" \
369 LLL_STUB_UNWIND_INFO_4 \
371 : "=a" (ignore1), "=c" (ignore2), "=m" (futex), \
373 : "0" (0), "1" (2), "m" (futex), "g" ((int) (private))\
378 #define lll_robust_cond_lock(futex, id, private) \
379 ({ int result, ignore1, ignore2; \
380 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
381 "jnz _L_robust_cond_lock_%=\n\t" \
382 ".subsection 1\n\t" \
383 ".type _L_robust_cond_lock_%=,@function\n" \
384 "_L_robust_cond_lock_%=:\n" \
385 "1:\tleal %2, %%edx\n" \
386 "0:\tmovl %7, %%ecx\n" \
387 "2:\tcall __lll_robust_lock_wait\n" \
389 "4:\t.size _L_robust_cond_lock_%=, 4b-1b\n\t" \
391 LLL_STUB_UNWIND_INFO_4 \
393 : "=a" (result), "=c" (ignore1), "=m" (futex), \
395 : "0" (0), "1" (id | FUTEX_WAITERS), "m" (futex), \
396 "g" ((int) (private)) \
401 #define lll_timedlock(futex, timeout, private) \
402 ({ int result, ignore1, ignore2, ignore3; \
403 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
404 "jnz _L_timedlock_%=\n\t" \
405 ".subsection 1\n\t" \
406 ".type _L_timedlock_%=,@function\n" \
407 "_L_timedlock_%=:\n" \
408 "1:\tleal %3, %%ecx\n" \
409 "0:\tmovl %8, %%edx\n" \
410 "2:\tcall __lll_timedlock_wait\n" \
412 "4:\t.size _L_timedlock_%=, 4b-1b\n\t" \
414 LLL_STUB_UNWIND_INFO_4 \
416 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
417 "=m" (futex), "=S" (ignore3) \
418 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
419 "4" ((int) (private)) \
424 #define lll_robust_timedlock(futex, timeout, id, private) \
425 ({ int result, ignore1, ignore2, ignore3; \
426 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
427 "jnz _L_robust_timedlock_%=\n\t" \
428 ".subsection 1\n\t" \
429 ".type _L_robust_timedlock_%=,@function\n" \
430 "_L_robust_timedlock_%=:\n" \
431 "1:\tleal %3, %%ecx\n" \
432 "0:\tmovl %8, %%edx\n" \
433 "2:\tcall __lll_robust_timedlock_wait\n" \
435 "4:\t.size _L_robust_timedlock_%=, 4b-1b\n\t" \
437 LLL_STUB_UNWIND_INFO_4 \
439 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
440 "=m" (futex), "=S" (ignore3) \
441 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
442 "4" ((int) (private)) \
446 #if defined NOT_IN_libc || defined UP
447 # define __lll_unlock_asm LOCK_INSTR "subl $1, %0\n\t"
449 # define __lll_unlock_asm "cmpl $0, %%gs:%P3\n\t" \
455 #define lll_unlock(futex, private) \
458 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
459 __asm __volatile (__lll_unlock_asm \
460 "jne _L_unlock_%=\n\t" \
461 ".subsection 1\n\t" \
462 ".type _L_unlock_%=,@function\n" \
464 "1:\tleal %0, %%eax\n" \
465 "2:\tcall __lll_unlock_wake_private\n" \
467 "4:\t.size _L_unlock_%=, 4b-1b\n\t" \
469 LLL_STUB_UNWIND_INFO_3 \
471 : "=m" (futex), "=&a" (ignore) \
472 : "m" (futex), "i" (MULTIPLE_THREADS_OFFSET) \
477 __asm __volatile (__lll_unlock_asm \
478 "jne _L_unlock_%=\n\t" \
479 ".subsection 1\n\t" \
480 ".type _L_unlock_%=,@function\n" \
482 "1:\tleal %0, %%eax\n" \
483 "0:\tmovl %5, %%ecx\n" \
484 "2:\tcall __lll_unlock_wake\n" \
486 "4:\t.size _L_unlock_%=, 4b-1b\n\t" \
488 LLL_STUB_UNWIND_INFO_4 \
490 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
491 : "i" (MULTIPLE_THREADS_OFFSET), "m" (futex), \
492 "g" ((int) (private)) \
497 #define lll_robust_unlock(futex, private) \
499 ({ int ignore, ignore2; \
500 __asm __volatile (LOCK_INSTR "andl %3, %0\n\t" \
501 "jne _L_robust_unlock_%=\n\t" \
502 ".subsection 1\n\t" \
503 ".type _L_robust_unlock_%=,@function\n" \
504 "_L_robust_unlock_%=:\n\t" \
505 "1:\tleal %0, %%eax\n" \
506 "0:\tmovl %5, %%ecx\n" \
507 "2:\tcall __lll_unlock_wake\n" \
509 "4:\t.size _L_robust_unlock_%=, 4b-1b\n\t" \
511 LLL_STUB_UNWIND_INFO_4 \
513 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
514 : "i" (FUTEX_WAITERS), "m" (futex), \
515 "g" ((int) (private)) \
520 #define lll_robust_dead(futex, private) \
523 register int _nr asm ("edx") = 1; \
524 __asm __volatile (LOCK_INSTR "orl %5, (%2)\n\t" \
529 : "0" (SYS_futex), LLL_EBX_REG (&(futex)), \
530 "c" (__lll_private_flag (FUTEX_WAKE, private)), \
531 "d" (_nr), "i" (FUTEX_OWNER_DIED), \
532 "i" (offsetof (tcbhead_t, sysinfo))); \
535 #define lll_islocked(futex) \
536 (futex != LLL_LOCK_INITIALIZER)
538 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
539 wakeup when the clone terminates. The memory location contains the
540 thread ID while the clone is running and is reset to zero
543 The macro parameter must not have any side effect. */
544 #define lll_wait_tid(tid) \
547 register __typeof (tid) _tid asm ("edx") = (tid); \
549 __asm __volatile (LLL_EBX_LOAD \
550 "1:\tmovl %1, %%eax\n\t" \
552 "cmpl $0, (%%ebx)\n\t" \
556 : "i" (SYS_futex), LLL_EBX_REG (&tid), "S" (0), \
557 "c" (FUTEX_WAIT), "d" (_tid), \
558 "i" (offsetof (tcbhead_t, sysinfo)) \
562 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
563 __attribute__ ((regparm (2))) attribute_hidden
;
564 #define lll_timedwait_tid(tid, abstime) \
569 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
572 __result = __lll_timedwait_tid (&tid, abstime); \
576 #endif /* !__ASSEMBLER__ */
578 #endif /* lowlevellock.h */