1 /* Copyright (C) 2002, 2003 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)
44 #define lll_futex_wait(futex, val) \
47 register __typeof (val) _val asm ("edx") = (val); \
48 __asm __volatile ("xorq %%r10, %%r10\n\t" \
51 : "0" (SYS_futex), "D" (&futex), "S" (FUTEX_WAIT), \
53 : "memory", "cc", "r10", "r11", "cx"); \
57 #define lll_futex_wake(futex, nr) \
60 register __typeof (nr) _nr asm ("edx") = (nr); \
61 __asm __volatile ("syscall" \
63 : "0" (SYS_futex), "D" (&futex), "S" (FUTEX_WAKE), \
65 : "memory", "cc", "r10", "r11", "cx"); \
69 /* Does not preserve %eax and %ecx. */
70 extern int __lll_mutex_lock_wait (int *__futex, int __val) attribute_hidden;
71 /* Does not preserver %eax, %ecx, and %edx. */
72 extern int __lll_mutex_timedlock_wait (int *__futex, int __val,
73 const struct timespec *__abstime)
75 /* Preserves all registers but %eax. */
76 extern int __lll_mutex_unlock_wait (int *__futex) attribute_hidden;
79 #define lll_mutex_trylock(futex) \
80 ({ unsigned char ret; \
81 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1; setne %0" \
82 : "=a" (ret), "=m" (futex) \
83 : "r" (1), "1" (futex), "0" (0) \
88 #define lll_mutex_lock(futex) \
89 (void) ({ int ignore1, ignore2; \
90 __asm __volatile (LOCK_INSTR "xaddl %0, %2\n\t" \
94 "1:\tleaq %2, %%rdi\n\t" \
95 "subq $128, %%rsp\n\t" \
96 "callq __lll_mutex_lock_wait\n\t" \
97 "addq $128, %%rsp\n\t" \
101 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex) \
102 : "0" (1), "2" (futex) \
103 : "ax", "cx", "r11", "cc", "memory"); })
106 #define lll_mutex_cond_lock(futex) \
107 (void) ({ int ignore1, ignore2; \
108 __asm __volatile (LOCK_INSTR "xaddl %0, %2\n\t" \
112 "1:\tleaq %2, %%rdi\n\t" \
113 "subq $128, %%rsp\n\t" \
114 "callq __lll_mutex_lock_wait\n\t" \
115 "addq $128, %%rsp\n\t" \
119 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex) \
120 : "0" (2), "2" (futex) \
121 : "ax", "cx", "r11", "cc", "memory"); })
124 #define lll_mutex_timedlock(futex, timeout) \
125 ({ int result, ignore1, ignore2, ignore3; \
126 __asm __volatile (LOCK_INSTR "xaddl %0, %4\n\t" \
130 "1:\tleaq %4, %%rdi\n\t" \
131 "movq %7, %%rdx\n\t" \
132 "subq $128, %%rsp\n\t" \
133 "callq __lll_mutex_timedlock_wait\n\t" \
134 "addq $128, %%rsp\n\t" \
138 : "=a" (result), "=&D" (ignore1), "=&S" (ignore2), \
139 "=&d" (ignore3), "=m" (futex) \
140 : "0" (1), "4" (futex), "m" (timeout) \
141 : "memory", "cx", "cc", "r10", "r11"); \
145 #define lll_mutex_unlock(futex) \
146 (void) ({ int ignore; \
147 __asm __volatile (LOCK_INSTR "decl %0\n\t" \
150 "1:\tleaq %0, %%rdi\n\t" \
151 "subq $128, %%rsp\n\t" \
152 "callq __lll_mutex_unlock_wake\n\t" \
153 "addq $128, %%rsp\n\t" \
157 : "=m" (futex), "=&D" (ignore) \
159 : "ax", "cx", "r11", "cc", "memory"); })
162 #define lll_mutex_islocked(futex) \
166 /* We have a separate internal lock implementation which is not tied
167 to binary compatibility. */
169 /* Type for lock object. */
170 typedef int lll_lock_t;
172 /* Initializers for lock. */
173 #define LLL_LOCK_INITIALIZER (1)
174 #define LLL_LOCK_INITIALIZER_LOCKED (0)
177 extern int __lll_lock_wait (int *__futex, int val) attribute_hidden;
178 extern int __lll_unlock_wake (int *__futex) attribute_hidden;
179 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
182 /* The states of a lock are:
184 0 - taken by one user
185 <0 - taken by more users */
188 #if defined NOT_IN_libc || defined UP
189 # define lll_trylock(futex) \
190 ({ unsigned char ret; \
191 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1; setne %0" \
192 : "=a" (ret), "=m" (futex) \
193 : "r" (0), "1" (futex), "0" (1) \
198 # define lll_lock(futex) \
199 (void) ({ int ignore1, ignore2; \
200 __asm __volatile (LOCK_INSTR "xaddl %0, %2\n\t" \
203 "1:\tleaq %2, %%rdi\n\t" \
204 "subq $128, %%rsp\n\t" \
205 "callq __lll_lock_wait\n\t" \
206 "addq $128, %%rsp\n\t" \
210 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex) \
211 : "0" (-1), "2" (futex) \
212 : "ax", "cx", "r11", "cc", "memory"); })
215 # define lll_unlock(futex) \
216 (void) ({ int ignore; \
217 __asm __volatile (LOCK_INSTR "incl %0\n\t" \
220 "1:\tleaq %0, %%rdi\n\t" \
221 "subq $128, %%rsp\n\t" \
222 "callq __lll_unlock_wake\n\t" \
223 "addq $128, %%rsp\n\t" \
227 : "=m" (futex), "=&D" (ignore) \
229 : "ax", "cx", "r11", "cc", "memory"); })
231 /* Special versions of the macros for use in libc itself. They avoid
232 the lock prefix when the thread library is not used.
234 XXX In future we might even want to avoid it on UP machines. */
236 # define lll_trylock(futex) \
237 ({ unsigned char ret; \
238 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
241 "0:\tcmpxchgl %2, %1; setne %0" \
242 : "=a" (ret), "=m" (futex) \
243 : "r" (0), "1" (futex), "0" (1) \
248 # define lll_lock(futex) \
249 (void) ({ int ignore1, ignore2; \
250 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
253 "0:\txaddl %0, %2\n\t" \
256 "1:\tleaq %2, %%rdi\n\t" \
257 "subq $128, %%rsp\n\t" \
258 "callq __lll_lock_wait\n\t" \
259 "addq $128, %%rsp\n\t" \
263 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex) \
264 : "0" (-1), "2" (futex) \
265 : "ax", "cx", "r11", "cc", "memory"); })
268 # define lll_unlock(futex) \
269 (void) ({ int ignore; \
270 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
276 "1:\tleaq %0, %%rdi\n\t" \
277 "subq $128, %%rsp\n\t" \
278 "callq __lll_unlock_wake\n\t" \
279 "addq $128, %%rsp\n\t" \
283 : "=m" (futex), "=&D" (ignore) \
285 : "ax", "cx", "r11", "cc", "memory"); })
289 #define lll_islocked(futex) \
293 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
294 wakeup when the clone terminates. The memory location contains the
295 thread ID while the clone is running and is reset to zero
298 The macro parameter must not have any side effect. */
299 #define lll_wait_tid(tid) \
302 register __typeof (tid) _tid asm ("edx") = (tid); \
304 __asm __volatile ("xorq %%r10, %%r10\n\t" \
305 "1:\tmovq %2, %%rax\n\t" \
307 "cmpl $0, (%%rdi)\n\t" \
310 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
312 : "memory", "cc", "r10", "r11", "cx"); \
315 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
317 #define lll_timedwait_tid(tid, abstime) \
322 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
325 __result = __lll_timedwait_tid (&tid, abstime); \
330 /* Conditional variable handling. */
332 extern void __lll_cond_wait (pthread_cond_t *cond) attribute_hidden;
333 extern int __lll_cond_timedwait (pthread_cond_t *cond,
334 const struct timespec *abstime)
336 extern void __lll_cond_wake (pthread_cond_t *cond) attribute_hidden;
337 extern void __lll_cond_broadcast (pthread_cond_t *cond) attribute_hidden;
340 #define lll_cond_wait(cond) \
341 __lll_cond_wait (cond)
342 #define lll_cond_timedwait(cond, abstime) \
343 __lll_cond_timedwait (cond, abstime)
344 #define lll_cond_wake(cond) \
345 __lll_cond_wake (cond)
346 #define lll_cond_broadcast(cond) \
347 __lll_cond_broadcast (cond)
350 #endif /* lowlevellock.h */