* sysdeps/unix/sysv/linux/not-cancel.h (__openat_not_cancel,
[glibc.git] / nptl / sysdeps / unix / sysv / linux / x86_64 / lowlevellock.h
blobf91ce9b8d93bd20bfb09fa62a3d101dfb32b4980
1 /* Copyright (C) 2002, 2003, 2004, 2006 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
40 /* Initializer for compatibility lock. */
41 #define LLL_MUTEX_LOCK_INITIALIZER (0)
42 #define LLL_MUTEX_LOCK_INITIALIZER_LOCKED (1)
43 #define LLL_MUTEX_LOCK_INITIALIZER_WAITERS (2)
45 /* Delay in spinlock loop. */
46 #define BUSY_WAIT_NOP asm ("rep; nop")
49 #define lll_futex_wait(futex, val) \
50 ({ \
51 int __status; \
52 register __typeof (val) _val asm ("edx") = (val); \
53 __asm __volatile ("xorq %%r10, %%r10\n\t" \
54 "syscall" \
55 : "=a" (__status) \
56 : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAIT), \
57 "d" (_val) \
58 : "memory", "cc", "r10", "r11", "cx"); \
59 __status; \
63 #define lll_futex_timed_wait(futex, val, timeout) \
64 ({ \
65 register const struct timespec *__to __asm__ ("r10") = timeout; \
66 int __status; \
67 register __typeof (val) _val asm ("edx") = (val); \
68 __asm __volatile ("syscall" \
69 : "=a" (__status) \
70 : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAIT), \
71 "d" (_val), "r" (__to) \
72 : "memory", "cc", "r11", "cx"); \
73 __status; \
77 #define lll_futex_wake(futex, nr) \
78 do { \
79 int __ignore; \
80 register __typeof (nr) _nr asm ("edx") = (nr); \
81 __asm __volatile ("syscall" \
82 : "=a" (__ignore) \
83 : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAKE), \
84 "d" (_nr) \
85 : "memory", "cc", "r10", "r11", "cx"); \
86 } while (0)
89 /* Does not preserve %eax and %ecx. */
90 extern int __lll_mutex_lock_wait (int *__futex, int __val) attribute_hidden;
91 /* Does not preserver %eax, %ecx, and %edx. */
92 extern int __lll_mutex_timedlock_wait (int *__futex, int __val,
93 const struct timespec *__abstime)
94 attribute_hidden;
95 /* Preserves all registers but %eax. */
96 extern int __lll_mutex_unlock_wait (int *__futex) attribute_hidden;
99 /* NB: in the lll_mutex_trylock macro we simply return the value in %eax
100 after the cmpxchg instruction. In case the operation succeded this
101 value is zero. In case the operation failed, the cmpxchg instruction
102 has loaded the current value of the memory work which is guaranteed
103 to be nonzero. */
104 #define lll_mutex_trylock(futex) \
105 ({ int ret; \
106 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
107 : "=a" (ret), "=m" (futex) \
108 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
109 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
110 : "memory"); \
111 ret; })
114 #define lll_robust_mutex_trylock(futex, id) \
115 ({ int ret; \
116 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
117 : "=a" (ret), "=m" (futex) \
118 : "r" (id), "m" (futex), \
119 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
120 : "memory"); \
121 ret; })
124 #define lll_mutex_cond_trylock(futex) \
125 ({ int ret; \
126 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
127 : "=a" (ret), "=m" (futex) \
128 : "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
129 "m" (futex), "0" (LLL_MUTEX_LOCK_INITIALIZER) \
130 : "memory"); \
131 ret; })
134 #define lll_mutex_lock(futex) \
135 (void) ({ int ignore1, ignore2, ignore3; \
136 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
137 "jnz 1f\n\t" \
138 ".subsection 1\n" \
139 "1:\tleaq %2, %%rdi\n\t" \
140 "subq $128, %%rsp\n\t" \
141 "callq __lll_mutex_lock_wait\n\t" \
142 "addq $128, %%rsp\n\t" \
143 "jmp 2f\n\t" \
144 ".previous\n" \
145 "2:" \
146 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
147 "=a" (ignore3) \
148 : "0" (1), "m" (futex), "3" (0) \
149 : "cx", "r11", "cc", "memory"); })
152 #define lll_robust_mutex_lock(futex, id) \
153 ({ int result, ignore1, ignore2; \
154 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
155 "jnz 1f\n\t" \
156 ".subsection 1\n" \
157 "1:\tleaq %2, %%rdi\n\t" \
158 "subq $128, %%rsp\n\t" \
159 "callq __lll_robust_mutex_lock_wait\n\t" \
160 "addq $128, %%rsp\n\t" \
161 "jmp 2f\n\t" \
162 ".previous\n" \
163 "2:" \
164 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
165 "=a" (result) \
166 : "0" (id), "m" (futex), "3" (0) \
167 : "cx", "r11", "cc", "memory"); \
168 result; })
171 #define lll_mutex_cond_lock(futex) \
172 (void) ({ int ignore1, ignore2, ignore3; \
173 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
174 "jnz 1f\n\t" \
175 ".subsection 1\n" \
176 "1:\tleaq %2, %%rdi\n\t" \
177 "subq $128, %%rsp\n\t" \
178 "callq __lll_mutex_lock_wait\n\t" \
179 "addq $128, %%rsp\n\t" \
180 "jmp 2f\n\t" \
181 ".previous\n" \
182 "2:" \
183 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
184 "=a" (ignore3) \
185 : "0" (2), "m" (futex), "3" (0) \
186 : "cx", "r11", "cc", "memory"); })
189 #define lll_robust_mutex_cond_lock(futex, id) \
190 ({ int result, ignore1, ignore2; \
191 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
192 "jnz 1f\n\t" \
193 ".subsection 1\n" \
194 "1:\tleaq %2, %%rdi\n\t" \
195 "subq $128, %%rsp\n\t" \
196 "callq __lll_robust_mutex_lock_wait\n\t" \
197 "addq $128, %%rsp\n\t" \
198 "jmp 2f\n\t" \
199 ".previous\n" \
200 "2:" \
201 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
202 "=a" (result) \
203 : "0" (id | FUTEX_WAITERS), "m" (futex), "3" (0) \
204 : "cx", "r11", "cc", "memory"); \
205 result; })
208 #define lll_mutex_timedlock(futex, timeout) \
209 ({ int result, ignore1, ignore2, ignore3; \
210 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %4\n\t" \
211 "jnz 1f\n\t" \
212 ".subsection 1\n" \
213 "1:\tleaq %4, %%rdi\n\t" \
214 "movq %8, %%rdx\n\t" \
215 "subq $128, %%rsp\n\t" \
216 "callq __lll_mutex_timedlock_wait\n\t" \
217 "addq $128, %%rsp\n\t" \
218 "jmp 2f\n\t" \
219 ".previous\n" \
220 "2:" \
221 : "=a" (result), "=&D" (ignore1), "=S" (ignore2), \
222 "=&d" (ignore3), "=m" (futex) \
223 : "0" (0), "2" (1), "m" (futex), "m" (timeout) \
224 : "memory", "cx", "cc", "r10", "r11"); \
225 result; })
228 #define lll_robust_mutex_timedlock(futex, timeout, id) \
229 ({ int result, ignore1, ignore2, ignore3; \
230 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %4\n\t" \
231 "jnz 1f\n\t" \
232 ".subsection 1\n" \
233 "1:\tleaq %4, %%rdi\n\t" \
234 "movq %8, %%rdx\n\t" \
235 "subq $128, %%rsp\n\t" \
236 "callq __lll_robust_mutex_timedlock_wait\n\t" \
237 "addq $128, %%rsp\n\t" \
238 "jmp 2f\n\t" \
239 ".previous\n" \
240 "2:" \
241 : "=a" (result), "=&D" (ignore1), "=S" (ignore2), \
242 "=&d" (ignore3), "=m" (futex) \
243 : "0" (0), "2" (id), "m" (futex), "m" (timeout) \
244 : "memory", "cx", "cc", "r10", "r11"); \
245 result; })
248 #define lll_mutex_unlock(futex) \
249 (void) ({ int ignore; \
250 __asm __volatile (LOCK_INSTR "decl %0\n\t" \
251 "jne 1f\n\t" \
252 ".subsection 1\n" \
253 "1:\tleaq %0, %%rdi\n\t" \
254 "subq $128, %%rsp\n\t" \
255 "callq __lll_mutex_unlock_wake\n\t" \
256 "addq $128, %%rsp\n\t" \
257 "jmp 2f\n\t" \
258 ".previous\n" \
259 "2:" \
260 : "=m" (futex), "=&D" (ignore) \
261 : "m" (futex) \
262 : "ax", "cx", "r11", "cc", "memory"); })
265 #define lll_robust_mutex_unlock(futex) \
266 (void) ({ int ignore; \
267 __asm __volatile (LOCK_INSTR "andl %2, %0\n\t" \
268 "jne 1f\n\t" \
269 ".subsection 1\n" \
270 "1:\tleaq %0, %%rdi\n\t" \
271 "subq $128, %%rsp\n\t" \
272 "callq __lll_mutex_unlock_wake\n\t" \
273 "addq $128, %%rsp\n\t" \
274 "jmp 2f\n\t" \
275 ".previous\n" \
276 "2:" \
277 : "=m" (futex), "=&D" (ignore) \
278 : "i" (FUTEX_TID_MASK), "m" (futex) \
279 : "ax", "cx", "r11", "cc", "memory"); })
282 #define lll_robust_mutex_dead(futex) \
283 (void) ({ int ignore; \
284 __asm __volatile (LOCK_INSTR "orl %3, (%2)\n\t" \
285 "syscall" \
286 : "=m" (futex), "=a" (ignore) \
287 : "D" (&(futex)), "i" (FUTEX_OWNER_DIED), \
288 "S" (FUTEX_WAKE), "1" (__NR_futex), \
289 "d" (1) \
290 : "cx", "r11", "cc", "memory"); })
293 #define lll_mutex_islocked(futex) \
294 (futex != LLL_MUTEX_LOCK_INITIALIZER)
297 /* We have a separate internal lock implementation which is not tied
298 to binary compatibility. */
300 /* Type for lock object. */
301 typedef int lll_lock_t;
303 /* Initializers for lock. */
304 #define LLL_LOCK_INITIALIZER (0)
305 #define LLL_LOCK_INITIALIZER_LOCKED (1)
308 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
311 /* The states of a lock are:
312 0 - untaken
313 1 - taken by one user
314 2 - taken by more users */
317 #if defined NOT_IN_libc || defined UP
318 # define lll_trylock(futex) lll_mutex_trylock (futex)
319 # define lll_lock(futex) lll_mutex_lock (futex)
320 # define lll_unlock(futex) lll_mutex_unlock (futex)
321 #else
322 /* Special versions of the macros for use in libc itself. They avoid
323 the lock prefix when the thread library is not used.
325 The code sequence to avoid unnecessary lock prefixes is what the AMD
326 guys suggested. If you do not like it, bring it up with AMD.
328 XXX In future we might even want to avoid it on UP machines. */
330 # define lll_trylock(futex) \
331 ({ unsigned char ret; \
332 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
333 "je 0f\n\t" \
334 "lock; cmpxchgl %2, %1\n\t" \
335 "jmp 1f\n" \
336 "0:\tcmpxchgl %2, %1\n\t" \
337 "1:setne %0" \
338 : "=a" (ret), "=m" (futex) \
339 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
340 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
341 : "memory"); \
342 ret; })
345 # define lll_lock(futex) \
346 (void) ({ int ignore1, ignore2, ignore3; \
347 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
348 "je 0f\n\t" \
349 "lock; cmpxchgl %0, %2\n\t" \
350 "jnz 1f\n\t" \
351 "jmp 2f\n" \
352 "0:\tcmpxchgl %0, %2\n\t" \
353 "jnz 1f\n\t" \
354 ".subsection 1\n" \
355 "1:\tleaq %2, %%rdi\n\t" \
356 "subq $128, %%rsp\n\t" \
357 "callq __lll_mutex_lock_wait\n\t" \
358 "addq $128, %%rsp\n\t" \
359 "jmp 2f\n\t" \
360 ".previous\n" \
361 "2:" \
362 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
363 "=a" (ignore3) \
364 : "0" (1), "m" (futex), "3" (0) \
365 : "cx", "r11", "cc", "memory"); })
368 # define lll_unlock(futex) \
369 (void) ({ int ignore; \
370 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
371 "je 0f\n\t" \
372 "lock; decl %0\n\t" \
373 "jne 1f\n\t" \
374 "jmp 2f\n" \
375 "0:\tdecl %0\n\t" \
376 "jne 1f\n\t" \
377 ".subsection 1\n" \
378 "1:\tleaq %0, %%rdi\n\t" \
379 "subq $128, %%rsp\n\t" \
380 "callq __lll_mutex_unlock_wake\n\t" \
381 "addq $128, %%rsp\n\t" \
382 "jmp 2f\n\t" \
383 ".previous\n" \
384 "2:" \
385 : "=m" (futex), "=&D" (ignore) \
386 : "m" (futex) \
387 : "ax", "cx", "r11", "cc", "memory"); })
388 #endif
391 #define lll_islocked(futex) \
392 (futex != LLL_MUTEX_LOCK_INITIALIZER)
395 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
396 wakeup when the clone terminates. The memory location contains the
397 thread ID while the clone is running and is reset to zero
398 afterwards.
400 The macro parameter must not have any side effect. */
401 #define lll_wait_tid(tid) \
402 do { \
403 int __ignore; \
404 register __typeof (tid) _tid asm ("edx") = (tid); \
405 if (_tid != 0) \
406 __asm __volatile ("xorq %%r10, %%r10\n\t" \
407 "1:\tmovq %2, %%rax\n\t" \
408 "syscall\n\t" \
409 "cmpl $0, (%%rdi)\n\t" \
410 "jne 1b" \
411 : "=&a" (__ignore) \
412 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
413 "d" (_tid) \
414 : "memory", "cc", "r10", "r11", "cx"); \
415 } while (0)
417 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
418 attribute_hidden;
419 #define lll_timedwait_tid(tid, abstime) \
420 ({ \
421 int __result = 0; \
422 if (tid != 0) \
424 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
425 __result = EINVAL; \
426 else \
427 __result = __lll_timedwait_tid (&tid, abstime); \
429 __result; })
432 /* Conditional variable handling. */
434 extern void __lll_cond_wait (pthread_cond_t *cond) attribute_hidden;
435 extern int __lll_cond_timedwait (pthread_cond_t *cond,
436 const struct timespec *abstime)
437 attribute_hidden;
438 extern void __lll_cond_wake (pthread_cond_t *cond) attribute_hidden;
439 extern void __lll_cond_broadcast (pthread_cond_t *cond) attribute_hidden;
442 #define lll_cond_wait(cond) \
443 __lll_cond_wait (cond)
444 #define lll_cond_timedwait(cond, abstime) \
445 __lll_cond_timedwait (cond, abstime)
446 #define lll_cond_wake(cond) \
447 __lll_cond_wake (cond)
448 #define lll_cond_broadcast(cond) \
449 __lll_cond_broadcast (cond)
452 #endif /* lowlevellock.h */