[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[glibc.git] / nptl / sysdeps / unix / sysv / linux / x86_64 / lowlevellock.h
blob4030bb7fb875858430b3e21add2643d4abaa6f77
1 /* Copyright (C) 2002, 2003, 2004, 2006, 2007 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
18 02111-1307 USA. */
20 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
23 #include <time.h>
24 #include <sys/param.h>
25 #include <bits/pthreadtypes.h>
27 #ifndef LOCK_INSTR
28 # ifdef UP
29 # define LOCK_INSTR /* nothing */
30 # else
31 # define LOCK_INSTR "lock;"
32 # endif
33 #endif
35 #define SYS_futex 202
36 #define FUTEX_WAIT 0
37 #define FUTEX_WAKE 1
38 #define FUTEX_CMP_REQUEUE 4
39 #define FUTEX_LOCK_PI 6
40 #define FUTEX_UNLOCK_PI 7
41 #define FUTEX_TRYLOCK_PI 8
44 /* Initializer for compatibility lock. */
45 #define LLL_MUTEX_LOCK_INITIALIZER (0)
46 #define LLL_MUTEX_LOCK_INITIALIZER_LOCKED (1)
47 #define LLL_MUTEX_LOCK_INITIALIZER_WAITERS (2)
49 /* Delay in spinlock loop. */
50 #define BUSY_WAIT_NOP asm ("rep; nop")
53 #define LLL_STUB_UNWIND_INFO_START \
54 ".section .eh_frame,\"a\",@progbits\n" \
55 "7:\t" ".long 9f-8f # Length of Common Information Entry\n" \
56 "8:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
57 ".byte 0x1 # CIE Version\n\t" \
58 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
59 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
60 ".sleb128 -8 # CIE Data Alignment Factor\n\t" \
61 ".byte 0x10 # CIE RA Column\n\t" \
62 ".uleb128 0x1 # Augmentation size\n\t" \
63 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
64 ".byte 0x12 # DW_CFA_def_cfa_sf\n\t" \
65 ".uleb128 0x7\n\t" \
66 ".sleb128 16\n\t" \
67 ".align 8\n" \
68 "9:\t" ".long 23f-10f # FDE Length\n" \
69 "10:\t" ".long 10b-7b # FDE CIE offset\n\t" \
70 ".long 1b-. # FDE initial location\n\t" \
71 ".long 6b-1b # FDE address range\n\t" \
72 ".uleb128 0x0 # Augmentation size\n\t" \
73 ".byte 0x16 # DW_CFA_val_expression\n\t" \
74 ".uleb128 0x10\n\t" \
75 ".uleb128 12f-11f\n" \
76 "11:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
77 ".sleb128 4b-1b\n"
78 #define LLL_STUB_UNWIND_INFO_END \
79 ".byte 0x16 # DW_CFA_val_expression\n\t" \
80 ".uleb128 0x10\n\t" \
81 ".uleb128 14f-13f\n" \
82 "13:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
83 ".sleb128 4b-2b\n" \
84 "14:\t" ".byte 0x40 + (3b-2b) # DW_CFA_advance_loc\n\t" \
85 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
86 ".uleb128 0\n\t" \
87 ".byte 0x16 # DW_CFA_val_expression\n\t" \
88 ".uleb128 0x10\n\t" \
89 ".uleb128 16f-15f\n" \
90 "15:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
91 ".sleb128 4b-3b\n" \
92 "16:\t" ".byte 0x40 + (4b-3b-1) # DW_CFA_advance_loc\n\t" \
93 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
94 ".uleb128 128\n\t" \
95 ".byte 0x16 # DW_CFA_val_expression\n\t" \
96 ".uleb128 0x10\n\t" \
97 ".uleb128 20f-17f\n" \
98 "17:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
99 ".sleb128 19f-18f\n\t" \
100 ".byte 0x0d # DW_OP_const4s\n" \
101 "18:\t" ".4byte 4b-.\n\t" \
102 ".byte 0x1c # DW_OP_minus\n\t" \
103 ".byte 0x0d # DW_OP_const4s\n" \
104 "19:\t" ".4byte 24f-.\n\t" \
105 ".byte 0x22 # DW_OP_plus\n" \
106 "20:\t" ".byte 0x40 + (5b-4b+1) # DW_CFA_advance_loc\n\t" \
107 ".byte 0x13 # DW_CFA_def_cfa_offset_sf\n\t" \
108 ".sleb128 16\n\t" \
109 ".byte 0x16 # DW_CFA_val_expression\n\t" \
110 ".uleb128 0x10\n\t" \
111 ".uleb128 22f-21f\n" \
112 "21:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
113 ".sleb128 4b-5b\n" \
114 "22:\t" ".align 8\n" \
115 "23:\t" ".previous\n"
117 /* Unwind info for
118 1: leaq ..., %rdi
119 2: subq $128, %rsp
120 3: callq ...
121 4: addq $128, %rsp
122 5: jmp 24f
124 snippet. */
125 #define LLL_STUB_UNWIND_INFO_5 \
126 LLL_STUB_UNWIND_INFO_START \
127 "12:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
128 LLL_STUB_UNWIND_INFO_END
130 /* Unwind info for
131 1: leaq ..., %rdi
132 0: movq ..., %rdx
133 2: subq $128, %rsp
134 3: callq ...
135 4: addq $128, %rsp
136 5: jmp 24f
138 snippet. */
139 #define LLL_STUB_UNWIND_INFO_6 \
140 LLL_STUB_UNWIND_INFO_START \
141 "12:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
142 ".byte 0x16 # DW_CFA_val_expression\n\t" \
143 ".uleb128 0x10\n\t" \
144 ".uleb128 26f-25f\n" \
145 "25:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
146 ".sleb128 4b-0b\n" \
147 "26:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
148 LLL_STUB_UNWIND_INFO_END
151 #define lll_futex_wait(futex, val) \
152 ({ \
153 int __status; \
154 register __typeof (val) _val __asm ("edx") = (val); \
155 __asm __volatile ("xorq %%r10, %%r10\n\t" \
156 "syscall" \
157 : "=a" (__status) \
158 : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAIT), \
159 "d" (_val) \
160 : "memory", "cc", "r10", "r11", "cx"); \
161 __status; \
165 #define lll_futex_timed_wait(futex, val, timeout) \
166 ({ \
167 register const struct timespec *__to __asm ("r10") = timeout; \
168 int __status; \
169 register __typeof (val) _val __asm ("edx") = (val); \
170 __asm __volatile ("syscall" \
171 : "=a" (__status) \
172 : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAIT), \
173 "d" (_val), "r" (__to) \
174 : "memory", "cc", "r11", "cx"); \
175 -__status; \
179 #define lll_futex_wake(futex, nr) \
180 do { \
181 int __ignore; \
182 register __typeof (nr) _nr __asm ("edx") = (nr); \
183 __asm __volatile ("syscall" \
184 : "=a" (__ignore) \
185 : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAKE), \
186 "d" (_nr) \
187 : "memory", "cc", "r10", "r11", "cx"); \
188 } while (0)
191 /* Does not preserve %eax and %ecx. */
192 extern int __lll_mutex_lock_wait (int *__futex, int __val) attribute_hidden;
193 /* Does not preserver %eax, %ecx, and %edx. */
194 extern int __lll_mutex_timedlock_wait (int *__futex, int __val,
195 const struct timespec *__abstime)
196 attribute_hidden;
197 /* Preserves all registers but %eax. */
198 extern int __lll_mutex_unlock_wait (int *__futex) attribute_hidden;
201 /* NB: in the lll_mutex_trylock macro we simply return the value in %eax
202 after the cmpxchg instruction. In case the operation succeded this
203 value is zero. In case the operation failed, the cmpxchg instruction
204 has loaded the current value of the memory work which is guaranteed
205 to be nonzero. */
206 #define lll_mutex_trylock(futex) \
207 ({ int ret; \
208 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
209 : "=a" (ret), "=m" (futex) \
210 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
211 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
212 : "memory"); \
213 ret; })
216 #define lll_robust_mutex_trylock(futex, id) \
217 ({ int ret; \
218 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
219 : "=a" (ret), "=m" (futex) \
220 : "r" (id), "m" (futex), \
221 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
222 : "memory"); \
223 ret; })
226 #define lll_mutex_cond_trylock(futex) \
227 ({ int ret; \
228 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
229 : "=a" (ret), "=m" (futex) \
230 : "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
231 "m" (futex), "0" (LLL_MUTEX_LOCK_INITIALIZER) \
232 : "memory"); \
233 ret; })
236 #define lll_mutex_lock(futex) \
237 (void) ({ int ignore1, ignore2, ignore3; \
238 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
239 "jnz 1f\n\t" \
240 ".subsection 1\n\t" \
241 ".type _L_mutex_lock_%=, @function\n" \
242 "_L_mutex_lock_%=:\n" \
243 "1:\tleaq %2, %%rdi\n" \
244 "2:\tsubq $128, %%rsp\n" \
245 "3:\tcallq __lll_mutex_lock_wait\n" \
246 "4:\taddq $128, %%rsp\n" \
247 "5:\tjmp 24f\n" \
248 "6:\t.size _L_mutex_lock_%=, 6b-1b\n\t" \
249 ".previous\n" \
250 LLL_STUB_UNWIND_INFO_5 \
251 "24:" \
252 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
253 "=a" (ignore3) \
254 : "0" (1), "m" (futex), "3" (0) \
255 : "cx", "r11", "cc", "memory"); })
258 #define lll_robust_mutex_lock(futex, id) \
259 ({ int result, ignore1, ignore2; \
260 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
261 "jnz 1f\n\t" \
262 ".subsection 1\n\t" \
263 ".type _L_robust_mutex_lock_%=, @function\n" \
264 "_L_robust_mutex_lock_%=:\n" \
265 "1:\tleaq %2, %%rdi\n" \
266 "2:\tsubq $128, %%rsp\n" \
267 "3:\tcallq __lll_robust_mutex_lock_wait\n" \
268 "4:\taddq $128, %%rsp\n" \
269 "5:\tjmp 24f\n" \
270 "6:\t.size _L_robust_mutex_lock_%=, 6b-1b\n\t" \
271 ".previous\n" \
272 LLL_STUB_UNWIND_INFO_5 \
273 "24:" \
274 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
275 "=a" (result) \
276 : "0" (id), "m" (futex), "3" (0) \
277 : "cx", "r11", "cc", "memory"); \
278 result; })
281 #define lll_mutex_cond_lock(futex) \
282 (void) ({ int ignore1, ignore2, ignore3; \
283 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
284 "jnz 1f\n\t" \
285 ".subsection 1\n\t" \
286 ".type _L_mutex_cond_lock_%=, @function\n" \
287 "_L_mutex_cond_lock_%=:\n" \
288 "1:\tleaq %2, %%rdi\n" \
289 "2:\tsubq $128, %%rsp\n" \
290 "3:\tcallq __lll_mutex_lock_wait\n" \
291 "4:\taddq $128, %%rsp\n" \
292 "5:\tjmp 24f\n" \
293 "6:\t.size _L_mutex_cond_lock_%=, 6b-1b\n\t" \
294 ".previous\n" \
295 LLL_STUB_UNWIND_INFO_5 \
296 "24:" \
297 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
298 "=a" (ignore3) \
299 : "0" (2), "m" (futex), "3" (0) \
300 : "cx", "r11", "cc", "memory"); })
303 #define lll_robust_mutex_cond_lock(futex, id) \
304 ({ int result, ignore1, ignore2; \
305 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
306 "jnz 1f\n\t" \
307 ".subsection 1\n\t" \
308 ".type _L_robust_mutex_cond_lock_%=, @function\n" \
309 "_L_robust_mutex_cond_lock_%=:\n" \
310 "1:\tleaq %2, %%rdi\n" \
311 "2:\tsubq $128, %%rsp\n" \
312 "3:\tcallq __lll_robust_mutex_lock_wait\n" \
313 "4:\taddq $128, %%rsp\n" \
314 "5:\tjmp 24f\n" \
315 "6:\t.size _L_robust_mutex_cond_lock_%=, 6b-1b\n\t" \
316 ".previous\n" \
317 LLL_STUB_UNWIND_INFO_5 \
318 "24:" \
319 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
320 "=a" (result) \
321 : "0" (id | FUTEX_WAITERS), "m" (futex), "3" (0) \
322 : "cx", "r11", "cc", "memory"); \
323 result; })
326 #define lll_mutex_timedlock(futex, timeout) \
327 ({ int result, ignore1, ignore2, ignore3; \
328 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %4\n\t" \
329 "jnz 1f\n\t" \
330 ".subsection 1\n\t" \
331 ".type _L_mutex_timedlock_%=, @function\n" \
332 "_L_mutex_timedlock_%=:\n" \
333 "1:\tleaq %4, %%rdi\n" \
334 "0:\tmovq %8, %%rdx\n" \
335 "2:\tsubq $128, %%rsp\n" \
336 "3:\tcallq __lll_mutex_timedlock_wait\n" \
337 "4:\taddq $128, %%rsp\n" \
338 "5:\tjmp 24f\n" \
339 "6:\t.size _L_mutex_timedlock_%=, 6b-1b\n\t" \
340 ".previous\n" \
341 LLL_STUB_UNWIND_INFO_6 \
342 "24:" \
343 : "=a" (result), "=&D" (ignore1), "=S" (ignore2), \
344 "=&d" (ignore3), "=m" (futex) \
345 : "0" (0), "2" (1), "m" (futex), "m" (timeout) \
346 : "memory", "cx", "cc", "r10", "r11"); \
347 result; })
350 #define lll_robust_mutex_timedlock(futex, timeout, id) \
351 ({ int result, ignore1, ignore2, ignore3; \
352 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %4\n\t" \
353 "jnz 1f\n\t" \
354 ".subsection 1\n\t" \
355 ".type _L_robust_mutex_timedlock_%=, @function\n" \
356 "_L_robust_mutex_timedlock_%=:\n" \
357 "1:\tleaq %4, %%rdi\n" \
358 "0:\tmovq %8, %%rdx\n" \
359 "2:\tsubq $128, %%rsp\n" \
360 "3:\tcallq __lll_robust_mutex_timedlock_wait\n" \
361 "4:\taddq $128, %%rsp\n" \
362 "5:\tjmp 24f\n" \
363 "6:\t.size _L_robust_mutex_timedlock_%=, 6b-1b\n\t" \
364 ".previous\n" \
365 LLL_STUB_UNWIND_INFO_6 \
366 "24:" \
367 : "=a" (result), "=&D" (ignore1), "=S" (ignore2), \
368 "=&d" (ignore3), "=m" (futex) \
369 : "0" (0), "2" (id), "m" (futex), "m" (timeout) \
370 : "memory", "cx", "cc", "r10", "r11"); \
371 result; })
374 #define lll_mutex_unlock(futex) \
375 (void) ({ int ignore; \
376 __asm __volatile (LOCK_INSTR "decl %0\n\t" \
377 "jne 1f\n\t" \
378 ".subsection 1\n\t" \
379 ".type _L_mutex_unlock_%=, @function\n" \
380 "_L_mutex_unlock_%=:\n" \
381 "1:\tleaq %0, %%rdi\n" \
382 "2:\tsubq $128, %%rsp\n" \
383 "3:\tcallq __lll_mutex_unlock_wake\n" \
384 "4:\taddq $128, %%rsp\n" \
385 "5:\tjmp 24f\n" \
386 "6:\t.size _L_mutex_unlock_%=, 6b-1b\n\t" \
387 ".previous\n" \
388 LLL_STUB_UNWIND_INFO_5 \
389 "24:" \
390 : "=m" (futex), "=&D" (ignore) \
391 : "m" (futex) \
392 : "ax", "cx", "r11", "cc", "memory"); })
395 #define lll_robust_mutex_unlock(futex) \
396 (void) ({ int ignore; \
397 __asm __volatile (LOCK_INSTR "andl %2, %0\n\t" \
398 "jne 1f\n\t" \
399 ".subsection 1\n\t" \
400 ".type _L_robust_mutex_unlock_%=, @function\n" \
401 "_L_robust_mutex_unlock_%=:\n" \
402 "1:\tleaq %0, %%rdi\n" \
403 "2:\tsubq $128, %%rsp\n" \
404 "3:\tcallq __lll_mutex_unlock_wake\n" \
405 "4:\taddq $128, %%rsp\n" \
406 "5:\tjmp 24f\n" \
407 "6:\t.size _L_robust_mutex_unlock_%=, 6b-1b\n\t"\
408 ".previous\n" \
409 LLL_STUB_UNWIND_INFO_5 \
410 "24:" \
411 : "=m" (futex), "=&D" (ignore) \
412 : "i" (FUTEX_WAITERS), "m" (futex) \
413 : "ax", "cx", "r11", "cc", "memory"); })
416 #define lll_robust_mutex_dead(futex) \
417 (void) ({ int ignore; \
418 __asm __volatile (LOCK_INSTR "orl %3, (%2)\n\t" \
419 "syscall" \
420 : "=m" (futex), "=a" (ignore) \
421 : "D" (&(futex)), "i" (FUTEX_OWNER_DIED), \
422 "S" (FUTEX_WAKE), "1" (__NR_futex), \
423 "d" (1) \
424 : "cx", "r11", "cc", "memory"); })
427 /* Returns non-zero if error happened, zero if success. */
428 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val) \
429 ({ int __res; \
430 register int __nr_move __asm ("r10") = nr_move; \
431 register void *__mutex __asm ("r8") = mutex; \
432 register int __val __asm ("r9") = val; \
433 __asm __volatile ("syscall" \
434 : "=a" (__res) \
435 : "0" (__NR_futex), "D" ((void *) ftx), \
436 "S" (FUTEX_CMP_REQUEUE), "d" (nr_wake), \
437 "r" (__nr_move), "r" (__mutex), "r" (__val) \
438 : "cx", "r11", "cc", "memory"); \
439 __res < 0; })
442 #define lll_mutex_islocked(futex) \
443 (futex != LLL_MUTEX_LOCK_INITIALIZER)
446 /* We have a separate internal lock implementation which is not tied
447 to binary compatibility. */
449 /* Type for lock object. */
450 typedef int lll_lock_t;
452 /* Initializers for lock. */
453 #define LLL_LOCK_INITIALIZER (0)
454 #define LLL_LOCK_INITIALIZER_LOCKED (1)
457 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
460 /* The states of a lock are:
461 0 - untaken
462 1 - taken by one user
463 2 - taken by more users */
466 #if defined NOT_IN_libc || defined UP
467 # define lll_trylock(futex) lll_mutex_trylock (futex)
468 # define lll_lock(futex) lll_mutex_lock (futex)
469 # define lll_unlock(futex) lll_mutex_unlock (futex)
470 #else
471 /* Special versions of the macros for use in libc itself. They avoid
472 the lock prefix when the thread library is not used.
474 The code sequence to avoid unnecessary lock prefixes is what the AMD
475 guys suggested. If you do not like it, bring it up with AMD.
477 XXX In future we might even want to avoid it on UP machines. */
479 # define lll_trylock(futex) \
480 ({ unsigned char ret; \
481 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
482 "je 0f\n\t" \
483 "lock; cmpxchgl %2, %1\n\t" \
484 "jmp 1f\n" \
485 "0:\tcmpxchgl %2, %1\n\t" \
486 "1:setne %0" \
487 : "=a" (ret), "=m" (futex) \
488 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
489 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
490 : "memory"); \
491 ret; })
494 # define lll_lock(futex) \
495 (void) ({ int ignore1, ignore2, ignore3; \
496 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
497 "je 0f\n\t" \
498 "lock; cmpxchgl %0, %2\n\t" \
499 "jnz 1f\n\t" \
500 "jmp 24f\n" \
501 "0:\tcmpxchgl %0, %2\n\t" \
502 "jnz 1f\n\t" \
503 ".subsection 1\n\t" \
504 ".type _L_lock_%=, @function\n" \
505 "_L_lock_%=:\n" \
506 "1:\tleaq %2, %%rdi\n" \
507 "2:\tsubq $128, %%rsp\n" \
508 "3:\tcallq __lll_mutex_lock_wait\n" \
509 "4:\taddq $128, %%rsp\n" \
510 "5:\tjmp 24f\n" \
511 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
512 ".previous\n" \
513 LLL_STUB_UNWIND_INFO_5 \
514 "24:" \
515 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
516 "=a" (ignore3) \
517 : "0" (1), "m" (futex), "3" (0) \
518 : "cx", "r11", "cc", "memory"); })
521 # define lll_unlock(futex) \
522 (void) ({ int ignore; \
523 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
524 "je 0f\n\t" \
525 "lock; decl %0\n\t" \
526 "jne 1f\n\t" \
527 "jmp 24f\n" \
528 "0:\tdecl %0\n\t" \
529 "jne 1f\n\t" \
530 ".subsection 1\n\t" \
531 ".type _L_unlock_%=, @function\n" \
532 "_L_unlock_%=:\n" \
533 "1:\tleaq %0, %%rdi\n" \
534 "2:\tsubq $128, %%rsp\n" \
535 "3:\tcallq __lll_mutex_unlock_wake\n" \
536 "4:\taddq $128, %%rsp\n" \
537 "5:\tjmp 24f\n" \
538 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
539 ".previous\n" \
540 LLL_STUB_UNWIND_INFO_5 \
541 "24:" \
542 : "=m" (futex), "=&D" (ignore) \
543 : "m" (futex) \
544 : "ax", "cx", "r11", "cc", "memory"); })
545 #endif
548 #define lll_islocked(futex) \
549 (futex != LLL_MUTEX_LOCK_INITIALIZER)
552 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
553 wakeup when the clone terminates. The memory location contains the
554 thread ID while the clone is running and is reset to zero
555 afterwards.
557 The macro parameter must not have any side effect. */
558 #define lll_wait_tid(tid) \
559 do { \
560 int __ignore; \
561 register __typeof (tid) _tid asm ("edx") = (tid); \
562 if (_tid != 0) \
563 __asm __volatile ("xorq %%r10, %%r10\n\t" \
564 "1:\tmovq %2, %%rax\n\t" \
565 "syscall\n\t" \
566 "cmpl $0, (%%rdi)\n\t" \
567 "jne 1b" \
568 : "=&a" (__ignore) \
569 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
570 "d" (_tid) \
571 : "memory", "cc", "r10", "r11", "cx"); \
572 } while (0)
574 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
575 attribute_hidden;
576 #define lll_timedwait_tid(tid, abstime) \
577 ({ \
578 int __result = 0; \
579 if (tid != 0) \
581 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
582 __result = EINVAL; \
583 else \
584 __result = __lll_timedwait_tid (&tid, abstime); \
586 __result; })
589 /* Conditional variable handling. */
591 extern void __lll_cond_wait (pthread_cond_t *cond) attribute_hidden;
592 extern int __lll_cond_timedwait (pthread_cond_t *cond,
593 const struct timespec *abstime)
594 attribute_hidden;
595 extern void __lll_cond_wake (pthread_cond_t *cond) attribute_hidden;
596 extern void __lll_cond_broadcast (pthread_cond_t *cond) attribute_hidden;
599 #define lll_cond_wait(cond) \
600 __lll_cond_wait (cond)
601 #define lll_cond_timedwait(cond, abstime) \
602 __lll_cond_timedwait (cond, abstime)
603 #define lll_cond_wake(cond) \
604 __lll_cond_wake (cond)
605 #define lll_cond_broadcast(cond) \
606 __lll_cond_broadcast (cond)
609 #endif /* lowlevellock.h */