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)
47 # define LLL_EBX_LOAD "xchgl %2, %%ebx\n"
48 # define LLL_EBX_REG "D"
51 # define LLL_EBX_REG "b"
54 #ifdef I386_USE_SYSENTER
56 # define LLL_ENTER_KERNEL "call *%%gs:%P6\n\t"
58 # define LLL_ENTER_KERNEL "call *_dl_sysinfo\n\t"
61 # define LLL_ENTER_KERNEL "int $0x80\n\t"
64 /* Delay in spinlock loop. */
65 #define BUSY_WAIT_NOP asm ("rep; nop")
68 #define lll_futex_wait(futex, val) \
71 register __typeof (val) _val asm ("edx") = (val); \
72 __asm __volatile (LLL_EBX_LOAD \
76 : "0" (SYS_futex), LLL_EBX_REG (futex), "S" (0), \
77 "c" (FUTEX_WAIT), "d" (_val), \
78 "i" (offsetof (tcbhead_t, sysinfo))); \
82 #define lll_futex_wake(futex, nr) \
85 register __typeof (nr) _nr asm ("edx") = (nr); \
86 __asm __volatile (LLL_EBX_LOAD \
90 : "0" (SYS_futex), LLL_EBX_REG (futex), \
91 "c" (FUTEX_WAKE), "d" (_nr), \
92 "i" (0) /* phony, to align next arg's number */, \
93 "i" (offsetof (tcbhead_t, sysinfo))); \
97 /* Does not preserve %eax and %ecx. */
98 extern int __lll_mutex_lock_wait (int val
, int *__futex
)
99 __attribute ((regparm (2))) attribute_hidden
;
100 /* Does not preserve %eax, %ecx, and %edx. */
101 extern int __lll_mutex_timedlock_wait (int val
, int *__futex
,
102 const struct timespec
*abstime
)
103 __attribute ((regparm (3))) attribute_hidden
;
104 /* Preserves all registers but %eax. */
105 extern int __lll_mutex_unlock_wake (int *__futex
)
106 __attribute ((regparm (1))) attribute_hidden
;
109 /* NB: in the lll_mutex_trylock macro we simply return the value in %eax
110 after the cmpxchg instruction. In case the operation succeded this
111 value is zero. In case the operation failed, the cmpxchg instruction
112 has loaded the current value of the memory work which is guaranteed
114 #define lll_mutex_trylock(futex) \
116 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
117 : "=a" (ret), "=m" (futex) \
118 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "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; \
136 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
137 "jnz _L_mutex_lock_%=\n\t" \
138 ".subsection 1\n\t" \
139 ".type _L_mutex_lock_%=,@function\n" \
140 "_L_mutex_lock_%=:\n\t" \
141 "leal %2, %%ecx\n\t" \
142 "call __lll_mutex_lock_wait\n\t" \
144 ".size _L_mutex_lock_%=,.-_L_mutex_lock_%=\n" \
147 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
148 : "0" (0), "1" (1), "m" (futex) \
152 /* Special version of lll_mutex_lock which causes the unlock function to
153 always wakeup waiters. */
154 #define lll_mutex_cond_lock(futex) \
155 (void) ({ int ignore1, ignore2; \
156 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
157 "jnz _L_mutex_cond_lock_%=\n\t" \
158 ".subsection 1\n\t" \
159 ".type _L_mutex_cond_lock_%=,@function\n" \
160 "_L_mutex_cond_lock_%=:\n\t" \
161 "leal %2, %%ecx\n\t" \
162 "call __lll_mutex_lock_wait\n\t" \
164 ".size _L_mutex_cond_lock_%=,.-_L_mutex_cond_lock_%=\n" \
167 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
168 : "0" (0), "1" (2), "m" (futex) \
172 #define lll_mutex_timedlock(futex, timeout) \
173 ({ int result, ignore1, ignore2; \
174 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
175 "jnz _L_mutex_timedlock_%=\n\t" \
176 ".subsection 1\n\t" \
177 ".type _L_mutex_timedlock_%=,@function\n" \
178 "_L_mutex_timedlock_%=:\n\t" \
179 "leal %3, %%ecx\n\t" \
180 "movl %7, %%edx\n\t" \
181 "call __lll_mutex_timedlock_wait\n\t" \
183 ".size _L_mutex_timedlock_%=,.-_L_mutex_timedlock_%=\n"\
186 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
188 : "0" (0), "1" (1), "m" (futex), "m" (timeout) \
193 #define lll_mutex_unlock(futex) \
194 (void) ({ int ignore; \
195 __asm __volatile (LOCK_INSTR "subl $1,%0\n\t" \
196 "jne _L_mutex_unlock_%=\n\t" \
197 ".subsection 1\n\t" \
198 ".type _L_mutex_unlock_%=,@function\n" \
199 "_L_mutex_unlock_%=:\n\t" \
200 "leal %0, %%eax\n\t" \
201 "call __lll_mutex_unlock_wake\n\t" \
203 ".size _L_mutex_unlock_%=,.-_L_mutex_unlock_%=\n" \
206 : "=m" (futex), "=&a" (ignore) \
211 #define lll_mutex_islocked(futex) \
215 /* We have a separate internal lock implementation which is not tied
216 to binary compatibility. */
218 /* Type for lock object. */
219 typedef int lll_lock_t
;
221 /* Initializers for lock. */
222 #define LLL_LOCK_INITIALIZER (0)
223 #define LLL_LOCK_INITIALIZER_LOCKED (1)
226 extern int __lll_lock_wait (int val
, int *__futex
)
227 __attribute ((regparm (2))) attribute_hidden
;
228 extern int __lll_unlock_wake (int *__futex
)
229 __attribute ((regparm (1))) attribute_hidden
;
230 extern int lll_unlock_wake_cb (int *__futex
) attribute_hidden
;
233 /* The states of a lock are:
235 1 - taken by one user
236 2 - taken by more users */
239 #if defined NOT_IN_libc || defined UP
240 # define lll_trylock(futex) lll_mutex_trylock (futex)
241 # define lll_lock(futex) lll_mutex_lock (futex)
242 # define lll_unlock(futex) lll_mutex_unlock (futex)
244 /* Special versions of the macros for use in libc itself. They avoid
245 the lock prefix when the thread library is not used.
247 XXX In future we might even want to avoid it on UP machines. */
250 # define lll_trylock(futex) \
251 ({ unsigned char ret; \
252 __asm __volatile ("cmpl $0, %%gs:%P5\n\t" \
255 "0:\tcmpxchgl %2, %1; setne %0" \
256 : "=a" (ret), "=m" (futex) \
257 : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
258 "0" (LLL_MUTEX_LOCK_INITIALIZER), \
259 "i" (offsetof (tcbhead_t, multiple_threads)) \
264 # define lll_lock(futex) \
265 (void) ({ int ignore1, ignore2; \
266 __asm __volatile ("cmpl $0, %%gs:%P6\n\t" \
269 "0:\tcmpxchgl %1, %2\n\t" \
270 "jnz _L_mutex_lock_%=\n\t" \
271 ".subsection 1\n\t" \
272 ".type _L_mutex_lock_%=,@function\n" \
273 "_L_mutex_lock_%=:\n\t" \
274 "leal %2, %%ecx\n\t" \
275 "call __lll_mutex_lock_wait\n\t" \
277 ".size _L_mutex_lock_%=,.-_L_mutex_lock_%=\n" \
280 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
281 : "0" (0), "1" (1), "m" (futex), \
282 "i" (offsetof (tcbhead_t, multiple_threads)) \
286 # define lll_unlock(futex) \
287 (void) ({ int ignore; \
288 __asm __volatile ("cmpl $0, %%gs:%P3\n\t" \
291 "0:\tsubl $1,%0\n\t" \
292 "jne _L_mutex_unlock_%=\n\t" \
293 ".subsection 1\n\t" \
294 ".type _L_mutex_unlock_%=,@function\n" \
295 "_L_mutex_unlock_%=:\n\t" \
296 "leal %0, %%eax\n\t" \
297 "call __lll_mutex_unlock_wake\n\t" \
299 ".size _L_mutex_unlock_%=,.-_L_mutex_unlock_%=\n" \
302 : "=m" (futex), "=&a" (ignore) \
304 "i" (offsetof (tcbhead_t, multiple_threads)) \
309 #define lll_islocked(futex) \
310 (futex != LLL_LOCK_INITIALIZER)
313 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
314 wakeup when the clone terminates. The memory location contains the
315 thread ID while the clone is running and is reset to zero
318 The macro parameter must not have any side effect. */
319 #define lll_wait_tid(tid) \
322 register __typeof (tid) _tid asm ("edx") = (tid); \
324 __asm __volatile (LLL_EBX_LOAD \
325 "1:\tmovl %1, %%eax\n\t" \
327 "cmpl $0, (%%ebx)\n\t" \
331 : "i" (SYS_futex), LLL_EBX_REG (&tid), "S" (0), \
332 "c" (FUTEX_WAIT), "d" (_tid), \
333 "i" (offsetof (tcbhead_t, sysinfo))); \
336 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
337 __attribute__ ((regparm (2))) attribute_hidden
;
338 #define lll_timedwait_tid(tid, abstime) \
343 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
346 __result = __lll_timedwait_tid (&tid, abstime); \
351 /* Conditional variable handling. */
353 extern void __lll_cond_wait (pthread_cond_t
*cond
)
354 __attribute ((regparm (1))) attribute_hidden
;
355 extern int __lll_cond_timedwait (pthread_cond_t
*cond
,
356 const struct timespec
*abstime
)
357 __attribute ((regparm (2))) attribute_hidden
;
358 extern void __lll_cond_wake (pthread_cond_t
*cond
)
359 __attribute ((regparm (1))) attribute_hidden
;
360 extern void __lll_cond_broadcast (pthread_cond_t
*cond
)
361 __attribute ((regparm (1))) attribute_hidden
;
364 #define lll_cond_wait(cond) \
365 __lll_cond_wait (cond)
366 #define lll_cond_timedwait(cond, abstime) \
367 __lll_cond_timedwait (cond, abstime)
368 #define lll_cond_wake(cond) \
369 __lll_cond_wake (cond)
370 #define lll_cond_broadcast(cond) \
371 __lll_cond_broadcast (cond)
374 #endif /* lowlevellock.h */