Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / sysdeps / unix / sysv / linux / x86_64 / lowlevellock.h
blob0a2673974ece23bae29cb8ac25b1f66b7b439d98
1 /* Copyright (C) 2002-2014 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
22 #include <stap-probe.h>
24 #ifndef __ASSEMBLER__
25 # include <time.h>
26 # include <sys/param.h>
27 # include <bits/pthreadtypes.h>
28 # include <kernel-features.h>
29 # include <tcb-offsets.h>
31 # ifndef LOCK_INSTR
32 # ifdef UP
33 # define LOCK_INSTR /* nothing */
34 # else
35 # define LOCK_INSTR "lock;"
36 # endif
37 # endif
38 #else
39 # ifndef LOCK
40 # ifdef UP
41 # define LOCK
42 # else
43 # define LOCK lock
44 # endif
45 # endif
46 #endif
48 #define SYS_futex __NR_futex
49 #define FUTEX_WAIT 0
50 #define FUTEX_WAKE 1
51 #define FUTEX_CMP_REQUEUE 4
52 #define FUTEX_WAKE_OP 5
53 #define FUTEX_LOCK_PI 6
54 #define FUTEX_UNLOCK_PI 7
55 #define FUTEX_TRYLOCK_PI 8
56 #define FUTEX_WAIT_BITSET 9
57 #define FUTEX_WAKE_BITSET 10
58 #define FUTEX_WAIT_REQUEUE_PI 11
59 #define FUTEX_CMP_REQUEUE_PI 12
60 #define FUTEX_PRIVATE_FLAG 128
61 #define FUTEX_CLOCK_REALTIME 256
63 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
65 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
67 /* Values for 'private' parameter of locking macros. Yes, the
68 definition seems to be backwards. But it is not. The bit will be
69 reversed before passing to the system call. */
70 #define LLL_PRIVATE 0
71 #define LLL_SHARED FUTEX_PRIVATE_FLAG
73 #ifndef __ASSEMBLER__
75 #if !defined NOT_IN_libc || defined IS_IN_rtld
76 /* In libc.so or ld.so all futexes are private. */
77 # ifdef __ASSUME_PRIVATE_FUTEX
78 # define __lll_private_flag(fl, private) \
79 ((fl) | FUTEX_PRIVATE_FLAG)
80 # else
81 # define __lll_private_flag(fl, private) \
82 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
83 # endif
84 #else
85 # ifdef __ASSUME_PRIVATE_FUTEX
86 # define __lll_private_flag(fl, private) \
87 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
88 # else
89 # define __lll_private_flag(fl, private) \
90 (__builtin_constant_p (private) \
91 ? ((private) == 0 \
92 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
93 : (fl)) \
94 : ({ unsigned int __fl = ((private) ^ FUTEX_PRIVATE_FLAG); \
95 asm ("andl %%fs:%P1, %0" : "+r" (__fl) \
96 : "i" (offsetof (struct pthread, header.private_futex))); \
97 __fl | (fl); }))
98 # endif
99 #endif
101 /* Initializer for lock. */
102 #define LLL_LOCK_INITIALIZER (0)
103 #define LLL_LOCK_INITIALIZER_LOCKED (1)
104 #define LLL_LOCK_INITIALIZER_WAITERS (2)
106 /* Delay in spinlock loop. */
107 #define BUSY_WAIT_NOP asm ("rep; nop")
110 #define LLL_STUB_UNWIND_INFO_START \
111 ".section .eh_frame,\"a\",@progbits\n" \
112 "7:\t" ".long 9f-8f # Length of Common Information Entry\n" \
113 "8:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
114 ".byte 0x1 # CIE Version\n\t" \
115 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
116 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
117 ".sleb128 -8 # CIE Data Alignment Factor\n\t" \
118 ".byte 0x10 # CIE RA Column\n\t" \
119 ".uleb128 0x1 # Augmentation size\n\t" \
120 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
121 ".byte 0x12 # DW_CFA_def_cfa_sf\n\t" \
122 ".uleb128 0x7\n\t" \
123 ".sleb128 16\n\t" \
124 ".align " LP_SIZE "\n" \
125 "9:\t" ".long 23f-10f # FDE Length\n" \
126 "10:\t" ".long 10b-7b # FDE CIE offset\n\t" \
127 ".long 1b-. # FDE initial location\n\t" \
128 ".long 6b-1b # FDE address range\n\t" \
129 ".uleb128 0x0 # Augmentation size\n\t" \
130 ".byte 0x16 # DW_CFA_val_expression\n\t" \
131 ".uleb128 0x10\n\t" \
132 ".uleb128 12f-11f\n" \
133 "11:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
134 ".sleb128 4b-1b\n"
135 #define LLL_STUB_UNWIND_INFO_END \
136 ".byte 0x16 # DW_CFA_val_expression\n\t" \
137 ".uleb128 0x10\n\t" \
138 ".uleb128 14f-13f\n" \
139 "13:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
140 ".sleb128 4b-2b\n" \
141 "14:\t" ".byte 0x40 + (3b-2b) # DW_CFA_advance_loc\n\t" \
142 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
143 ".uleb128 0\n\t" \
144 ".byte 0x16 # DW_CFA_val_expression\n\t" \
145 ".uleb128 0x10\n\t" \
146 ".uleb128 16f-15f\n" \
147 "15:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
148 ".sleb128 4b-3b\n" \
149 "16:\t" ".byte 0x40 + (4b-3b-1) # DW_CFA_advance_loc\n\t" \
150 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
151 ".uleb128 128\n\t" \
152 ".byte 0x16 # DW_CFA_val_expression\n\t" \
153 ".uleb128 0x10\n\t" \
154 ".uleb128 20f-17f\n" \
155 "17:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
156 ".sleb128 19f-18f\n\t" \
157 ".byte 0x0d # DW_OP_const4s\n" \
158 "18:\t" ".4byte 4b-.\n\t" \
159 ".byte 0x1c # DW_OP_minus\n\t" \
160 ".byte 0x0d # DW_OP_const4s\n" \
161 "19:\t" ".4byte 24f-.\n\t" \
162 ".byte 0x22 # DW_OP_plus\n" \
163 "20:\t" ".byte 0x40 + (5b-4b+1) # DW_CFA_advance_loc\n\t" \
164 ".byte 0x13 # DW_CFA_def_cfa_offset_sf\n\t" \
165 ".sleb128 16\n\t" \
166 ".byte 0x16 # DW_CFA_val_expression\n\t" \
167 ".uleb128 0x10\n\t" \
168 ".uleb128 22f-21f\n" \
169 "21:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
170 ".sleb128 4b-5b\n" \
171 "22:\t" ".align " LP_SIZE "\n" \
172 "23:\t" ".previous\n"
174 /* Unwind info for
175 1: leaq ..., %rdi
176 2: subq $128, %rsp
177 3: callq ...
178 4: addq $128, %rsp
179 5: jmp 24f
181 snippet. */
182 #define LLL_STUB_UNWIND_INFO_5 \
183 LLL_STUB_UNWIND_INFO_START \
184 "12:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
185 LLL_STUB_UNWIND_INFO_END
187 /* Unwind info for
188 1: leaq ..., %rdi
189 0: movq ..., %rdx
190 2: subq $128, %rsp
191 3: callq ...
192 4: addq $128, %rsp
193 5: jmp 24f
195 snippet. */
196 #define LLL_STUB_UNWIND_INFO_6 \
197 LLL_STUB_UNWIND_INFO_START \
198 "12:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
199 ".byte 0x16 # DW_CFA_val_expression\n\t" \
200 ".uleb128 0x10\n\t" \
201 ".uleb128 26f-25f\n" \
202 "25:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
203 ".sleb128 4b-0b\n" \
204 "26:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
205 LLL_STUB_UNWIND_INFO_END
208 #define lll_futex_wait(futex, val, private) \
209 lll_futex_timed_wait(futex, val, NULL, private)
212 #define lll_futex_timed_wait(futex, val, timeout, private) \
213 ({ \
214 register const struct timespec *__to __asm ("r10") = timeout; \
215 int __status; \
216 register __typeof (val) _val __asm ("edx") = (val); \
217 __asm __volatile ("syscall" \
218 : "=a" (__status) \
219 : "0" (SYS_futex), "D" (futex), \
220 "S" (__lll_private_flag (FUTEX_WAIT, private)), \
221 "d" (_val), "r" (__to) \
222 : "memory", "cc", "r11", "cx"); \
223 __status; \
227 #define lll_futex_wake(futex, nr, private) \
228 ({ \
229 int __status; \
230 register __typeof (nr) _nr __asm ("edx") = (nr); \
231 LIBC_PROBE (lll_futex_wake, 3, futex, nr, private); \
232 __asm __volatile ("syscall" \
233 : "=a" (__status) \
234 : "0" (SYS_futex), "D" (futex), \
235 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
236 "d" (_nr) \
237 : "memory", "cc", "r10", "r11", "cx"); \
238 __status; \
242 /* NB: in the lll_trylock macro we simply return the value in %eax
243 after the cmpxchg instruction. In case the operation succeded this
244 value is zero. In case the operation failed, the cmpxchg instruction
245 has loaded the current value of the memory work which is guaranteed
246 to be nonzero. */
247 #if defined NOT_IN_libc || defined UP
248 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
249 #else
250 # define __lll_trylock_asm "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
251 "je 0f\n\t" \
252 "lock; cmpxchgl %2, %1\n\t" \
253 "jmp 1f\n\t" \
254 "0:\tcmpxchgl %2, %1\n\t" \
255 "1:"
256 #endif
258 #define lll_trylock(futex) \
259 ({ int ret; \
260 __asm __volatile (__lll_trylock_asm \
261 : "=a" (ret), "=m" (futex) \
262 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
263 "0" (LLL_LOCK_INITIALIZER) \
264 : "memory"); \
265 ret; })
267 #define lll_robust_trylock(futex, id) \
268 ({ int ret; \
269 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
270 : "=a" (ret), "=m" (futex) \
271 : "r" (id), "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
272 : "memory"); \
273 ret; })
275 #define lll_cond_trylock(futex) \
276 ({ int ret; \
277 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
278 : "=a" (ret), "=m" (futex) \
279 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
280 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
281 : "memory"); \
282 ret; })
284 #if defined NOT_IN_libc || defined UP
285 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %4, %2\n\t" \
286 "jnz 1f\n\t"
287 #else
288 # define __lll_lock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
289 "je 0f\n\t" \
290 "lock; cmpxchgl %4, %2\n\t" \
291 "jnz 1f\n\t" \
292 "jmp 24f\n" \
293 "0:\tcmpxchgl %4, %2\n\t" \
294 "jnz 1f\n\t"
295 #endif
297 #define lll_lock(futex, private) \
298 (void) \
299 ({ int ignore1, ignore2, ignore3; \
300 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
301 __asm __volatile (__lll_lock_asm_start \
302 ".subsection 1\n\t" \
303 ".type _L_lock_%=, @function\n" \
304 "_L_lock_%=:\n" \
305 "1:\tlea %2, %%" RDI_LP "\n" \
306 "2:\tsub $128, %%" RSP_LP "\n" \
307 "3:\tcallq __lll_lock_wait_private\n" \
308 "4:\tadd $128, %%" RSP_LP "\n" \
309 "5:\tjmp 24f\n" \
310 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
311 ".previous\n" \
312 LLL_STUB_UNWIND_INFO_5 \
313 "24:" \
314 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
315 "=a" (ignore3) \
316 : "0" (1), "m" (futex), "3" (0) \
317 : "cx", "r11", "cc", "memory"); \
318 else \
319 __asm __volatile (__lll_lock_asm_start \
320 ".subsection 1\n\t" \
321 ".type _L_lock_%=, @function\n" \
322 "_L_lock_%=:\n" \
323 "1:\tlea %2, %%" RDI_LP "\n" \
324 "2:\tsub $128, %%" RSP_LP "\n" \
325 "3:\tcallq __lll_lock_wait\n" \
326 "4:\tadd $128, %%" RSP_LP "\n" \
327 "5:\tjmp 24f\n" \
328 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
329 ".previous\n" \
330 LLL_STUB_UNWIND_INFO_5 \
331 "24:" \
332 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
333 "=a" (ignore3) \
334 : "1" (1), "m" (futex), "3" (0), "0" (private) \
335 : "cx", "r11", "cc", "memory"); \
336 }) \
338 #define lll_robust_lock(futex, id, private) \
339 ({ int result, ignore1, ignore2; \
340 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
341 "jnz 1f\n\t" \
342 ".subsection 1\n\t" \
343 ".type _L_robust_lock_%=, @function\n" \
344 "_L_robust_lock_%=:\n" \
345 "1:\tlea %2, %%" RDI_LP "\n" \
346 "2:\tsub $128, %%" RSP_LP "\n" \
347 "3:\tcallq __lll_robust_lock_wait\n" \
348 "4:\tadd $128, %%" RSP_LP "\n" \
349 "5:\tjmp 24f\n" \
350 "6:\t.size _L_robust_lock_%=, 6b-1b\n\t" \
351 ".previous\n" \
352 LLL_STUB_UNWIND_INFO_5 \
353 "24:" \
354 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
355 "=a" (result) \
356 : "1" (id), "m" (futex), "3" (0), "0" (private) \
357 : "cx", "r11", "cc", "memory"); \
358 result; })
360 #define lll_cond_lock(futex, private) \
361 (void) \
362 ({ int ignore1, ignore2, ignore3; \
363 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
364 "jnz 1f\n\t" \
365 ".subsection 1\n\t" \
366 ".type _L_cond_lock_%=, @function\n" \
367 "_L_cond_lock_%=:\n" \
368 "1:\tlea %2, %%" RDI_LP "\n" \
369 "2:\tsub $128, %%" RSP_LP "\n" \
370 "3:\tcallq __lll_lock_wait\n" \
371 "4:\tadd $128, %%" RSP_LP "\n" \
372 "5:\tjmp 24f\n" \
373 "6:\t.size _L_cond_lock_%=, 6b-1b\n\t" \
374 ".previous\n" \
375 LLL_STUB_UNWIND_INFO_5 \
376 "24:" \
377 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
378 "=a" (ignore3) \
379 : "1" (2), "m" (futex), "3" (0), "0" (private) \
380 : "cx", "r11", "cc", "memory"); \
383 #define lll_robust_cond_lock(futex, id, private) \
384 ({ int result, ignore1, ignore2; \
385 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
386 "jnz 1f\n\t" \
387 ".subsection 1\n\t" \
388 ".type _L_robust_cond_lock_%=, @function\n" \
389 "_L_robust_cond_lock_%=:\n" \
390 "1:\tlea %2, %%" RDI_LP "\n" \
391 "2:\tsub $128, %%" RSP_LP "\n" \
392 "3:\tcallq __lll_robust_lock_wait\n" \
393 "4:\tadd $128, %%" RSP_LP "\n" \
394 "5:\tjmp 24f\n" \
395 "6:\t.size _L_robust_cond_lock_%=, 6b-1b\n\t" \
396 ".previous\n" \
397 LLL_STUB_UNWIND_INFO_5 \
398 "24:" \
399 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
400 "=a" (result) \
401 : "1" (id | FUTEX_WAITERS), "m" (futex), "3" (0), \
402 "0" (private) \
403 : "cx", "r11", "cc", "memory"); \
404 result; })
406 #define lll_timedlock(futex, timeout, private) \
407 ({ int result, ignore1, ignore2, ignore3; \
408 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
409 "jnz 1f\n\t" \
410 ".subsection 1\n\t" \
411 ".type _L_timedlock_%=, @function\n" \
412 "_L_timedlock_%=:\n" \
413 "1:\tlea %4, %%" RDI_LP "\n" \
414 "0:\tmov %8, %%" RDX_LP "\n" \
415 "2:\tsub $128, %%" RSP_LP "\n" \
416 "3:\tcallq __lll_timedlock_wait\n" \
417 "4:\tadd $128, %%" RSP_LP "\n" \
418 "5:\tjmp 24f\n" \
419 "6:\t.size _L_timedlock_%=, 6b-1b\n\t" \
420 ".previous\n" \
421 LLL_STUB_UNWIND_INFO_6 \
422 "24:" \
423 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
424 "=&d" (ignore3), "=m" (futex) \
425 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
426 "2" (private) \
427 : "memory", "cx", "cc", "r10", "r11"); \
428 result; })
430 extern int __lll_timedlock_elision (int *futex, short *adapt_count,
431 const struct timespec *timeout,
432 int private) attribute_hidden;
434 #define lll_timedlock_elision(futex, adapt_count, timeout, private) \
435 __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
437 #define lll_robust_timedlock(futex, timeout, id, private) \
438 ({ int result, ignore1, ignore2, ignore3; \
439 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
440 "jnz 1f\n\t" \
441 ".subsection 1\n\t" \
442 ".type _L_robust_timedlock_%=, @function\n" \
443 "_L_robust_timedlock_%=:\n" \
444 "1:\tlea %4, %%" RDI_LP "\n" \
445 "0:\tmov %8, %%" RDX_LP "\n" \
446 "2:\tsub $128, %%" RSP_LP "\n" \
447 "3:\tcallq __lll_robust_timedlock_wait\n" \
448 "4:\tadd $128, %%" RSP_LP "\n" \
449 "5:\tjmp 24f\n" \
450 "6:\t.size _L_robust_timedlock_%=, 6b-1b\n\t" \
451 ".previous\n" \
452 LLL_STUB_UNWIND_INFO_6 \
453 "24:" \
454 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
455 "=&d" (ignore3), "=m" (futex) \
456 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
457 "2" (private) \
458 : "memory", "cx", "cc", "r10", "r11"); \
459 result; })
461 #if defined NOT_IN_libc || defined UP
462 # define __lll_unlock_asm_start LOCK_INSTR "decl %0\n\t" \
463 "jne 1f\n\t"
464 #else
465 # define __lll_unlock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
466 "je 0f\n\t" \
467 "lock; decl %0\n\t" \
468 "jne 1f\n\t" \
469 "jmp 24f\n\t" \
470 "0:\tdecl %0\n\t" \
471 "jne 1f\n\t"
472 #endif
474 #define lll_unlock(futex, private) \
475 (void) \
476 ({ int ignore; \
477 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
478 __asm __volatile (__lll_unlock_asm_start \
479 ".subsection 1\n\t" \
480 ".type _L_unlock_%=, @function\n" \
481 "_L_unlock_%=:\n" \
482 "1:\tlea %0, %%" RDI_LP "\n" \
483 "2:\tsub $128, %%" RSP_LP "\n" \
484 "3:\tcallq __lll_unlock_wake_private\n" \
485 "4:\tadd $128, %%" RSP_LP "\n" \
486 "5:\tjmp 24f\n" \
487 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
488 ".previous\n" \
489 LLL_STUB_UNWIND_INFO_5 \
490 "24:" \
491 : "=m" (futex), "=&D" (ignore) \
492 : "m" (futex) \
493 : "ax", "cx", "r11", "cc", "memory"); \
494 else \
495 __asm __volatile (__lll_unlock_asm_start \
496 ".subsection 1\n\t" \
497 ".type _L_unlock_%=, @function\n" \
498 "_L_unlock_%=:\n" \
499 "1:\tlea %0, %%" RDI_LP "\n" \
500 "2:\tsub $128, %%" RSP_LP "\n" \
501 "3:\tcallq __lll_unlock_wake\n" \
502 "4:\tadd $128, %%" RSP_LP "\n" \
503 "5:\tjmp 24f\n" \
504 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
505 ".previous\n" \
506 LLL_STUB_UNWIND_INFO_5 \
507 "24:" \
508 : "=m" (futex), "=&D" (ignore) \
509 : "m" (futex), "S" (private) \
510 : "ax", "cx", "r11", "cc", "memory"); \
513 #define lll_robust_unlock(futex, private) \
514 do \
516 int ignore; \
517 __asm __volatile (LOCK_INSTR "andl %2, %0\n\t" \
518 "jne 1f\n\t" \
519 ".subsection 1\n\t" \
520 ".type _L_robust_unlock_%=, @function\n" \
521 "_L_robust_unlock_%=:\n" \
522 "1:\tlea %0, %%" RDI_LP "\n" \
523 "2:\tsub $128, %%" RSP_LP "\n" \
524 "3:\tcallq __lll_unlock_wake\n" \
525 "4:\tadd $128, %%" RSP_LP "\n" \
526 "5:\tjmp 24f\n" \
527 "6:\t.size _L_robust_unlock_%=, 6b-1b\n\t" \
528 ".previous\n" \
529 LLL_STUB_UNWIND_INFO_5 \
530 "24:" \
531 : "=m" (futex), "=&D" (ignore) \
532 : "i" (FUTEX_WAITERS), "m" (futex), \
533 "S" (private) \
534 : "ax", "cx", "r11", "cc", "memory"); \
536 while (0)
538 #define lll_robust_dead(futex, private) \
539 do \
541 int ignore; \
542 __asm __volatile (LOCK_INSTR "orl %3, (%2)\n\t" \
543 "syscall" \
544 : "=m" (futex), "=a" (ignore) \
545 : "D" (&(futex)), "i" (FUTEX_OWNER_DIED), \
546 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
547 "1" (__NR_futex), "d" (1) \
548 : "cx", "r11", "cc", "memory"); \
550 while (0)
552 /* Returns non-zero if error happened, zero if success. */
553 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val, private) \
554 ({ int __res; \
555 register int __nr_move __asm ("r10") = nr_move; \
556 register void *__mutex __asm ("r8") = mutex; \
557 register int __val __asm ("r9") = val; \
558 __asm __volatile ("syscall" \
559 : "=a" (__res) \
560 : "0" (__NR_futex), "D" ((void *) ftx), \
561 "S" (__lll_private_flag (FUTEX_CMP_REQUEUE, \
562 private)), "d" (nr_wake), \
563 "r" (__nr_move), "r" (__mutex), "r" (__val) \
564 : "cx", "r11", "cc", "memory"); \
565 __res < 0; })
567 #define lll_islocked(futex) \
568 (futex != LLL_LOCK_INITIALIZER)
571 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
572 wakeup when the clone terminates. The memory location contains the
573 thread ID while the clone is running and is reset to zero
574 afterwards.
576 The macro parameter must not have any side effect. */
577 #define lll_wait_tid(tid) \
578 do { \
579 int __ignore; \
580 register __typeof (tid) _tid asm ("edx") = (tid); \
581 if (_tid != 0) \
582 __asm __volatile ("xorq %%r10, %%r10\n\t" \
583 "1:\tmovq %2, %%rax\n\t" \
584 "syscall\n\t" \
585 "cmpl $0, (%%rdi)\n\t" \
586 "jne 1b" \
587 : "=&a" (__ignore) \
588 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
589 "d" (_tid) \
590 : "memory", "cc", "r10", "r11", "cx"); \
591 } while (0)
593 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
594 attribute_hidden;
595 #define lll_timedwait_tid(tid, abstime) \
596 ({ \
597 int __result = 0; \
598 if (tid != 0) \
600 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
601 __result = EINVAL; \
602 else \
603 __result = __lll_timedwait_tid (&tid, abstime); \
605 __result; })
607 extern int __lll_lock_elision (int *futex, short *adapt_count, int private)
608 attribute_hidden;
610 extern int __lll_unlock_elision (int *lock, int private)
611 attribute_hidden;
613 extern int __lll_trylock_elision (int *lock, short *adapt_count)
614 attribute_hidden;
616 #define lll_lock_elision(futex, adapt_count, private) \
617 __lll_lock_elision (&(futex), &(adapt_count), private)
618 #define lll_unlock_elision(futex, private) \
619 __lll_unlock_elision (&(futex), private)
620 #define lll_trylock_elision(futex, adapt_count) \
621 __lll_trylock_elision (&(futex), &(adapt_count))
623 #endif /* !__ASSEMBLER__ */
625 #endif /* lowlevellock.h */