1 /* Copyright (C) 2002, 2003, 2004 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"); \
62 #define lll_futex_wake(futex, nr) \
65 register __typeof (nr) _nr asm ("edx") = (nr); \
66 __asm __volatile ("syscall" \
68 : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAKE), \
70 : "memory", "cc", "r10", "r11", "cx"); \
74 /* Does not preserve %eax and %ecx. */
75 extern int __lll_mutex_lock_wait (int *__futex, int __val) attribute_hidden;
76 /* Does not preserver %eax, %ecx, and %edx. */
77 extern int __lll_mutex_timedlock_wait (int *__futex, int __val,
78 const struct timespec *__abstime)
80 /* Preserves all registers but %eax. */
81 extern int __lll_mutex_unlock_wait (int *__futex) attribute_hidden;
84 /* NB: in the lll_mutex_trylock macro we simply return the value in %eax
85 after the cmpxchg instruction. In case the operation succeded this
86 value is zero. In case the operation failed, the cmpxchg instruction
87 has loaded the current value of the memory work which is guaranteed
89 #define lll_mutex_trylock(futex) \
91 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
92 : "=a" (ret), "=m" (futex) \
93 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
94 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
99 #define lll_mutex_cond_trylock(futex) \
101 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
102 : "=a" (ret), "=m" (futex) \
103 : "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
104 "m" (futex), "0" (LLL_MUTEX_LOCK_INITIALIZER) \
109 #define lll_mutex_lock(futex) \
110 (void) ({ int ignore1, ignore2, ignore3; \
111 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
114 "1:\tleaq %2, %%rdi\n\t" \
115 "subq $128, %%rsp\n\t" \
116 "callq __lll_mutex_lock_wait\n\t" \
117 "addq $128, %%rsp\n\t" \
121 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
123 : "0" (1), "m" (futex), "3" (0) \
124 : "cx", "r11", "cc", "memory"); })
127 #define lll_mutex_cond_lock(futex) \
128 (void) ({ int ignore1, ignore2, ignore3; \
129 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
132 "1:\tleaq %2, %%rdi\n\t" \
133 "subq $128, %%rsp\n\t" \
134 "callq __lll_mutex_lock_wait\n\t" \
135 "addq $128, %%rsp\n\t" \
139 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
141 : "0" (2), "m" (futex), "3" (0) \
142 : "cx", "r11", "cc", "memory"); })
145 #define lll_mutex_timedlock(futex, timeout) \
146 ({ int result, ignore1, ignore2, ignore3; \
147 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %4\n\t" \
150 "1:\tleaq %4, %%rdi\n\t" \
151 "movq %8, %%rdx\n\t" \
152 "subq $128, %%rsp\n\t" \
153 "callq __lll_mutex_timedlock_wait\n\t" \
154 "addq $128, %%rsp\n\t" \
158 : "=a" (result), "=&D" (ignore1), "=S" (ignore2), \
159 "=&d" (ignore3), "=m" (futex) \
160 : "0" (0), "2" (1), "m" (futex), "m" (timeout) \
161 : "memory", "cx", "cc", "r10", "r11"); \
165 #define lll_mutex_unlock(futex) \
166 (void) ({ int ignore; \
167 __asm __volatile (LOCK_INSTR "decl %0\n\t" \
170 "1:\tleaq %0, %%rdi\n\t" \
171 "subq $128, %%rsp\n\t" \
172 "callq __lll_mutex_unlock_wake\n\t" \
173 "addq $128, %%rsp\n\t" \
177 : "=m" (futex), "=&D" (ignore) \
179 : "ax", "cx", "r11", "cc", "memory"); })
182 #define lll_mutex_islocked(futex) \
183 (futex != LLL_MUTEX_LOCK_INITIALIZER)
186 /* We have a separate internal lock implementation which is not tied
187 to binary compatibility. */
189 /* Type for lock object. */
190 typedef int lll_lock_t;
192 /* Initializers for lock. */
193 #define LLL_LOCK_INITIALIZER (0)
194 #define LLL_LOCK_INITIALIZER_LOCKED (1)
197 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
200 /* The states of a lock are:
202 1 - taken by one user
203 2 - taken by more users */
206 #if defined NOT_IN_libc || defined UP
207 # define lll_trylock(futex) lll_mutex_trylock (futex)
208 # define lll_lock(futex) lll_mutex_lock (futex)
209 # define lll_unlock(futex) lll_mutex_unlock (futex)
211 /* Special versions of the macros for use in libc itself. They avoid
212 the lock prefix when the thread library is not used.
214 The code sequence to avoid unnecessary lock prefixes is what the AMD
215 guys suggested. If you do not like it, bring it up with AMD.
217 XXX In future we might even want to avoid it on UP machines. */
219 # define lll_trylock(futex) \
220 ({ unsigned char ret; \
221 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
223 "lock; cmpxchgl %2, %1\n\t" \
225 "0:\tcmpxchgl %2, %1\n\t" \
227 : "=a" (ret), "=m" (futex) \
228 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
229 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
234 # define lll_lock(futex) \
235 (void) ({ int ignore1, ignore2, ignore3; \
236 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
238 "lock; cmpxchgl %0, %2\n\t" \
241 "0:\tcmpxchgl %0, %2\n\t" \
244 "1:\tleaq %2, %%rdi\n\t" \
245 "subq $128, %%rsp\n\t" \
246 "callq __lll_mutex_lock_wait\n\t" \
247 "addq $128, %%rsp\n\t" \
251 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
253 : "0" (1), "m" (futex), "3" (0) \
254 : "cx", "r11", "cc", "memory"); })
257 # define lll_unlock(futex) \
258 (void) ({ int ignore; \
259 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
261 "lock; decl %0\n\t" \
267 "1:\tleaq %0, %%rdi\n\t" \
268 "subq $128, %%rsp\n\t" \
269 "callq __lll_mutex_unlock_wake\n\t" \
270 "addq $128, %%rsp\n\t" \
274 : "=m" (futex), "=&D" (ignore) \
276 : "ax", "cx", "r11", "cc", "memory"); })
280 #define lll_islocked(futex) \
281 (futex != LLL_MUTEX_LOCK_INITIALIZER)
284 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
285 wakeup when the clone terminates. The memory location contains the
286 thread ID while the clone is running and is reset to zero
289 The macro parameter must not have any side effect. */
290 #define lll_wait_tid(tid) \
293 register __typeof (tid) _tid asm ("edx") = (tid); \
295 __asm __volatile ("xorq %%r10, %%r10\n\t" \
296 "1:\tmovq %2, %%rax\n\t" \
298 "cmpl $0, (%%rdi)\n\t" \
301 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
303 : "memory", "cc", "r10", "r11", "cx"); \
306 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
308 #define lll_timedwait_tid(tid, abstime) \
313 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
316 __result = __lll_timedwait_tid (&tid, abstime); \
321 /* Conditional variable handling. */
323 extern void __lll_cond_wait (pthread_cond_t *cond) attribute_hidden;
324 extern int __lll_cond_timedwait (pthread_cond_t *cond,
325 const struct timespec *abstime)
327 extern void __lll_cond_wake (pthread_cond_t *cond) attribute_hidden;
328 extern void __lll_cond_broadcast (pthread_cond_t *cond) attribute_hidden;
331 #define lll_cond_wait(cond) \
332 __lll_cond_wait (cond)
333 #define lll_cond_timedwait(cond, abstime) \
334 __lll_cond_timedwait (cond, abstime)
335 #define lll_cond_wake(cond) \
336 __lll_cond_wake (cond)
337 #define lll_cond_broadcast(cond) \
338 __lll_cond_broadcast (cond)
341 #endif /* lowlevellock.h */