* sysdeps/unix/sysv/linux/powerpc/lowlevellock.h
[glibc.git] / nptl / sysdeps / unix / sysv / linux / x86_64 / lowlevellock.h
blob6323d4850db44369fdb0671c07ea59f84a2e5cb7
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>
26 #include <kernel-features.h>
27 #include <tcb-offsets.h>
29 #ifndef LOCK_INSTR
30 # ifdef UP
31 # define LOCK_INSTR /* nothing */
32 # else
33 # define LOCK_INSTR "lock;"
34 # endif
35 #endif
37 #define SYS_futex 202
38 #define FUTEX_WAIT 0
39 #define FUTEX_WAKE 1
40 #define FUTEX_CMP_REQUEUE 4
41 #define FUTEX_LOCK_PI 6
42 #define FUTEX_UNLOCK_PI 7
43 #define FUTEX_TRYLOCK_PI 8
44 #define FUTEX_PRIVATE_FLAG 128
47 /* Values for 'private' parameter of locking macros. Yes, the
48 definition seems to be backwards. But it is not. The bit will be
49 reversed before passing to the system call. */
50 #define LLL_PRIVATE 0
51 #define LLL_SHARED FUTEX_PRIVATE_FLAG
53 #if !defined NOT_IN_libc || defined IS_IN_rtld
54 /* In libc.so or ld.so all futexes are private. */
55 # ifdef __ASSUME_PRIVATE_FUTEX
56 # define __lll_private_flag(fl, private) \
57 ((fl) | FUTEX_PRIVATE_FLAG)
58 # else
59 # define __lll_private_flag(fl, private) \
60 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
61 # endif
62 #else
63 # ifdef __ASSUME_PRIVATE_FUTEX
64 # define __lll_private_flag(fl, private) \
65 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
66 # else
67 # define __lll_private_flag(fl, private) \
68 (__builtin_constant_p (private) \
69 ? ((private) == 0 \
70 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
71 : (fl)) \
72 : ({ unsigned int __fl = ((private) ^ FUTEX_PRIVATE_FLAG); \
73 asm ("andl %%fs:%P1, %0" : "+r" (__fl) \
74 : "i" offsetof (struct pthread, header.private_futex)); \
75 __fl | (fl); })
76 # endif
77 #endif
79 /* Initializer for compatibility lock. */
80 #define LLL_MUTEX_LOCK_INITIALIZER (0)
81 #define LLL_MUTEX_LOCK_INITIALIZER_LOCKED (1)
82 #define LLL_MUTEX_LOCK_INITIALIZER_WAITERS (2)
84 /* Delay in spinlock loop. */
85 #define BUSY_WAIT_NOP asm ("rep; nop")
88 #define LLL_STUB_UNWIND_INFO_START \
89 ".section .eh_frame,\"a\",@progbits\n" \
90 "7:\t" ".long 9f-8f # Length of Common Information Entry\n" \
91 "8:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
92 ".byte 0x1 # CIE Version\n\t" \
93 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
94 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
95 ".sleb128 -8 # CIE Data Alignment Factor\n\t" \
96 ".byte 0x10 # CIE RA Column\n\t" \
97 ".uleb128 0x1 # Augmentation size\n\t" \
98 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
99 ".byte 0x12 # DW_CFA_def_cfa_sf\n\t" \
100 ".uleb128 0x7\n\t" \
101 ".sleb128 16\n\t" \
102 ".align 8\n" \
103 "9:\t" ".long 23f-10f # FDE Length\n" \
104 "10:\t" ".long 10b-7b # FDE CIE offset\n\t" \
105 ".long 1b-. # FDE initial location\n\t" \
106 ".long 6b-1b # FDE address range\n\t" \
107 ".uleb128 0x0 # Augmentation size\n\t" \
108 ".byte 0x16 # DW_CFA_val_expression\n\t" \
109 ".uleb128 0x10\n\t" \
110 ".uleb128 12f-11f\n" \
111 "11:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
112 ".sleb128 4b-1b\n"
113 #define LLL_STUB_UNWIND_INFO_END \
114 ".byte 0x16 # DW_CFA_val_expression\n\t" \
115 ".uleb128 0x10\n\t" \
116 ".uleb128 14f-13f\n" \
117 "13:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
118 ".sleb128 4b-2b\n" \
119 "14:\t" ".byte 0x40 + (3b-2b) # DW_CFA_advance_loc\n\t" \
120 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
121 ".uleb128 0\n\t" \
122 ".byte 0x16 # DW_CFA_val_expression\n\t" \
123 ".uleb128 0x10\n\t" \
124 ".uleb128 16f-15f\n" \
125 "15:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
126 ".sleb128 4b-3b\n" \
127 "16:\t" ".byte 0x40 + (4b-3b-1) # DW_CFA_advance_loc\n\t" \
128 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
129 ".uleb128 128\n\t" \
130 ".byte 0x16 # DW_CFA_val_expression\n\t" \
131 ".uleb128 0x10\n\t" \
132 ".uleb128 20f-17f\n" \
133 "17:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
134 ".sleb128 19f-18f\n\t" \
135 ".byte 0x0d # DW_OP_const4s\n" \
136 "18:\t" ".4byte 4b-.\n\t" \
137 ".byte 0x1c # DW_OP_minus\n\t" \
138 ".byte 0x0d # DW_OP_const4s\n" \
139 "19:\t" ".4byte 24f-.\n\t" \
140 ".byte 0x22 # DW_OP_plus\n" \
141 "20:\t" ".byte 0x40 + (5b-4b+1) # DW_CFA_advance_loc\n\t" \
142 ".byte 0x13 # DW_CFA_def_cfa_offset_sf\n\t" \
143 ".sleb128 16\n\t" \
144 ".byte 0x16 # DW_CFA_val_expression\n\t" \
145 ".uleb128 0x10\n\t" \
146 ".uleb128 22f-21f\n" \
147 "21:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
148 ".sleb128 4b-5b\n" \
149 "22:\t" ".align 8\n" \
150 "23:\t" ".previous\n"
152 /* Unwind info for
153 1: leaq ..., %rdi
154 2: subq $128, %rsp
155 3: callq ...
156 4: addq $128, %rsp
157 5: jmp 24f
159 snippet. */
160 #define LLL_STUB_UNWIND_INFO_5 \
161 LLL_STUB_UNWIND_INFO_START \
162 "12:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
163 LLL_STUB_UNWIND_INFO_END
165 /* Unwind info for
166 1: leaq ..., %rdi
167 0: movq ..., %rdx
168 2: subq $128, %rsp
169 3: callq ...
170 4: addq $128, %rsp
171 5: jmp 24f
173 snippet. */
174 #define LLL_STUB_UNWIND_INFO_6 \
175 LLL_STUB_UNWIND_INFO_START \
176 "12:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
177 ".byte 0x16 # DW_CFA_val_expression\n\t" \
178 ".uleb128 0x10\n\t" \
179 ".uleb128 26f-25f\n" \
180 "25:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
181 ".sleb128 4b-0b\n" \
182 "26:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
183 LLL_STUB_UNWIND_INFO_END
186 #define lll_futex_wait(futex, val, private) \
187 lll_futex_timed_wait(futex, val, NULL, private)
190 #define lll_futex_timed_wait(futex, val, timeout, private) \
191 ({ \
192 register const struct timespec *__to __asm ("r10") = timeout; \
193 int __status; \
194 register __typeof (val) _val __asm ("edx") = (val); \
195 __asm __volatile ("syscall" \
196 : "=a" (__status) \
197 : "0" (SYS_futex), "D" (futex), \
198 "S" (__lll_private_flag (FUTEX_WAIT, private)), \
199 "d" (_val), "r" (__to) \
200 : "memory", "cc", "r11", "cx"); \
201 __status; \
205 #define lll_futex_wake(futex, nr, private) \
206 do { \
207 int __ignore; \
208 register __typeof (nr) _nr __asm ("edx") = (nr); \
209 __asm __volatile ("syscall" \
210 : "=a" (__ignore) \
211 : "0" (SYS_futex), "D" (futex), \
212 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
213 "d" (_nr) \
214 : "memory", "cc", "r10", "r11", "cx"); \
215 } while (0)
218 #define lll_private_futex_wait(futex, val) \
219 lll_futex_timed_wait (futex, val, NULL, LLL_PRIVATE)
221 #define lll_private_futex_timed_wait(futex, val, timeout) \
222 lll_futex_timed_wait (futex, val, timeout, LLL_PRIVATE)
224 #define lll_private_futex_wake(futex, nr) \
225 lll_futex_wake (futex, nr, LLL_PRIVATE)
228 /* Does not preserve %eax and %ecx. */
229 extern int __lll_mutex_lock_wait (int *__futex, int __val) attribute_hidden;
230 /* Does not preserver %eax, %ecx, and %edx. */
231 extern int __lll_mutex_timedlock_wait (int *__futex, int __val,
232 const struct timespec *__abstime)
233 attribute_hidden;
234 /* Preserves all registers but %eax. */
235 extern int __lll_mutex_unlock_wait (int *__futex) attribute_hidden;
238 /* NB: in the lll_mutex_trylock macro we simply return the value in %eax
239 after the cmpxchg instruction. In case the operation succeded this
240 value is zero. In case the operation failed, the cmpxchg instruction
241 has loaded the current value of the memory work which is guaranteed
242 to be nonzero. */
243 #define lll_mutex_trylock(futex) \
244 ({ int ret; \
245 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
246 : "=a" (ret), "=m" (futex) \
247 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
248 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
249 : "memory"); \
250 ret; })
253 #define lll_robust_mutex_trylock(futex, id) \
254 ({ int ret; \
255 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
256 : "=a" (ret), "=m" (futex) \
257 : "r" (id), "m" (futex), \
258 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
259 : "memory"); \
260 ret; })
263 #define lll_mutex_cond_trylock(futex) \
264 ({ int ret; \
265 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
266 : "=a" (ret), "=m" (futex) \
267 : "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
268 "m" (futex), "0" (LLL_MUTEX_LOCK_INITIALIZER) \
269 : "memory"); \
270 ret; })
273 #define lll_mutex_lock(futex) \
274 (void) ({ int ignore1, ignore2, ignore3; \
275 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
276 "jnz 1f\n\t" \
277 ".subsection 1\n\t" \
278 ".type _L_mutex_lock_%=, @function\n" \
279 "_L_mutex_lock_%=:\n" \
280 "1:\tleaq %2, %%rdi\n" \
281 "2:\tsubq $128, %%rsp\n" \
282 "3:\tcallq __lll_mutex_lock_wait\n" \
283 "4:\taddq $128, %%rsp\n" \
284 "5:\tjmp 24f\n" \
285 "6:\t.size _L_mutex_lock_%=, 6b-1b\n\t" \
286 ".previous\n" \
287 LLL_STUB_UNWIND_INFO_5 \
288 "24:" \
289 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
290 "=a" (ignore3) \
291 : "0" (1), "m" (futex), "3" (0) \
292 : "cx", "r11", "cc", "memory"); })
295 #define lll_robust_mutex_lock(futex, id) \
296 ({ int result, ignore1, ignore2; \
297 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
298 "jnz 1f\n\t" \
299 ".subsection 1\n\t" \
300 ".type _L_robust_mutex_lock_%=, @function\n" \
301 "_L_robust_mutex_lock_%=:\n" \
302 "1:\tleaq %2, %%rdi\n" \
303 "2:\tsubq $128, %%rsp\n" \
304 "3:\tcallq __lll_robust_mutex_lock_wait\n" \
305 "4:\taddq $128, %%rsp\n" \
306 "5:\tjmp 24f\n" \
307 "6:\t.size _L_robust_mutex_lock_%=, 6b-1b\n\t" \
308 ".previous\n" \
309 LLL_STUB_UNWIND_INFO_5 \
310 "24:" \
311 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
312 "=a" (result) \
313 : "0" (id), "m" (futex), "3" (0) \
314 : "cx", "r11", "cc", "memory"); \
315 result; })
318 #define lll_mutex_cond_lock(futex) \
319 (void) ({ int ignore1, ignore2, ignore3; \
320 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
321 "jnz 1f\n\t" \
322 ".subsection 1\n\t" \
323 ".type _L_mutex_cond_lock_%=, @function\n" \
324 "_L_mutex_cond_lock_%=:\n" \
325 "1:\tleaq %2, %%rdi\n" \
326 "2:\tsubq $128, %%rsp\n" \
327 "3:\tcallq __lll_mutex_lock_wait\n" \
328 "4:\taddq $128, %%rsp\n" \
329 "5:\tjmp 24f\n" \
330 "6:\t.size _L_mutex_cond_lock_%=, 6b-1b\n\t" \
331 ".previous\n" \
332 LLL_STUB_UNWIND_INFO_5 \
333 "24:" \
334 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
335 "=a" (ignore3) \
336 : "0" (2), "m" (futex), "3" (0) \
337 : "cx", "r11", "cc", "memory"); })
340 #define lll_robust_mutex_cond_lock(futex, id) \
341 ({ int result, ignore1, ignore2; \
342 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
343 "jnz 1f\n\t" \
344 ".subsection 1\n\t" \
345 ".type _L_robust_mutex_cond_lock_%=, @function\n" \
346 "_L_robust_mutex_cond_lock_%=:\n" \
347 "1:\tleaq %2, %%rdi\n" \
348 "2:\tsubq $128, %%rsp\n" \
349 "3:\tcallq __lll_robust_mutex_lock_wait\n" \
350 "4:\taddq $128, %%rsp\n" \
351 "5:\tjmp 24f\n" \
352 "6:\t.size _L_robust_mutex_cond_lock_%=, 6b-1b\n\t" \
353 ".previous\n" \
354 LLL_STUB_UNWIND_INFO_5 \
355 "24:" \
356 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
357 "=a" (result) \
358 : "0" (id | FUTEX_WAITERS), "m" (futex), "3" (0) \
359 : "cx", "r11", "cc", "memory"); \
360 result; })
363 #define lll_mutex_timedlock(futex, timeout) \
364 ({ int result, ignore1, ignore2, ignore3; \
365 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %4\n\t" \
366 "jnz 1f\n\t" \
367 ".subsection 1\n\t" \
368 ".type _L_mutex_timedlock_%=, @function\n" \
369 "_L_mutex_timedlock_%=:\n" \
370 "1:\tleaq %4, %%rdi\n" \
371 "0:\tmovq %8, %%rdx\n" \
372 "2:\tsubq $128, %%rsp\n" \
373 "3:\tcallq __lll_mutex_timedlock_wait\n" \
374 "4:\taddq $128, %%rsp\n" \
375 "5:\tjmp 24f\n" \
376 "6:\t.size _L_mutex_timedlock_%=, 6b-1b\n\t" \
377 ".previous\n" \
378 LLL_STUB_UNWIND_INFO_6 \
379 "24:" \
380 : "=a" (result), "=&D" (ignore1), "=S" (ignore2), \
381 "=&d" (ignore3), "=m" (futex) \
382 : "0" (0), "2" (1), "m" (futex), "m" (timeout) \
383 : "memory", "cx", "cc", "r10", "r11"); \
384 result; })
387 #define lll_robust_mutex_timedlock(futex, timeout, id) \
388 ({ int result, ignore1, ignore2, ignore3; \
389 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %4\n\t" \
390 "jnz 1f\n\t" \
391 ".subsection 1\n\t" \
392 ".type _L_robust_mutex_timedlock_%=, @function\n" \
393 "_L_robust_mutex_timedlock_%=:\n" \
394 "1:\tleaq %4, %%rdi\n" \
395 "0:\tmovq %8, %%rdx\n" \
396 "2:\tsubq $128, %%rsp\n" \
397 "3:\tcallq __lll_robust_mutex_timedlock_wait\n" \
398 "4:\taddq $128, %%rsp\n" \
399 "5:\tjmp 24f\n" \
400 "6:\t.size _L_robust_mutex_timedlock_%=, 6b-1b\n\t" \
401 ".previous\n" \
402 LLL_STUB_UNWIND_INFO_6 \
403 "24:" \
404 : "=a" (result), "=&D" (ignore1), "=S" (ignore2), \
405 "=&d" (ignore3), "=m" (futex) \
406 : "0" (0), "2" (id), "m" (futex), "m" (timeout) \
407 : "memory", "cx", "cc", "r10", "r11"); \
408 result; })
411 #define lll_mutex_unlock(futex) \
412 (void) ({ int ignore; \
413 __asm __volatile (LOCK_INSTR "decl %0\n\t" \
414 "jne 1f\n\t" \
415 ".subsection 1\n\t" \
416 ".type _L_mutex_unlock_%=, @function\n" \
417 "_L_mutex_unlock_%=:\n" \
418 "1:\tleaq %0, %%rdi\n" \
419 "2:\tsubq $128, %%rsp\n" \
420 "3:\tcallq __lll_mutex_unlock_wake\n" \
421 "4:\taddq $128, %%rsp\n" \
422 "5:\tjmp 24f\n" \
423 "6:\t.size _L_mutex_unlock_%=, 6b-1b\n\t" \
424 ".previous\n" \
425 LLL_STUB_UNWIND_INFO_5 \
426 "24:" \
427 : "=m" (futex), "=&D" (ignore) \
428 : "m" (futex) \
429 : "ax", "cx", "r11", "cc", "memory"); })
432 #define lll_robust_mutex_unlock(futex) \
433 (void) ({ int ignore; \
434 __asm __volatile (LOCK_INSTR "andl %2, %0\n\t" \
435 "jne 1f\n\t" \
436 ".subsection 1\n\t" \
437 ".type _L_robust_mutex_unlock_%=, @function\n" \
438 "_L_robust_mutex_unlock_%=:\n" \
439 "1:\tleaq %0, %%rdi\n" \
440 "2:\tsubq $128, %%rsp\n" \
441 "3:\tcallq __lll_mutex_unlock_wake\n" \
442 "4:\taddq $128, %%rsp\n" \
443 "5:\tjmp 24f\n" \
444 "6:\t.size _L_robust_mutex_unlock_%=, 6b-1b\n\t"\
445 ".previous\n" \
446 LLL_STUB_UNWIND_INFO_5 \
447 "24:" \
448 : "=m" (futex), "=&D" (ignore) \
449 : "i" (FUTEX_WAITERS), "m" (futex) \
450 : "ax", "cx", "r11", "cc", "memory"); })
453 #define lll_robust_mutex_dead(futex) \
454 (void) ({ int ignore; \
455 __asm __volatile (LOCK_INSTR "orl %3, (%2)\n\t" \
456 "syscall" \
457 : "=m" (futex), "=a" (ignore) \
458 : "D" (&(futex)), "i" (FUTEX_OWNER_DIED), \
459 "S" (FUTEX_WAKE), "1" (__NR_futex), \
460 "d" (1) \
461 : "cx", "r11", "cc", "memory"); })
464 /* Returns non-zero if error happened, zero if success. */
465 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val) \
466 ({ int __res; \
467 register int __nr_move __asm ("r10") = nr_move; \
468 register void *__mutex __asm ("r8") = mutex; \
469 register int __val __asm ("r9") = val; \
470 __asm __volatile ("syscall" \
471 : "=a" (__res) \
472 : "0" (__NR_futex), "D" ((void *) ftx), \
473 "S" (FUTEX_CMP_REQUEUE), "d" (nr_wake), \
474 "r" (__nr_move), "r" (__mutex), "r" (__val) \
475 : "cx", "r11", "cc", "memory"); \
476 __res < 0; })
479 #define lll_mutex_islocked(futex) \
480 (futex != LLL_MUTEX_LOCK_INITIALIZER)
483 /* We have a separate internal lock implementation which is not tied
484 to binary compatibility. */
486 /* Type for lock object. */
487 typedef int lll_lock_t;
489 /* Initializers for lock. */
490 #define LLL_LOCK_INITIALIZER (0)
491 #define LLL_LOCK_INITIALIZER_LOCKED (1)
494 /* The states of a lock are:
495 0 - untaken
496 1 - taken by one user
497 2 - taken by more users */
500 #if defined NOT_IN_libc || defined UP
501 # define lll_trylock(futex) lll_mutex_trylock (futex)
502 # define lll_lock(futex) lll_mutex_lock (futex)
503 # define lll_unlock(futex) lll_mutex_unlock (futex)
504 #else
505 /* Special versions of the macros for use in libc itself. They avoid
506 the lock prefix when the thread library is not used.
508 The code sequence to avoid unnecessary lock prefixes is what the AMD
509 guys suggested. If you do not like it, bring it up with AMD.
511 XXX In future we might even want to avoid it on UP machines. */
513 # define lll_trylock(futex) \
514 ({ unsigned char ret; \
515 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
516 "je 0f\n\t" \
517 "lock; cmpxchgl %2, %1\n\t" \
518 "jmp 1f\n" \
519 "0:\tcmpxchgl %2, %1\n\t" \
520 "1:setne %0" \
521 : "=a" (ret), "=m" (futex) \
522 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
523 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
524 : "memory"); \
525 ret; })
528 # define lll_lock(futex) \
529 (void) ({ int ignore1, ignore2, ignore3; \
530 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
531 "je 0f\n\t" \
532 "lock; cmpxchgl %0, %2\n\t" \
533 "jnz 1f\n\t" \
534 "jmp 24f\n" \
535 "0:\tcmpxchgl %0, %2\n\t" \
536 "jnz 1f\n\t" \
537 ".subsection 1\n\t" \
538 ".type _L_lock_%=, @function\n" \
539 "_L_lock_%=:\n" \
540 "1:\tleaq %2, %%rdi\n" \
541 "2:\tsubq $128, %%rsp\n" \
542 "3:\tcallq __lll_mutex_lock_wait\n" \
543 "4:\taddq $128, %%rsp\n" \
544 "5:\tjmp 24f\n" \
545 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
546 ".previous\n" \
547 LLL_STUB_UNWIND_INFO_5 \
548 "24:" \
549 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
550 "=a" (ignore3) \
551 : "0" (1), "m" (futex), "3" (0) \
552 : "cx", "r11", "cc", "memory"); })
555 # define lll_unlock(futex) \
556 (void) ({ int ignore; \
557 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
558 "je 0f\n\t" \
559 "lock; decl %0\n\t" \
560 "jne 1f\n\t" \
561 "jmp 24f\n" \
562 "0:\tdecl %0\n\t" \
563 "jne 1f\n\t" \
564 ".subsection 1\n\t" \
565 ".type _L_unlock_%=, @function\n" \
566 "_L_unlock_%=:\n" \
567 "1:\tleaq %0, %%rdi\n" \
568 "2:\tsubq $128, %%rsp\n" \
569 "3:\tcallq __lll_mutex_unlock_wake\n" \
570 "4:\taddq $128, %%rsp\n" \
571 "5:\tjmp 24f\n" \
572 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
573 ".previous\n" \
574 LLL_STUB_UNWIND_INFO_5 \
575 "24:" \
576 : "=m" (futex), "=&D" (ignore) \
577 : "m" (futex) \
578 : "ax", "cx", "r11", "cc", "memory"); })
579 #endif
582 #define lll_islocked(futex) \
583 (futex != LLL_MUTEX_LOCK_INITIALIZER)
586 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
587 wakeup when the clone terminates. The memory location contains the
588 thread ID while the clone is running and is reset to zero
589 afterwards.
591 The macro parameter must not have any side effect. */
592 #define lll_wait_tid(tid) \
593 do { \
594 int __ignore; \
595 register __typeof (tid) _tid asm ("edx") = (tid); \
596 if (_tid != 0) \
597 __asm __volatile ("xorq %%r10, %%r10\n\t" \
598 "1:\tmovq %2, %%rax\n\t" \
599 "syscall\n\t" \
600 "cmpl $0, (%%rdi)\n\t" \
601 "jne 1b" \
602 : "=&a" (__ignore) \
603 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
604 "d" (_tid) \
605 : "memory", "cc", "r10", "r11", "cx"); \
606 } while (0)
608 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
609 attribute_hidden;
610 #define lll_timedwait_tid(tid, abstime) \
611 ({ \
612 int __result = 0; \
613 if (tid != 0) \
615 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
616 __result = EINVAL; \
617 else \
618 __result = __lll_timedwait_tid (&tid, abstime); \
620 __result; })
623 /* Conditional variable handling. */
625 extern void __lll_cond_wait (pthread_cond_t *cond) attribute_hidden;
626 extern int __lll_cond_timedwait (pthread_cond_t *cond,
627 const struct timespec *abstime)
628 attribute_hidden;
629 extern void __lll_cond_wake (pthread_cond_t *cond) attribute_hidden;
630 extern void __lll_cond_broadcast (pthread_cond_t *cond) attribute_hidden;
633 #define lll_cond_wait(cond) \
634 __lll_cond_wait (cond)
635 #define lll_cond_timedwait(cond, abstime) \
636 __lll_cond_timedwait (cond, abstime)
637 #define lll_cond_wake(cond) \
638 __lll_cond_wake (cond)
639 #define lll_cond_broadcast(cond) \
640 __lll_cond_broadcast (cond)
643 #endif /* lowlevellock.h */