1 /* Copyright (C) 2002-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_WAIT_BITSET 9
56 #define FUTEX_WAKE_BITSET 10
57 #define FUTEX_PRIVATE_FLAG 128
59 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
61 /* Values for 'private' parameter of locking macros. Yes, the
62 definition seems to be backwards. But it is not. The bit will be
63 reversed before passing to the system call. */
65 #define LLL_SHARED FUTEX_PRIVATE_FLAG
69 #if !defined NOT_IN_libc || defined IS_IN_rtld
70 /* In libc.so or ld.so all futexes are private. */
71 # ifdef __ASSUME_PRIVATE_FUTEX
72 # define __lll_private_flag(fl, private) \
73 ((fl) | FUTEX_PRIVATE_FLAG)
75 # define __lll_private_flag(fl, private) \
76 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
79 # ifdef __ASSUME_PRIVATE_FUTEX
80 # define __lll_private_flag(fl, private) \
81 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
83 # define __lll_private_flag(fl, private) \
84 (__builtin_constant_p (private) \
86 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
88 : ({ unsigned int __fl = ((private) ^ FUTEX_PRIVATE_FLAG); \
89 asm ("andl %%fs:%P1, %0" : "+r" (__fl) \
90 : "i" (offsetof (struct pthread, header.private_futex))); \
95 /* Initializer for lock. */
96 #define LLL_LOCK_INITIALIZER (0)
97 #define LLL_LOCK_INITIALIZER_LOCKED (1)
98 #define LLL_LOCK_INITIALIZER_WAITERS (2)
100 /* Delay in spinlock loop. */
101 #define BUSY_WAIT_NOP asm ("rep; nop")
104 #define LLL_STUB_UNWIND_INFO_START \
105 ".section .eh_frame,\"a\",@progbits\n" \
106 "7:\t" ".long 9f-8f # Length of Common Information Entry\n" \
107 "8:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
108 ".byte 0x1 # CIE Version\n\t" \
109 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
110 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
111 ".sleb128 -8 # CIE Data Alignment Factor\n\t" \
112 ".byte 0x10 # CIE RA Column\n\t" \
113 ".uleb128 0x1 # Augmentation size\n\t" \
114 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
115 ".byte 0x12 # DW_CFA_def_cfa_sf\n\t" \
119 "9:\t" ".long 23f-10f # FDE Length\n" \
120 "10:\t" ".long 10b-7b # FDE CIE offset\n\t" \
121 ".long 1b-. # FDE initial location\n\t" \
122 ".long 6b-1b # FDE address range\n\t" \
123 ".uleb128 0x0 # Augmentation size\n\t" \
124 ".byte 0x16 # DW_CFA_val_expression\n\t" \
125 ".uleb128 0x10\n\t" \
126 ".uleb128 12f-11f\n" \
127 "11:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
129 #define LLL_STUB_UNWIND_INFO_END \
130 ".byte 0x16 # DW_CFA_val_expression\n\t" \
131 ".uleb128 0x10\n\t" \
132 ".uleb128 14f-13f\n" \
133 "13:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
135 "14:\t" ".byte 0x40 + (3b-2b) # DW_CFA_advance_loc\n\t" \
136 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
138 ".byte 0x16 # DW_CFA_val_expression\n\t" \
139 ".uleb128 0x10\n\t" \
140 ".uleb128 16f-15f\n" \
141 "15:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
143 "16:\t" ".byte 0x40 + (4b-3b-1) # DW_CFA_advance_loc\n\t" \
144 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
146 ".byte 0x16 # DW_CFA_val_expression\n\t" \
147 ".uleb128 0x10\n\t" \
148 ".uleb128 20f-17f\n" \
149 "17:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
150 ".sleb128 19f-18f\n\t" \
151 ".byte 0x0d # DW_OP_const4s\n" \
152 "18:\t" ".4byte 4b-.\n\t" \
153 ".byte 0x1c # DW_OP_minus\n\t" \
154 ".byte 0x0d # DW_OP_const4s\n" \
155 "19:\t" ".4byte 24f-.\n\t" \
156 ".byte 0x22 # DW_OP_plus\n" \
157 "20:\t" ".byte 0x40 + (5b-4b+1) # DW_CFA_advance_loc\n\t" \
158 ".byte 0x13 # DW_CFA_def_cfa_offset_sf\n\t" \
160 ".byte 0x16 # DW_CFA_val_expression\n\t" \
161 ".uleb128 0x10\n\t" \
162 ".uleb128 22f-21f\n" \
163 "21:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
165 "22:\t" ".align 8\n" \
166 "23:\t" ".previous\n"
176 #define LLL_STUB_UNWIND_INFO_5 \
177 LLL_STUB_UNWIND_INFO_START \
178 "12:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
179 LLL_STUB_UNWIND_INFO_END
190 #define LLL_STUB_UNWIND_INFO_6 \
191 LLL_STUB_UNWIND_INFO_START \
192 "12:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
193 ".byte 0x16 # DW_CFA_val_expression\n\t" \
194 ".uleb128 0x10\n\t" \
195 ".uleb128 26f-25f\n" \
196 "25:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
198 "26:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
199 LLL_STUB_UNWIND_INFO_END
202 #define lll_futex_wait(futex, val, private) \
203 lll_futex_timed_wait(futex, val, NULL, private)
206 #define lll_futex_timed_wait(futex, val, timeout, private) \
208 register const struct timespec *__to __asm ("r10") = timeout; \
210 register __typeof (val) _val __asm ("edx") = (val); \
211 __asm __volatile ("syscall" \
213 : "0" (SYS_futex), "D" (futex), \
214 "S" (__lll_private_flag (FUTEX_WAIT, private)), \
215 "d" (_val), "r" (__to) \
216 : "memory", "cc", "r11", "cx"); \
221 #define lll_futex_wake(futex, nr, private) \
224 register __typeof (nr) _nr __asm ("edx") = (nr); \
225 __asm __volatile ("syscall" \
227 : "0" (SYS_futex), "D" (futex), \
228 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
230 : "memory", "cc", "r10", "r11", "cx"); \
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, __libc_multiple_threads(%%rip)\n\t" \
244 "lock; cmpxchgl %2, %1\n\t" \
246 "0:\tcmpxchgl %2, %1\n\t" \
250 #define lll_trylock(futex) \
252 __asm __volatile (__lll_trylock_asm \
253 : "=a" (ret), "=m" (futex) \
254 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
255 "0" (LLL_LOCK_INITIALIZER) \
259 #define lll_robust_trylock(futex, id) \
261 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
262 : "=a" (ret), "=m" (futex) \
263 : "r" (id), "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
267 #define lll_cond_trylock(futex) \
269 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
270 : "=a" (ret), "=m" (futex) \
271 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
272 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
276 #if defined NOT_IN_libc || defined UP
277 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %4, %2\n\t" \
280 # define __lll_lock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
282 "lock; cmpxchgl %4, %2\n\t" \
285 "0:\tcmpxchgl %4, %2\n\t" \
289 #define lll_lock(futex, private) \
291 ({ int ignore1, ignore2, ignore3; \
292 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
293 __asm __volatile (__lll_lock_asm_start \
294 ".subsection 1\n\t" \
295 ".type _L_lock_%=, @function\n" \
297 "1:\tleaq %2, %%rdi\n" \
298 "2:\tsubq $128, %%rsp\n" \
299 "3:\tcallq __lll_lock_wait_private\n" \
300 "4:\taddq $128, %%rsp\n" \
302 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
304 LLL_STUB_UNWIND_INFO_5 \
306 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
308 : "0" (1), "m" (futex), "3" (0) \
309 : "cx", "r11", "cc", "memory"); \
311 __asm __volatile (__lll_lock_asm_start \
312 ".subsection 1\n\t" \
313 ".type _L_lock_%=, @function\n" \
315 "1:\tleaq %2, %%rdi\n" \
316 "2:\tsubq $128, %%rsp\n" \
317 "3:\tcallq __lll_lock_wait\n" \
318 "4:\taddq $128, %%rsp\n" \
320 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
322 LLL_STUB_UNWIND_INFO_5 \
324 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
326 : "1" (1), "m" (futex), "3" (0), "0" (private) \
327 : "cx", "r11", "cc", "memory"); \
330 #define lll_robust_lock(futex, id, private) \
331 ({ int result, ignore1, ignore2; \
332 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
334 ".subsection 1\n\t" \
335 ".type _L_robust_lock_%=, @function\n" \
336 "_L_robust_lock_%=:\n" \
337 "1:\tleaq %2, %%rdi\n" \
338 "2:\tsubq $128, %%rsp\n" \
339 "3:\tcallq __lll_robust_lock_wait\n" \
340 "4:\taddq $128, %%rsp\n" \
342 "6:\t.size _L_robust_lock_%=, 6b-1b\n\t" \
344 LLL_STUB_UNWIND_INFO_5 \
346 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
348 : "1" (id), "m" (futex), "3" (0), "0" (private) \
349 : "cx", "r11", "cc", "memory"); \
352 #define lll_cond_lock(futex, private) \
354 ({ int ignore1, ignore2, ignore3; \
355 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
357 ".subsection 1\n\t" \
358 ".type _L_cond_lock_%=, @function\n" \
359 "_L_cond_lock_%=:\n" \
360 "1:\tleaq %2, %%rdi\n" \
361 "2:\tsubq $128, %%rsp\n" \
362 "3:\tcallq __lll_lock_wait\n" \
363 "4:\taddq $128, %%rsp\n" \
365 "6:\t.size _L_cond_lock_%=, 6b-1b\n\t" \
367 LLL_STUB_UNWIND_INFO_5 \
369 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
371 : "1" (2), "m" (futex), "3" (0), "0" (private) \
372 : "cx", "r11", "cc", "memory"); \
375 #define lll_robust_cond_lock(futex, id, private) \
376 ({ int result, ignore1, ignore2; \
377 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
379 ".subsection 1\n\t" \
380 ".type _L_robust_cond_lock_%=, @function\n" \
381 "_L_robust_cond_lock_%=:\n" \
382 "1:\tleaq %2, %%rdi\n" \
383 "2:\tsubq $128, %%rsp\n" \
384 "3:\tcallq __lll_robust_lock_wait\n" \
385 "4:\taddq $128, %%rsp\n" \
387 "6:\t.size _L_robust_cond_lock_%=, 6b-1b\n\t" \
389 LLL_STUB_UNWIND_INFO_5 \
391 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
393 : "1" (id | FUTEX_WAITERS), "m" (futex), "3" (0), \
395 : "cx", "r11", "cc", "memory"); \
398 #define lll_timedlock(futex, timeout, private) \
399 ({ int result, ignore1, ignore2, ignore3; \
400 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
402 ".subsection 1\n\t" \
403 ".type _L_timedlock_%=, @function\n" \
404 "_L_timedlock_%=:\n" \
405 "1:\tleaq %4, %%rdi\n" \
406 "0:\tmovq %8, %%rdx\n" \
407 "2:\tsubq $128, %%rsp\n" \
408 "3:\tcallq __lll_timedlock_wait\n" \
409 "4:\taddq $128, %%rsp\n" \
411 "6:\t.size _L_timedlock_%=, 6b-1b\n\t" \
413 LLL_STUB_UNWIND_INFO_6 \
415 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
416 "=&d" (ignore3), "=m" (futex) \
417 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
419 : "memory", "cx", "cc", "r10", "r11"); \
422 #define lll_robust_timedlock(futex, timeout, id, private) \
423 ({ int result, ignore1, ignore2, ignore3; \
424 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
426 ".subsection 1\n\t" \
427 ".type _L_robust_timedlock_%=, @function\n" \
428 "_L_robust_timedlock_%=:\n" \
429 "1:\tleaq %4, %%rdi\n" \
430 "0:\tmovq %8, %%rdx\n" \
431 "2:\tsubq $128, %%rsp\n" \
432 "3:\tcallq __lll_robust_timedlock_wait\n" \
433 "4:\taddq $128, %%rsp\n" \
435 "6:\t.size _L_robust_timedlock_%=, 6b-1b\n\t" \
437 LLL_STUB_UNWIND_INFO_6 \
439 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
440 "=&d" (ignore3), "=m" (futex) \
441 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
443 : "memory", "cx", "cc", "r10", "r11"); \
446 #if defined NOT_IN_libc || defined UP
447 # define __lll_unlock_asm_start LOCK_INSTR "decl %0\n\t" \
450 # define __lll_unlock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
452 "lock; decl %0\n\t" \
459 #define lll_unlock(futex, private) \
462 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
463 __asm __volatile (__lll_unlock_asm_start \
464 ".subsection 1\n\t" \
465 ".type _L_unlock_%=, @function\n" \
467 "1:\tleaq %0, %%rdi\n" \
468 "2:\tsubq $128, %%rsp\n" \
469 "3:\tcallq __lll_unlock_wake_private\n" \
470 "4:\taddq $128, %%rsp\n" \
472 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
474 LLL_STUB_UNWIND_INFO_5 \
476 : "=m" (futex), "=&D" (ignore) \
478 : "ax", "cx", "r11", "cc", "memory"); \
480 __asm __volatile (__lll_unlock_asm_start \
481 ".subsection 1\n\t" \
482 ".type _L_unlock_%=, @function\n" \
484 "1:\tleaq %0, %%rdi\n" \
485 "2:\tsubq $128, %%rsp\n" \
486 "3:\tcallq __lll_unlock_wake\n" \
487 "4:\taddq $128, %%rsp\n" \
489 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
491 LLL_STUB_UNWIND_INFO_5 \
493 : "=m" (futex), "=&D" (ignore) \
494 : "m" (futex), "S" (private) \
495 : "ax", "cx", "r11", "cc", "memory"); \
498 #define lll_robust_unlock(futex, private) \
502 __asm __volatile (LOCK_INSTR "andl %2, %0\n\t" \
504 ".subsection 1\n\t" \
505 ".type _L_robust_unlock_%=, @function\n" \
506 "_L_robust_unlock_%=:\n" \
507 "1:\tleaq %0, %%rdi\n" \
508 "2:\tsubq $128, %%rsp\n" \
509 "3:\tcallq __lll_unlock_wake\n" \
510 "4:\taddq $128, %%rsp\n" \
512 "6:\t.size _L_robust_unlock_%=, 6b-1b\n\t" \
514 LLL_STUB_UNWIND_INFO_5 \
516 : "=m" (futex), "=&D" (ignore) \
517 : "i" (FUTEX_WAITERS), "m" (futex), \
519 : "ax", "cx", "r11", "cc", "memory"); \
523 #define lll_robust_dead(futex, private) \
527 __asm __volatile (LOCK_INSTR "orl %3, (%2)\n\t" \
529 : "=m" (futex), "=a" (ignore) \
530 : "D" (&(futex)), "i" (FUTEX_OWNER_DIED), \
531 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
532 "1" (__NR_futex), "d" (1) \
533 : "cx", "r11", "cc", "memory"); \
537 /* Returns non-zero if error happened, zero if success. */
538 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val, private) \
540 register int __nr_move __asm ("r10") = nr_move; \
541 register void *__mutex __asm ("r8") = mutex; \
542 register int __val __asm ("r9") = val; \
543 __asm __volatile ("syscall" \
545 : "0" (__NR_futex), "D" ((void *) ftx), \
546 "S" (__lll_private_flag (FUTEX_CMP_REQUEUE, \
547 private)), "d" (nr_wake), \
548 "r" (__nr_move), "r" (__mutex), "r" (__val) \
549 : "cx", "r11", "cc", "memory"); \
552 #define lll_islocked(futex) \
553 (futex != LLL_LOCK_INITIALIZER)
556 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
557 wakeup when the clone terminates. The memory location contains the
558 thread ID while the clone is running and is reset to zero
561 The macro parameter must not have any side effect. */
562 #define lll_wait_tid(tid) \
565 register __typeof (tid) _tid asm ("edx") = (tid); \
567 __asm __volatile ("xorq %%r10, %%r10\n\t" \
568 "1:\tmovq %2, %%rax\n\t" \
570 "cmpl $0, (%%rdi)\n\t" \
573 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
575 : "memory", "cc", "r10", "r11", "cx"); \
578 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
580 #define lll_timedwait_tid(tid, abstime) \
585 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
588 __result = __lll_timedwait_tid (&tid, abstime); \
592 #endif /* !__ASSEMBLER__ */
594 #endif /* lowlevellock.h */