]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/i386/lowlevellock.h
38fbc2556f2f5a3fdf93953c6ed01b383f2a2433
1 /* Copyright (C) 2002-2018 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 /* <tcb-offsets.h> is generated from tcb-offsets.sym to define offsets
30 and sizes of types in <tls.h> as well as <pthread.h> which includes
31 <lowlevellock.h> via nptl/descr.h. Don't include <tcb-offsets.h>
32 when generating <tcb-offsets.h> to avoid circular dependency which
33 may lead to build hang on a many-core machine. */
34 # ifndef GEN_AS_CONST_HEADERS
35 # include <tcb-offsets.h>
40 # define LOCK_INSTR /* nothing */
42 # define LOCK_INSTR "lock;"
55 #include <lowlevellock-futex.h>
57 /* XXX Remove when no assembler code uses futexes anymore. */
58 #define SYS_futex __NR_futex
62 /* Initializer for compatibility lock. */
63 #define LLL_LOCK_INITIALIZER (0)
64 #define LLL_LOCK_INITIALIZER_LOCKED (1)
65 #define LLL_LOCK_INITIALIZER_WAITERS (2)
68 /* NB: in the lll_trylock macro we simply return the value in %eax
69 after the cmpxchg instruction. In case the operation succeded this
70 value is zero. In case the operation failed, the cmpxchg instruction
71 has loaded the current value of the memory work which is guaranteed
73 #if !IS_IN (libc) || defined UP
74 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
76 # define __lll_trylock_asm "cmpl $0, %%gs:%P5\n\t" \
82 #define lll_trylock(futex) \
84 __asm __volatile (__lll_trylock_asm \
85 : "=a" (ret), "=m" (futex) \
86 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
87 "0" (LLL_LOCK_INITIALIZER), \
88 "i" (MULTIPLE_THREADS_OFFSET) \
93 #define lll_cond_trylock(futex) \
95 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
96 : "=a" (ret), "=m" (futex) \
97 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
98 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
102 #if !IS_IN (libc) || defined UP
103 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %1, %2\n\t"
105 # define __lll_lock_asm_start "cmpl $0, %%gs:%P6\n\t" \
108 "0:\tcmpxchgl %1, %2\n\t"
111 #define lll_lock(futex, private) \
113 ({ int ignore1, ignore2; \
114 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
115 __asm __volatile (__lll_lock_asm_start \
117 "1:\tleal %2, %%ecx\n" \
118 "2:\tcall __lll_lock_wait_private\n" \
120 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
121 : "0" (0), "1" (1), "m" (futex), \
122 "i" (MULTIPLE_THREADS_OFFSET) \
127 __asm __volatile (__lll_lock_asm_start \
129 "1:\tleal %2, %%edx\n" \
130 "0:\tmovl %8, %%ecx\n" \
131 "2:\tcall __lll_lock_wait\n" \
133 : "=a" (ignore1), "=c" (ignore2), \
134 "=m" (futex), "=&d" (ignore3) \
135 : "1" (1), "m" (futex), \
136 "i" (MULTIPLE_THREADS_OFFSET), "0" (0), \
137 "g" ((int) (private)) \
143 /* Special version of lll_lock which causes the unlock function to
144 always wakeup waiters. */
145 #define lll_cond_lock(futex, private) \
147 ({ int ignore1, ignore2, ignore3; \
148 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
150 "1:\tleal %2, %%edx\n" \
151 "0:\tmovl %7, %%ecx\n" \
152 "2:\tcall __lll_lock_wait\n" \
154 : "=a" (ignore1), "=c" (ignore2), "=m" (futex), \
156 : "0" (0), "1" (2), "m" (futex), "g" ((int) (private))\
161 #define lll_timedlock(futex, timeout, private) \
162 ({ int result, ignore1, ignore2, ignore3; \
163 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
165 "1:\tleal %3, %%ecx\n" \
166 "0:\tmovl %8, %%edx\n" \
167 "2:\tcall __lll_timedlock_wait\n" \
169 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
170 "=m" (futex), "=S" (ignore3) \
171 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
172 "4" ((int) (private)) \
176 extern int __lll_timedlock_elision (int *futex
, short *adapt_count
,
177 const struct timespec
*timeout
,
178 int private) attribute_hidden
;
180 #define lll_timedlock_elision(futex, adapt_count, timeout, private) \
181 __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
183 #if !IS_IN (libc) || defined UP
184 # define __lll_unlock_asm LOCK_INSTR "subl $1, %0\n\t"
186 # define __lll_unlock_asm "cmpl $0, %%gs:%P3\n\t" \
192 #define lll_unlock(futex, private) \
195 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
196 __asm __volatile (__lll_unlock_asm \
198 "1:\tleal %0, %%eax\n" \
199 "2:\tcall __lll_unlock_wake_private\n" \
201 : "=m" (futex), "=&a" (ignore) \
202 : "m" (futex), "i" (MULTIPLE_THREADS_OFFSET) \
207 __asm __volatile (__lll_unlock_asm \
209 "1:\tleal %0, %%eax\n" \
210 "0:\tmovl %5, %%ecx\n" \
211 "2:\tcall __lll_unlock_wake\n" \
213 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
214 : "i" (MULTIPLE_THREADS_OFFSET), "m" (futex), \
215 "g" ((int) (private)) \
221 #define lll_islocked(futex) \
222 (futex != LLL_LOCK_INITIALIZER)
224 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
225 wake-up when the clone terminates. The memory location contains the
226 thread ID while the clone is running and is reset to zero by the kernel
227 afterwards. The kernel up to version 3.16.3 does not use the private futex
228 operations for futex wake-up when the clone terminates. */
229 #define lll_wait_tid(tid) \
231 __typeof (tid) __tid; \
232 while ((__tid = (tid)) != 0) \
233 lll_futex_wait (&(tid), __tid, LLL_SHARED);\
236 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
237 __attribute__ ((regparm (2))) attribute_hidden
;
239 /* As lll_wait_tid, but with a timeout. If the timeout occurs then return
240 ETIMEDOUT. If ABSTIME is invalid, return EINVAL.
241 XXX Note that this differs from the generic version in that we do the
242 error checking here and not in __lll_timedwait_tid. */
243 #define lll_timedwait_tid(tid, abstime) \
247 __result = __lll_timedwait_tid (&(tid), (abstime)); \
251 extern int __lll_lock_elision (int *futex
, short *adapt_count
, int private)
254 extern int __lll_unlock_elision(int *lock
, int private)
257 extern int __lll_trylock_elision(int *lock
, short *adapt_count
)
260 #define lll_lock_elision(futex, adapt_count, private) \
261 __lll_lock_elision (&(futex), &(adapt_count), private)
262 #define lll_unlock_elision(futex, adapt_count, private) \
263 __lll_unlock_elision (&(futex), private)
264 #define lll_trylock_elision(futex, adapt_count) \
265 __lll_trylock_elision(&(futex), &(adapt_count))
267 #endif /* !__ASSEMBLER__ */
269 #endif /* lowlevellock.h */