]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/i386/lowlevellock.h
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / lowlevellock.h
1 /* Copyright (C) 2002-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
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/>. */
18
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
21
22 #include <stap-probe.h>
23
24 #ifndef __ASSEMBLER__
25 # include <time.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>
36 # endif
37
38 # ifndef LOCK_INSTR
39 # ifdef UP
40 # define LOCK_INSTR /* nothing */
41 # else
42 # define LOCK_INSTR "lock;"
43 # endif
44 # endif
45 #else
46 # ifndef LOCK
47 # ifdef UP
48 # define LOCK
49 # else
50 # define LOCK lock
51 # endif
52 # endif
53 #endif
54
55 #include <lowlevellock-futex.h>
56
57 /* XXX Remove when no assembler code uses futexes anymore. */
58 #define SYS_futex __NR_futex
59
60 #ifndef __ASSEMBLER__
61
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)
66
67
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
72 to be nonzero. */
73 #if !IS_IN (libc) || defined UP
74 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
75 #else
76 # define __lll_trylock_asm "cmpl $0, %%gs:%P5\n\t" \
77 "je 0f\n\t" \
78 "lock\n" \
79 "0:\tcmpxchgl %2, %1"
80 #endif
81
82 #define lll_trylock(futex) \
83 ({ int ret; \
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) \
89 : "memory"); \
90 ret; })
91
92
93 #define lll_cond_trylock(futex) \
94 ({ int ret; \
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) \
99 : "memory"); \
100 ret; })
101
102 #if !IS_IN (libc) || defined UP
103 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %1, %2\n\t"
104 #else
105 # define __lll_lock_asm_start "cmpl $0, %%gs:%P6\n\t" \
106 "je 0f\n\t" \
107 "lock\n" \
108 "0:\tcmpxchgl %1, %2\n\t"
109 #endif
110
111 #define lll_lock(futex, private) \
112 (void) \
113 ({ int ignore1, ignore2; \
114 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
115 __asm __volatile (__lll_lock_asm_start \
116 "jz 18f\n\t" \
117 "1:\tleal %2, %%ecx\n" \
118 "2:\tcall __lll_lock_wait_private\n" \
119 "18:" \
120 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
121 : "0" (0), "1" (1), "m" (futex), \
122 "i" (MULTIPLE_THREADS_OFFSET) \
123 : "memory"); \
124 else \
125 { \
126 int ignore3; \
127 __asm __volatile (__lll_lock_asm_start \
128 "jz 18f\n\t" \
129 "1:\tleal %2, %%edx\n" \
130 "0:\tmovl %8, %%ecx\n" \
131 "2:\tcall __lll_lock_wait\n" \
132 "18:" \
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)) \
138 : "memory"); \
139 } \
140 })
141
142
143 /* Special version of lll_lock which causes the unlock function to
144 always wakeup waiters. */
145 #define lll_cond_lock(futex, private) \
146 (void) \
147 ({ int ignore1, ignore2, ignore3; \
148 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
149 "jz 18f\n\t" \
150 "1:\tleal %2, %%edx\n" \
151 "0:\tmovl %7, %%ecx\n" \
152 "2:\tcall __lll_lock_wait\n" \
153 "18:" \
154 : "=a" (ignore1), "=c" (ignore2), "=m" (futex), \
155 "=&d" (ignore3) \
156 : "0" (0), "1" (2), "m" (futex), "g" ((int) (private))\
157 : "memory"); \
158 })
159
160
161 #define lll_timedlock(futex, timeout, private) \
162 ({ int result, ignore1, ignore2, ignore3; \
163 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
164 "jz 18f\n\t" \
165 "1:\tleal %3, %%ecx\n" \
166 "0:\tmovl %8, %%edx\n" \
167 "2:\tcall __lll_timedlock_wait\n" \
168 "18:" \
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)) \
173 : "memory"); \
174 result; })
175
176 extern int __lll_timedlock_elision (int *futex, short *adapt_count,
177 const struct timespec *timeout,
178 int private) attribute_hidden;
179
180 #define lll_timedlock_elision(futex, adapt_count, timeout, private) \
181 __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
182
183 #if !IS_IN (libc) || defined UP
184 # define __lll_unlock_asm LOCK_INSTR "subl $1, %0\n\t"
185 #else
186 # define __lll_unlock_asm "cmpl $0, %%gs:%P3\n\t" \
187 "je 0f\n\t" \
188 "lock\n" \
189 "0:\tsubl $1,%0\n\t"
190 #endif
191
192 #define lll_unlock(futex, private) \
193 (void) \
194 ({ int ignore; \
195 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
196 __asm __volatile (__lll_unlock_asm \
197 "je 18f\n\t" \
198 "1:\tleal %0, %%eax\n" \
199 "2:\tcall __lll_unlock_wake_private\n" \
200 "18:" \
201 : "=m" (futex), "=&a" (ignore) \
202 : "m" (futex), "i" (MULTIPLE_THREADS_OFFSET) \
203 : "memory"); \
204 else \
205 { \
206 int ignore2; \
207 __asm __volatile (__lll_unlock_asm \
208 "je 18f\n\t" \
209 "1:\tleal %0, %%eax\n" \
210 "0:\tmovl %5, %%ecx\n" \
211 "2:\tcall __lll_unlock_wake\n" \
212 "18:" \
213 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
214 : "i" (MULTIPLE_THREADS_OFFSET), "m" (futex), \
215 "g" ((int) (private)) \
216 : "memory"); \
217 } \
218 })
219
220
221 #define lll_islocked(futex) \
222 (futex != LLL_LOCK_INITIALIZER)
223
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) \
230 do { \
231 __typeof (tid) __tid; \
232 while ((__tid = (tid)) != 0) \
233 lll_futex_wait (&(tid), __tid, LLL_SHARED);\
234 } while (0)
235
236 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
237 __attribute__ ((regparm (2))) attribute_hidden;
238
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) \
244 ({ \
245 int __result = 0; \
246 if ((tid) != 0) \
247 __result = __lll_timedwait_tid (&(tid), (abstime)); \
248 __result; })
249
250
251 extern int __lll_lock_elision (int *futex, short *adapt_count, int private)
252 attribute_hidden;
253
254 extern int __lll_unlock_elision(int *lock, int private)
255 attribute_hidden;
256
257 extern int __lll_trylock_elision(int *lock, short *adapt_count)
258 attribute_hidden;
259
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))
266
267 #endif /* !__ASSEMBLER__ */
268
269 #endif /* lowlevellock.h */