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
20 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
24 #include <sys/param.h>
25 #include <bits/pthreadtypes.h>
29 # define LOCK_INSTR /* nothing */
31 # define LOCK_INSTR "lock;"
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) \
52 register __typeof (val) _val asm ("edx") = (val); \
53 __asm __volatile ("xorq %%r10, %%r10\n\t" \
56 : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAIT), \
58 : "memory", "cc", "r10", "r11", "cx"); \
63 #define lll_futex_timed_wait(futex, val, timeout) \
65 register const struct timespec *__to __asm__ ("r10") = timeout; \
67 register __typeof (val) _val asm ("edx") = (val); \
68 __asm __volatile ("syscall" \
70 : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAIT), \
71 "d" (_val), "r" (__to) \
72 : "memory", "cc", "r11", "cx"); \
77 #define lll_futex_wake(futex, nr) \
80 register __typeof (nr) _nr asm ("edx") = (nr); \
81 __asm __volatile ("syscall" \
83 : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAKE), \
85 : "memory", "cc", "r10", "r11", "cx"); \
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
)
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
104 #define lll_mutex_trylock(futex) \
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) \
114 #define lll_robust_mutex_trylock(futex, id) \
116 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
117 : "=a" (ret), "=m" (futex) \
118 : "r" (id), "m" (futex), \
119 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
124 #define lll_mutex_cond_trylock(futex) \
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) \
134 #define lll_mutex_lock(futex) \
135 (void) ({ int ignore1, ignore2, ignore3; \
136 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
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" \
146 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
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" \
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" \
164 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
166 : "0" (id), "m" (futex), "3" (0) \
167 : "cx", "r11", "cc", "memory"); \
171 #define lll_mutex_cond_lock(futex) \
172 (void) ({ int ignore1, ignore2, ignore3; \
173 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
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" \
183 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
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" \
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" \
201 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
203 : "0" (id | FUTEX_WAITERS), "m" (futex), "3" (0) \
204 : "cx", "r11", "cc", "memory"); \
208 #define lll_mutex_timedlock(futex, timeout) \
209 ({ int result, ignore1, ignore2, ignore3; \
210 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %4\n\t" \
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" \
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"); \
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" \
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" \
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"); \
248 #define lll_mutex_unlock(futex) \
249 (void) ({ int ignore; \
250 __asm __volatile (LOCK_INSTR "decl %0\n\t" \
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" \
260 : "=m" (futex), "=&D" (ignore) \
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" \
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" \
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" \
286 : "=m" (futex), "=a" (ignore) \
287 : "D" (&(futex)), "i" (FUTEX_OWNER_DIED), \
288 "S" (FUTEX_WAKE), "1" (__NR_futex), \
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:
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)
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" \
334 "lock; cmpxchgl %2, %1\n\t" \
336 "0:\tcmpxchgl %2, %1\n\t" \
338 : "=a" (ret), "=m" (futex) \
339 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
340 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
345 # define lll_lock(futex) \
346 (void) ({ int ignore1, ignore2, ignore3; \
347 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
349 "lock; cmpxchgl %0, %2\n\t" \
352 "0:\tcmpxchgl %0, %2\n\t" \
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" \
362 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
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" \
372 "lock; decl %0\n\t" \
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" \
385 : "=m" (futex), "=&D" (ignore) \
387 : "ax", "cx", "r11", "cc", "memory"); })
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
400 The macro parameter must not have any side effect. */
401 #define lll_wait_tid(tid) \
404 register __typeof (tid) _tid asm ("edx") = (tid); \
406 __asm __volatile ("xorq %%r10, %%r10\n\t" \
407 "1:\tmovq %2, %%rax\n\t" \
409 "cmpl $0, (%%rdi)\n\t" \
412 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
414 : "memory", "cc", "r10", "r11", "cx"); \
417 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
419 #define lll_timedwait_tid(tid, abstime) \
424 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
427 __result = __lll_timedwait_tid (&tid, abstime); \
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
)
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 */