Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / i386 / lowlevellock.h
blob1132124308c4916e11409ea249e798e226a845f0
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>
24 #ifndef __ASSEMBLER__
25 # include <time.h>
26 # include <sys/param.h>
27 # include <bits/pthreadtypes.h>
28 # include <kernel-features.h>
29 # include <tcb-offsets.h>
31 # ifndef LOCK_INSTR
32 # ifdef UP
33 # define LOCK_INSTR /* nothing */
34 # else
35 # define LOCK_INSTR "lock;"
36 # endif
37 # endif
38 #else
39 # ifndef LOCK
40 # ifdef UP
41 # define LOCK
42 # else
43 # define LOCK lock
44 # endif
45 # endif
46 #endif
48 #include <lowlevellock-futex.h>
50 /* XXX Remove when no assembler code uses futexes anymore. */
51 #define SYS_futex 240
53 #ifndef __ASSEMBLER__
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)
61 #ifdef PIC
62 # define LLL_EBX_LOAD "xchgl %2, %%ebx\n"
63 # define LLL_EBX_REG "D"
64 #else
65 # define LLL_EBX_LOAD
66 # define LLL_EBX_REG "b"
67 #endif
69 #ifdef I386_USE_SYSENTER
70 # ifdef SHARED
71 # define LLL_ENTER_KERNEL "call *%%gs:%P6\n\t"
72 # else
73 # define LLL_ENTER_KERNEL "call *_dl_sysinfo\n\t"
74 # endif
75 #else
76 # define LLL_ENTER_KERNEL "int $0x80\n\t"
77 #endif
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
87 to be nonzero. */
88 #if !IS_IN (libc) || defined UP
89 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
90 #else
91 # define __lll_trylock_asm "cmpl $0, %%gs:%P5\n\t" \
92 "je 0f\n\t" \
93 "lock\n" \
94 "0:\tcmpxchgl %2, %1"
95 #endif
97 #define lll_trylock(futex) \
98 ({ int ret; \
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) \
104 : "memory"); \
105 ret; })
108 #define lll_cond_trylock(futex) \
109 ({ int ret; \
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) \
114 : "memory"); \
115 ret; })
117 #if !IS_IN (libc) || defined UP
118 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %1, %2\n\t"
119 #else
120 # define __lll_lock_asm_start "cmpl $0, %%gs:%P6\n\t" \
121 "je 0f\n\t" \
122 "lock\n" \
123 "0:\tcmpxchgl %1, %2\n\t"
124 #endif
126 #define lll_lock(futex, private) \
127 (void) \
128 ({ int ignore1, ignore2; \
129 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
130 __asm __volatile (__lll_lock_asm_start \
131 "jz 18f\n\t" \
132 "1:\tleal %2, %%ecx\n" \
133 "2:\tcall __lll_lock_wait_private\n" \
134 "18:" \
135 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
136 : "0" (0), "1" (1), "m" (futex), \
137 "i" (MULTIPLE_THREADS_OFFSET) \
138 : "memory"); \
139 else \
141 int ignore3; \
142 __asm __volatile (__lll_lock_asm_start \
143 "jz 18f\n\t" \
144 "1:\tleal %2, %%edx\n" \
145 "0:\tmovl %8, %%ecx\n" \
146 "2:\tcall __lll_lock_wait\n" \
147 "18:" \
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)) \
153 : "memory"); \
157 #define lll_robust_lock(futex, id, private) \
158 ({ int result, ignore1, ignore2; \
159 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
160 "jz 18f\n\t" \
161 "1:\tleal %2, %%edx\n" \
162 "0:\tmovl %7, %%ecx\n" \
163 "2:\tcall __lll_robust_lock_wait\n" \
164 "18:" \
165 : "=a" (result), "=c" (ignore1), "=m" (futex), \
166 "=&d" (ignore2) \
167 : "0" (0), "1" (id), "m" (futex), "g" ((int) (private))\
168 : "memory"); \
169 result; })
172 /* Special version of lll_lock which causes the unlock function to
173 always wakeup waiters. */
174 #define lll_cond_lock(futex, private) \
175 (void) \
176 ({ int ignore1, ignore2, ignore3; \
177 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
178 "jz 18f\n\t" \
179 "1:\tleal %2, %%edx\n" \
180 "0:\tmovl %7, %%ecx\n" \
181 "2:\tcall __lll_lock_wait\n" \
182 "18:" \
183 : "=a" (ignore1), "=c" (ignore2), "=m" (futex), \
184 "=&d" (ignore3) \
185 : "0" (0), "1" (2), "m" (futex), "g" ((int) (private))\
186 : "memory"); \
190 #define lll_robust_cond_lock(futex, id, private) \
191 ({ int result, ignore1, ignore2; \
192 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
193 "jz 18f\n\t" \
194 "1:\tleal %2, %%edx\n" \
195 "0:\tmovl %7, %%ecx\n" \
196 "2:\tcall __lll_robust_lock_wait\n" \
197 "18:" \
198 : "=a" (result), "=c" (ignore1), "=m" (futex), \
199 "=&d" (ignore2) \
200 : "0" (0), "1" (id | FUTEX_WAITERS), "m" (futex), \
201 "g" ((int) (private)) \
202 : "memory"); \
203 result; })
206 #define lll_timedlock(futex, timeout, private) \
207 ({ int result, ignore1, ignore2, ignore3; \
208 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
209 "jz 18f\n\t" \
210 "1:\tleal %3, %%ecx\n" \
211 "0:\tmovl %8, %%edx\n" \
212 "2:\tcall __lll_timedlock_wait\n" \
213 "18:" \
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)) \
218 : "memory"); \
219 result; })
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" \
231 "jz 18f\n\t" \
232 "1:\tleal %3, %%ecx\n" \
233 "0:\tmovl %8, %%edx\n" \
234 "2:\tcall __lll_robust_timedlock_wait\n" \
235 "18:" \
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)) \
240 : "memory"); \
241 result; })
243 #if !IS_IN (libc) || defined UP
244 # define __lll_unlock_asm LOCK_INSTR "subl $1, %0\n\t"
245 #else
246 # define __lll_unlock_asm "cmpl $0, %%gs:%P3\n\t" \
247 "je 0f\n\t" \
248 "lock\n" \
249 "0:\tsubl $1,%0\n\t"
250 #endif
252 #define lll_unlock(futex, private) \
253 (void) \
254 ({ int ignore; \
255 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
256 __asm __volatile (__lll_unlock_asm \
257 "je 18f\n\t" \
258 "1:\tleal %0, %%eax\n" \
259 "2:\tcall __lll_unlock_wake_private\n" \
260 "18:" \
261 : "=m" (futex), "=&a" (ignore) \
262 : "m" (futex), "i" (MULTIPLE_THREADS_OFFSET) \
263 : "memory"); \
264 else \
266 int ignore2; \
267 __asm __volatile (__lll_unlock_asm \
268 "je 18f\n\t" \
269 "1:\tleal %0, %%eax\n" \
270 "0:\tmovl %5, %%ecx\n" \
271 "2:\tcall __lll_unlock_wake\n" \
272 "18:" \
273 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
274 : "i" (MULTIPLE_THREADS_OFFSET), "m" (futex), \
275 "g" ((int) (private)) \
276 : "memory"); \
280 #define lll_robust_unlock(futex, private) \
281 (void) \
282 ({ int ignore, ignore2; \
283 __asm __volatile (LOCK_INSTR "andl %3, %0\n\t" \
284 "je 18f\n\t" \
285 "1:\tleal %0, %%eax\n" \
286 "0:\tmovl %5, %%ecx\n" \
287 "2:\tcall __lll_unlock_wake\n" \
288 "18:" \
289 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
290 : "i" (FUTEX_WAITERS), "m" (futex), \
291 "g" ((int) (private)) \
292 : "memory"); \
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) \
305 do { \
306 __typeof (tid) __tid; \
307 while ((__tid = (tid)) != 0) \
308 lll_futex_wait (&(tid), __tid, LLL_SHARED);\
309 } while (0)
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) \
319 ({ \
320 int __result = 0; \
321 if ((tid) != 0) \
323 if ((abstime)->tv_nsec < 0 || (abstime)->tv_nsec >= 1000000000) \
324 __result = EINVAL; \
325 else \
326 __result = __lll_timedwait_tid (&(tid), (abstime)); \
328 __result; })
331 extern int __lll_lock_elision (int *futex, short *adapt_count, int private)
332 attribute_hidden;
334 extern int __lll_unlock_elision(int *lock, int private)
335 attribute_hidden;
337 extern int __lll_trylock_elision(int *lock, short *adapt_count)
338 attribute_hidden;
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 */