]>
sourceware.org Git - glibc.git/blob - nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.h
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)
42 #define LLL_MUTEX_LOCK_INITIALIZER_LOCKED (1)
45 #define lll_futex_wait(futex, val) \
48 register __typeof (val) _val asm ("edx") = (val); \
49 __asm __volatile ("xorq %%r10, %%r10\n\t" \
52 : "0" (SYS_futex), "D" (&futex), "S" (FUTEX_WAIT), \
54 : "memory", "cc", "r10", "r11", "cx"); \
58 #define lll_futex_wake(futex, nr) \
61 register __typeof (nr) _nr asm ("edx") = (nr); \
62 __asm __volatile ("syscall" \
64 : "0" (SYS_futex), "D" (&futex), "S" (FUTEX_WAKE), \
66 : "memory", "cc", "r10", "r11", "cx"); \
70 /* Does not preserve %eax and %ecx. */
71 extern int __lll_mutex_lock_wait (int *__futex
, int __val
) attribute_hidden
;
72 /* Does not preserver %eax, %ecx, and %edx. */
73 extern int __lll_mutex_timedlock_wait (int *__futex
, int __val
,
74 const struct timespec
*__abstime
)
76 /* Preserves all registers but %eax. */
77 extern int __lll_mutex_unlock_wait (int *__futex
) attribute_hidden
;
80 #define lll_mutex_trylock(futex) \
81 ({ unsigned char ret; \
82 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1; setne %0" \
83 : "=a" (ret), "=m" (futex) \
84 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
85 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
90 #define lll_mutex_lock(futex) \
91 (void) ({ int ignore1, ignore2, ignore3; \
92 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
95 "1:\tleaq %2, %%rdi\n\t" \
96 "subq $128, %%rsp\n\t" \
97 "callq __lll_mutex_lock_wait\n\t" \
98 "addq $128, %%rsp\n\t" \
102 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
104 : "0" (1), "m" (futex), "3" (0) \
105 : "cx", "r11", "cc", "memory"); })
108 #define lll_mutex_cond_lock(futex) \
109 (void) ({ int ignore1, ignore2, ignore3; \
110 __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
113 "1:\tleaq %2, %%rdi\n\t" \
114 "subq $128, %%rsp\n\t" \
115 "callq __lll_mutex_lock_wait\n\t" \
116 "addq $128, %%rsp\n\t" \
120 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
122 : "0" (2), "m" (futex), "3" (0) \
123 : "cx", "r11", "cc", "memory"); })
126 #define lll_mutex_timedlock(futex, timeout) \
127 ({ int result, ignore1, ignore2, ignore3; \
128 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %4\n\t" \
131 "1:\tleaq %4, %%rdi\n\t" \
132 "movq %8, %%rdx\n\t" \
133 "subq $128, %%rsp\n\t" \
134 "callq __lll_mutex_timedlock_wait\n\t" \
135 "addq $128, %%rsp\n\t" \
139 : "=a" (result), "=&D" (ignore1), "=S" (ignore2), \
140 "=&d" (ignore3), "=m" (futex) \
141 : "0" (0), "2" (1), "m" (futex), "m" (timeout) \
142 : "memory", "cx", "cc", "r10", "r11"); \
146 #define lll_mutex_unlock(futex) \
147 (void) ({ int ignore; \
148 __asm __volatile (LOCK_INSTR "decl %0\n\t" \
151 "1:\tleaq %0, %%rdi\n\t" \
152 "subq $128, %%rsp\n\t" \
153 "callq __lll_mutex_unlock_wake\n\t" \
154 "addq $128, %%rsp\n\t" \
158 : "=m" (futex), "=&D" (ignore) \
160 : "ax", "cx", "r11", "cc", "memory"); })
163 #define lll_mutex_islocked(futex) \
164 (futex != LLL_MUTEX_LOCK_INITIALIZER)
167 /* We have a separate internal lock implementation which is not tied
168 to binary compatibility. */
170 /* Type for lock object. */
171 typedef int lll_lock_t
;
173 /* Initializers for lock. */
174 #define LLL_LOCK_INITIALIZER (0)
175 #define LLL_LOCK_INITIALIZER_LOCKED (1)
178 extern int lll_unlock_wake_cb (int *__futex
) attribute_hidden
;
181 /* The states of a lock are:
183 1 - taken by one user
184 2 - taken by more users */
187 #if defined NOT_IN_libc || defined UP
188 # define lll_trylock(futex) lll_mutex_trylock (futex)
189 # define lll_lock(futex) lll_mutex_lock (futex)
190 # define lll_unlock(futex) lll_mutex_unlock (futex)
192 /* Special versions of the macros for use in libc itself. They avoid
193 the lock prefix when the thread library is not used.
195 XXX In future we might even want to avoid it on UP machines. */
197 # define lll_trylock(futex) \
198 ({ unsigned char ret; \
199 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
202 "0:\tcmpxchgl %2, %1; setne %0" \
203 : "=a" (ret), "=m" (futex) \
204 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
205 "0" (LLL_MUTEX_LOCK_INITIALIZER) \
210 # define lll_lock(futex) \
211 (void) ({ int ignore1, ignore2, ignore3; \
212 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
215 "0:\tcmpxchgl %0, %2\n\t" \
218 "1:\tleaq %2, %%rdi\n\t" \
219 "subq $128, %%rsp\n\t" \
220 "callq __lll_mutex_lock_wait\n\t" \
221 "addq $128, %%rsp\n\t" \
225 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
227 : "0" (1), "m" (futex), "3" (0) \
228 : "cx", "r11", "cc", "memory"); })
231 # define lll_unlock(futex) \
232 (void) ({ int ignore; \
233 __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
239 "1:\tleaq %0, %%rdi\n\t" \
240 "subq $128, %%rsp\n\t" \
241 "callq __lll_mutex_unlock_wake\n\t" \
242 "addq $128, %%rsp\n\t" \
246 : "=m" (futex), "=&D" (ignore) \
248 : "ax", "cx", "r11", "cc", "memory"); })
252 #define lll_islocked(futex) \
253 (futex != LLL_MUTEX_LOCK_INITIALIZER)
256 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
257 wakeup when the clone terminates. The memory location contains the
258 thread ID while the clone is running and is reset to zero
261 The macro parameter must not have any side effect. */
262 #define lll_wait_tid(tid) \
265 register __typeof (tid) _tid asm ("edx") = (tid); \
267 __asm __volatile ("xorq %%r10, %%r10\n\t" \
268 "1:\tmovq %2, %%rax\n\t" \
270 "cmpl $0, (%%rdi)\n\t" \
273 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
275 : "memory", "cc", "r10", "r11", "cx"); \
278 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
280 #define lll_timedwait_tid(tid, abstime) \
285 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
288 __result = __lll_timedwait_tid (&tid, abstime); \
293 /* Conditional variable handling. */
295 extern void __lll_cond_wait (pthread_cond_t
*cond
) attribute_hidden
;
296 extern int __lll_cond_timedwait (pthread_cond_t
*cond
,
297 const struct timespec
*abstime
)
299 extern void __lll_cond_wake (pthread_cond_t
*cond
) attribute_hidden
;
300 extern void __lll_cond_broadcast (pthread_cond_t
*cond
) attribute_hidden
;
303 #define lll_cond_wait(cond) \
304 __lll_cond_wait (cond)
305 #define lll_cond_timedwait(cond, abstime) \
306 __lll_cond_timedwait (cond, abstime)
307 #define lll_cond_wake(cond) \
308 __lll_cond_wake (cond)
309 #define lll_cond_broadcast(cond) \
310 __lll_cond_broadcast (cond)
313 #endif /* lowlevellock.h */
This page took 0.052448 seconds and 5 git commands to generate.