Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / x86_64 / lowlevellock.h
blob573b48c4fece143ba7d28eb23043fd23f5443648
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 __NR_futex
53 #ifndef __ASSEMBLER__
55 /* Initializer for lock. */
56 #define LLL_LOCK_INITIALIZER (0)
57 #define LLL_LOCK_INITIALIZER_LOCKED (1)
58 #define LLL_LOCK_INITIALIZER_WAITERS (2)
60 /* Delay in spinlock loop. */
61 #define BUSY_WAIT_NOP asm ("rep; nop")
64 /* NB: in the lll_trylock macro we simply return the value in %eax
65 after the cmpxchg instruction. In case the operation succeded this
66 value is zero. In case the operation failed, the cmpxchg instruction
67 has loaded the current value of the memory work which is guaranteed
68 to be nonzero. */
69 #if !IS_IN (libc) || defined UP
70 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
71 #else
72 # define __lll_trylock_asm "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
73 "je 0f\n\t" \
74 "lock; cmpxchgl %2, %1\n\t" \
75 "jmp 1f\n\t" \
76 "0:\tcmpxchgl %2, %1\n\t" \
77 "1:"
78 #endif
80 #define lll_trylock(futex) \
81 ({ int ret; \
82 __asm __volatile (__lll_trylock_asm \
83 : "=a" (ret), "=m" (futex) \
84 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
85 "0" (LLL_LOCK_INITIALIZER) \
86 : "memory"); \
87 ret; })
89 #define lll_cond_trylock(futex) \
90 ({ int ret; \
91 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
92 : "=a" (ret), "=m" (futex) \
93 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
94 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
95 : "memory"); \
96 ret; })
98 #if !IS_IN (libc) || defined UP
99 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %4, %2\n\t" \
100 "jz 24f\n\t"
101 #else
102 # define __lll_lock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
103 "je 0f\n\t" \
104 "lock; cmpxchgl %4, %2\n\t" \
105 "jnz 1f\n\t" \
106 "jmp 24f\n" \
107 "0:\tcmpxchgl %4, %2\n\t" \
108 "jz 24f\n\t"
109 #endif
111 #define lll_lock(futex, private) \
112 (void) \
113 ({ int ignore1, ignore2, ignore3; \
114 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
115 __asm __volatile (__lll_lock_asm_start \
116 "1:\tlea %2, %%" RDI_LP "\n" \
117 "2:\tsub $128, %%" RSP_LP "\n" \
118 ".cfi_adjust_cfa_offset 128\n" \
119 "3:\tcallq __lll_lock_wait_private\n" \
120 "4:\tadd $128, %%" RSP_LP "\n" \
121 ".cfi_adjust_cfa_offset -128\n" \
122 "24:" \
123 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
124 "=a" (ignore3) \
125 : "0" (1), "m" (futex), "3" (0) \
126 : "cx", "r11", "cc", "memory"); \
127 else \
128 __asm __volatile (__lll_lock_asm_start \
129 "1:\tlea %2, %%" RDI_LP "\n" \
130 "2:\tsub $128, %%" RSP_LP "\n" \
131 ".cfi_adjust_cfa_offset 128\n" \
132 "3:\tcallq __lll_lock_wait\n" \
133 "4:\tadd $128, %%" RSP_LP "\n" \
134 ".cfi_adjust_cfa_offset -128\n" \
135 "24:" \
136 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
137 "=a" (ignore3) \
138 : "1" (1), "m" (futex), "3" (0), "0" (private) \
139 : "cx", "r11", "cc", "memory"); \
140 }) \
142 #define lll_robust_lock(futex, id, private) \
143 ({ int result, ignore1, ignore2; \
144 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
145 "jz 24f\n" \
146 "1:\tlea %2, %%" RDI_LP "\n" \
147 "2:\tsub $128, %%" RSP_LP "\n" \
148 ".cfi_adjust_cfa_offset 128\n" \
149 "3:\tcallq __lll_robust_lock_wait\n" \
150 "4:\tadd $128, %%" RSP_LP "\n" \
151 ".cfi_adjust_cfa_offset -128\n" \
152 "24:" \
153 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
154 "=a" (result) \
155 : "1" (id), "m" (futex), "3" (0), "0" (private) \
156 : "cx", "r11", "cc", "memory"); \
157 result; })
159 #define lll_cond_lock(futex, private) \
160 (void) \
161 ({ int ignore1, ignore2, ignore3; \
162 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
163 "jz 24f\n" \
164 "1:\tlea %2, %%" RDI_LP "\n" \
165 "2:\tsub $128, %%" RSP_LP "\n" \
166 ".cfi_adjust_cfa_offset 128\n" \
167 "3:\tcallq __lll_lock_wait\n" \
168 "4:\tadd $128, %%" RSP_LP "\n" \
169 ".cfi_adjust_cfa_offset -128\n" \
170 "24:" \
171 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
172 "=a" (ignore3) \
173 : "1" (2), "m" (futex), "3" (0), "0" (private) \
174 : "cx", "r11", "cc", "memory"); \
177 #define lll_robust_cond_lock(futex, id, private) \
178 ({ int result, ignore1, ignore2; \
179 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
180 "jz 24f\n" \
181 "1:\tlea %2, %%" RDI_LP "\n" \
182 "2:\tsub $128, %%" RSP_LP "\n" \
183 ".cfi_adjust_cfa_offset 128\n" \
184 "3:\tcallq __lll_robust_lock_wait\n" \
185 "4:\tadd $128, %%" RSP_LP "\n" \
186 ".cfi_adjust_cfa_offset -128\n" \
187 "24:" \
188 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
189 "=a" (result) \
190 : "1" (id | FUTEX_WAITERS), "m" (futex), "3" (0), \
191 "0" (private) \
192 : "cx", "r11", "cc", "memory"); \
193 result; })
195 #define lll_timedlock(futex, timeout, private) \
196 ({ int result, ignore1, ignore2, ignore3; \
197 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
198 "jz 24f\n" \
199 "1:\tlea %4, %%" RDI_LP "\n" \
200 "0:\tmov %8, %%" RDX_LP "\n" \
201 "2:\tsub $128, %%" RSP_LP "\n" \
202 ".cfi_adjust_cfa_offset 128\n" \
203 "3:\tcallq __lll_timedlock_wait\n" \
204 "4:\tadd $128, %%" RSP_LP "\n" \
205 ".cfi_adjust_cfa_offset -128\n" \
206 "24:" \
207 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
208 "=&d" (ignore3), "=m" (futex) \
209 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
210 "2" (private) \
211 : "memory", "cx", "cc", "r10", "r11"); \
212 result; })
214 extern int __lll_timedlock_elision (int *futex, short *adapt_count,
215 const struct timespec *timeout,
216 int private) attribute_hidden;
218 #define lll_timedlock_elision(futex, adapt_count, timeout, private) \
219 __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
221 #define lll_robust_timedlock(futex, timeout, id, private) \
222 ({ int result, ignore1, ignore2, ignore3; \
223 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
224 "jz 24f\n\t" \
225 "1:\tlea %4, %%" RDI_LP "\n" \
226 "0:\tmov %8, %%" RDX_LP "\n" \
227 "2:\tsub $128, %%" RSP_LP "\n" \
228 ".cfi_adjust_cfa_offset 128\n" \
229 "3:\tcallq __lll_robust_timedlock_wait\n" \
230 "4:\tadd $128, %%" RSP_LP "\n" \
231 ".cfi_adjust_cfa_offset -128\n" \
232 "24:" \
233 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
234 "=&d" (ignore3), "=m" (futex) \
235 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
236 "2" (private) \
237 : "memory", "cx", "cc", "r10", "r11"); \
238 result; })
240 #if !IS_IN (libc) || defined UP
241 # define __lll_unlock_asm_start LOCK_INSTR "decl %0\n\t" \
242 "je 24f\n\t"
243 #else
244 # define __lll_unlock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
245 "je 0f\n\t" \
246 "lock; decl %0\n\t" \
247 "jne 1f\n\t" \
248 "jmp 24f\n\t" \
249 "0:\tdecl %0\n\t" \
250 "je 24f\n\t"
251 #endif
253 #define lll_unlock(futex, private) \
254 (void) \
255 ({ int ignore; \
256 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
257 __asm __volatile (__lll_unlock_asm_start \
258 "1:\tlea %0, %%" RDI_LP "\n" \
259 "2:\tsub $128, %%" RSP_LP "\n" \
260 ".cfi_adjust_cfa_offset 128\n" \
261 "3:\tcallq __lll_unlock_wake_private\n" \
262 "4:\tadd $128, %%" RSP_LP "\n" \
263 ".cfi_adjust_cfa_offset -128\n" \
264 "24:" \
265 : "=m" (futex), "=&D" (ignore) \
266 : "m" (futex) \
267 : "ax", "cx", "r11", "cc", "memory"); \
268 else \
269 __asm __volatile (__lll_unlock_asm_start \
270 "1:\tlea %0, %%" RDI_LP "\n" \
271 "2:\tsub $128, %%" RSP_LP "\n" \
272 ".cfi_adjust_cfa_offset 128\n" \
273 "3:\tcallq __lll_unlock_wake\n" \
274 "4:\tadd $128, %%" RSP_LP "\n" \
275 ".cfi_adjust_cfa_offset -128\n" \
276 "24:" \
277 : "=m" (futex), "=&D" (ignore) \
278 : "m" (futex), "S" (private) \
279 : "ax", "cx", "r11", "cc", "memory"); \
282 #define lll_robust_unlock(futex, private) \
283 do \
285 int ignore; \
286 __asm __volatile (LOCK_INSTR "andl %2, %0\n\t" \
287 "je 24f\n\t" \
288 "1:\tlea %0, %%" RDI_LP "\n" \
289 "2:\tsub $128, %%" RSP_LP "\n" \
290 ".cfi_adjust_cfa_offset 128\n" \
291 "3:\tcallq __lll_unlock_wake\n" \
292 "4:\tadd $128, %%" RSP_LP "\n" \
293 ".cfi_adjust_cfa_offset -128\n" \
294 "24:" \
295 : "=m" (futex), "=&D" (ignore) \
296 : "i" (FUTEX_WAITERS), "m" (futex), \
297 "S" (private) \
298 : "ax", "cx", "r11", "cc", "memory"); \
300 while (0)
302 #define lll_islocked(futex) \
303 (futex != LLL_LOCK_INITIALIZER)
306 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
307 wake-up when the clone terminates. The memory location contains the
308 thread ID while the clone is running and is reset to zero by the kernel
309 afterwards. The kernel up to version 3.16.3 does not use the private futex
310 operations for futex wake-up when the clone terminates. */
311 #define lll_wait_tid(tid) \
312 do { \
313 __typeof (tid) __tid; \
314 while ((__tid = (tid)) != 0) \
315 lll_futex_wait (&(tid), __tid, LLL_SHARED);\
316 } while (0)
318 extern int __lll_timedwait_tid (int *, const struct timespec *)
319 attribute_hidden;
321 /* As lll_wait_tid, but with a timeout. If the timeout occurs then return
322 ETIMEDOUT. If ABSTIME is invalid, return EINVAL.
323 XXX Note that this differs from the generic version in that we do the
324 error checking here and not in __lll_timedwait_tid. */
325 #define lll_timedwait_tid(tid, abstime) \
326 ({ \
327 int __result = 0; \
328 if ((tid) != 0) \
330 if ((abstime)->tv_nsec < 0 || (abstime)->tv_nsec >= 1000000000) \
331 __result = EINVAL; \
332 else \
333 __result = __lll_timedwait_tid (&(tid), (abstime)); \
335 __result; })
337 extern int __lll_lock_elision (int *futex, short *adapt_count, int private)
338 attribute_hidden;
340 extern int __lll_unlock_elision (int *lock, int private)
341 attribute_hidden;
343 extern int __lll_trylock_elision (int *lock, short *adapt_count)
344 attribute_hidden;
346 #define lll_lock_elision(futex, adapt_count, private) \
347 __lll_lock_elision (&(futex), &(adapt_count), private)
348 #define lll_unlock_elision(futex, private) \
349 __lll_unlock_elision (&(futex), private)
350 #define lll_trylock_elision(futex, adapt_count) \
351 __lll_trylock_elision (&(futex), &(adapt_count))
353 #endif /* !__ASSEMBLER__ */
355 #endif /* lowlevellock.h */