1 /* Copyright (C) 2002-2015 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, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
22 #include <stap-probe.h>
26 # include <sys/param.h>
27 # include <bits/pthreadtypes.h>
28 # include <kernel-features.h>
29 # include <tcb-offsets.h>
33 # define LOCK_INSTR /* nothing */
35 # define LOCK_INSTR "lock;"
48 #include <lowlevellock-futex.h>
50 /* XXX Remove when no assembler code uses futexes anymore. */
55 /* Initializer for compatibility lock. */
56 #define LLL_LOCK_INITIALIZER (0)
57 #define LLL_LOCK_INITIALIZER_LOCKED (1)
58 #define LLL_LOCK_INITIALIZER_WAITERS (2)
62 # define LLL_EBX_LOAD "xchgl %2, %%ebx\n"
63 # define LLL_EBX_REG "D"
66 # define LLL_EBX_REG "b"
69 #ifdef I386_USE_SYSENTER
71 # define LLL_ENTER_KERNEL "call *%%gs:%P6\n\t"
73 # define LLL_ENTER_KERNEL "call *_dl_sysinfo\n\t"
76 # define LLL_ENTER_KERNEL "int $0x80\n\t"
79 /* Delay in spinlock loop. */
80 #define BUSY_WAIT_NOP asm ("rep; nop")
83 /* NB: in the lll_trylock macro we simply return the value in %eax
84 after the cmpxchg instruction. In case the operation succeded this
85 value is zero. In case the operation failed, the cmpxchg instruction
86 has loaded the current value of the memory work which is guaranteed
88 #if !IS_IN (libc) || defined UP
89 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
91 # define __lll_trylock_asm "cmpl $0, %%gs:%P5\n\t" \
97 #define lll_trylock(futex) \
99 __asm __volatile (__lll_trylock_asm \
100 : "=a" (ret), "=m" (futex) \
101 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
102 "0" (LLL_LOCK_INITIALIZER), \
103 "i" (MULTIPLE_THREADS_OFFSET) \
108 #define lll_cond_trylock(futex) \
110 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
111 : "=a" (ret), "=m" (futex) \
112 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
113 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
117 #if !IS_IN (libc) || defined UP
118 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %1, %2\n\t"
120 # define __lll_lock_asm_start "cmpl $0, %%gs:%P6\n\t" \
123 "0:\tcmpxchgl %1, %2\n\t"
126 #define lll_lock(futex, private) \
128 ({ int ignore1, ignore2; \
129 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
130 __asm __volatile (__lll_lock_asm_start \
132 "1:\tleal %2, %%ecx\n" \
133 "2:\tcall __lll_lock_wait_private\n" \
135 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
136 : "0" (0), "1" (1), "m" (futex), \
137 "i" (MULTIPLE_THREADS_OFFSET) \
142 __asm __volatile (__lll_lock_asm_start \
144 "1:\tleal %2, %%edx\n" \
145 "0:\tmovl %8, %%ecx\n" \
146 "2:\tcall __lll_lock_wait\n" \
148 : "=a" (ignore1), "=c" (ignore2), \
149 "=m" (futex), "=&d" (ignore3) \
150 : "1" (1), "m" (futex), \
151 "i" (MULTIPLE_THREADS_OFFSET), "0" (0), \
152 "g" ((int) (private)) \
157 #define lll_robust_lock(futex, id, private) \
158 ({ int result, ignore1, ignore2; \
159 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
161 "1:\tleal %2, %%edx\n" \
162 "0:\tmovl %7, %%ecx\n" \
163 "2:\tcall __lll_robust_lock_wait\n" \
165 : "=a" (result), "=c" (ignore1), "=m" (futex), \
167 : "0" (0), "1" (id), "m" (futex), "g" ((int) (private))\
172 /* Special version of lll_lock which causes the unlock function to
173 always wakeup waiters. */
174 #define lll_cond_lock(futex, private) \
176 ({ int ignore1, ignore2, ignore3; \
177 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
179 "1:\tleal %2, %%edx\n" \
180 "0:\tmovl %7, %%ecx\n" \
181 "2:\tcall __lll_lock_wait\n" \
183 : "=a" (ignore1), "=c" (ignore2), "=m" (futex), \
185 : "0" (0), "1" (2), "m" (futex), "g" ((int) (private))\
190 #define lll_robust_cond_lock(futex, id, private) \
191 ({ int result, ignore1, ignore2; \
192 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
194 "1:\tleal %2, %%edx\n" \
195 "0:\tmovl %7, %%ecx\n" \
196 "2:\tcall __lll_robust_lock_wait\n" \
198 : "=a" (result), "=c" (ignore1), "=m" (futex), \
200 : "0" (0), "1" (id | FUTEX_WAITERS), "m" (futex), \
201 "g" ((int) (private)) \
206 #define lll_timedlock(futex, timeout, private) \
207 ({ int result, ignore1, ignore2, ignore3; \
208 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
210 "1:\tleal %3, %%ecx\n" \
211 "0:\tmovl %8, %%edx\n" \
212 "2:\tcall __lll_timedlock_wait\n" \
214 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
215 "=m" (futex), "=S" (ignore3) \
216 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
217 "4" ((int) (private)) \
221 extern int __lll_timedlock_elision (int *futex
, short *adapt_count
,
222 const struct timespec
*timeout
,
223 int private) attribute_hidden
;
225 #define lll_timedlock_elision(futex, adapt_count, timeout, private) \
226 __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
228 #define lll_robust_timedlock(futex, timeout, id, private) \
229 ({ int result, ignore1, ignore2, ignore3; \
230 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
232 "1:\tleal %3, %%ecx\n" \
233 "0:\tmovl %8, %%edx\n" \
234 "2:\tcall __lll_robust_timedlock_wait\n" \
236 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
237 "=m" (futex), "=S" (ignore3) \
238 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
239 "4" ((int) (private)) \
243 #if !IS_IN (libc) || defined UP
244 # define __lll_unlock_asm LOCK_INSTR "subl $1, %0\n\t"
246 # define __lll_unlock_asm "cmpl $0, %%gs:%P3\n\t" \
252 #define lll_unlock(futex, private) \
255 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
256 __asm __volatile (__lll_unlock_asm \
258 "1:\tleal %0, %%eax\n" \
259 "2:\tcall __lll_unlock_wake_private\n" \
261 : "=m" (futex), "=&a" (ignore) \
262 : "m" (futex), "i" (MULTIPLE_THREADS_OFFSET) \
267 __asm __volatile (__lll_unlock_asm \
269 "1:\tleal %0, %%eax\n" \
270 "0:\tmovl %5, %%ecx\n" \
271 "2:\tcall __lll_unlock_wake\n" \
273 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
274 : "i" (MULTIPLE_THREADS_OFFSET), "m" (futex), \
275 "g" ((int) (private)) \
280 #define lll_robust_unlock(futex, private) \
282 ({ int ignore, ignore2; \
283 __asm __volatile (LOCK_INSTR "andl %3, %0\n\t" \
285 "1:\tleal %0, %%eax\n" \
286 "0:\tmovl %5, %%ecx\n" \
287 "2:\tcall __lll_unlock_wake\n" \
289 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
290 : "i" (FUTEX_WAITERS), "m" (futex), \
291 "g" ((int) (private)) \
296 #define lll_islocked(futex) \
297 (futex != LLL_LOCK_INITIALIZER)
299 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
300 wake-up when the clone terminates. The memory location contains the
301 thread ID while the clone is running and is reset to zero by the kernel
302 afterwards. The kernel up to version 3.16.3 does not use the private futex
303 operations for futex wake-up when the clone terminates. */
304 #define lll_wait_tid(tid) \
306 __typeof (tid) __tid; \
307 while ((__tid = (tid)) != 0) \
308 lll_futex_wait (&(tid), __tid, LLL_SHARED);\
311 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
312 __attribute__ ((regparm (2))) attribute_hidden
;
314 /* As lll_wait_tid, but with a timeout. If the timeout occurs then return
315 ETIMEDOUT. If ABSTIME is invalid, return EINVAL.
316 XXX Note that this differs from the generic version in that we do the
317 error checking here and not in __lll_timedwait_tid. */
318 #define lll_timedwait_tid(tid, abstime) \
323 if ((abstime)->tv_nsec < 0 || (abstime)->tv_nsec >= 1000000000) \
326 __result = __lll_timedwait_tid (&(tid), (abstime)); \
331 extern int __lll_lock_elision (int *futex
, short *adapt_count
, int private)
334 extern int __lll_unlock_elision(int *lock
, int private)
337 extern int __lll_trylock_elision(int *lock
, short *adapt_count
)
340 #define lll_lock_elision(futex, adapt_count, private) \
341 __lll_lock_elision (&(futex), &(adapt_count), private)
342 #define lll_unlock_elision(futex, private) \
343 __lll_unlock_elision (&(futex), private)
344 #define lll_trylock_elision(futex, adapt_count) \
345 __lll_trylock_elision(&(futex), &(adapt_count))
347 #endif /* !__ASSEMBLER__ */
349 #endif /* lowlevellock.h */