Remove unused macros from i386 lowlevellock.h.
[glibc.git] / sysdeps / unix / sysv / linux / i386 / lowlevellock.h
blobf57afc6e2a90f47e98bc20b187812b66557d4386
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 compatibility lock. */
56 #define LLL_LOCK_INITIALIZER (0)
57 #define LLL_LOCK_INITIALIZER_LOCKED (1)
58 #define LLL_LOCK_INITIALIZER_WAITERS (2)
61 /* Delay in spinlock loop. */
62 #define BUSY_WAIT_NOP asm ("rep; nop")
65 /* NB: in the lll_trylock macro we simply return the value in %eax
66 after the cmpxchg instruction. In case the operation succeded this
67 value is zero. In case the operation failed, the cmpxchg instruction
68 has loaded the current value of the memory work which is guaranteed
69 to be nonzero. */
70 #if !IS_IN (libc) || defined UP
71 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
72 #else
73 # define __lll_trylock_asm "cmpl $0, %%gs:%P5\n\t" \
74 "je 0f\n\t" \
75 "lock\n" \
76 "0:\tcmpxchgl %2, %1"
77 #endif
79 #define lll_trylock(futex) \
80 ({ int ret; \
81 __asm __volatile (__lll_trylock_asm \
82 : "=a" (ret), "=m" (futex) \
83 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
84 "0" (LLL_LOCK_INITIALIZER), \
85 "i" (MULTIPLE_THREADS_OFFSET) \
86 : "memory"); \
87 ret; })
90 #define lll_cond_trylock(futex) \
91 ({ int ret; \
92 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
93 : "=a" (ret), "=m" (futex) \
94 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
95 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
96 : "memory"); \
97 ret; })
99 #if !IS_IN (libc) || defined UP
100 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %1, %2\n\t"
101 #else
102 # define __lll_lock_asm_start "cmpl $0, %%gs:%P6\n\t" \
103 "je 0f\n\t" \
104 "lock\n" \
105 "0:\tcmpxchgl %1, %2\n\t"
106 #endif
108 #define lll_lock(futex, private) \
109 (void) \
110 ({ int ignore1, ignore2; \
111 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
112 __asm __volatile (__lll_lock_asm_start \
113 "jz 18f\n\t" \
114 "1:\tleal %2, %%ecx\n" \
115 "2:\tcall __lll_lock_wait_private\n" \
116 "18:" \
117 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
118 : "0" (0), "1" (1), "m" (futex), \
119 "i" (MULTIPLE_THREADS_OFFSET) \
120 : "memory"); \
121 else \
123 int ignore3; \
124 __asm __volatile (__lll_lock_asm_start \
125 "jz 18f\n\t" \
126 "1:\tleal %2, %%edx\n" \
127 "0:\tmovl %8, %%ecx\n" \
128 "2:\tcall __lll_lock_wait\n" \
129 "18:" \
130 : "=a" (ignore1), "=c" (ignore2), \
131 "=m" (futex), "=&d" (ignore3) \
132 : "1" (1), "m" (futex), \
133 "i" (MULTIPLE_THREADS_OFFSET), "0" (0), \
134 "g" ((int) (private)) \
135 : "memory"); \
139 #define lll_robust_lock(futex, id, private) \
140 ({ int result, ignore1, ignore2; \
141 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
142 "jz 18f\n\t" \
143 "1:\tleal %2, %%edx\n" \
144 "0:\tmovl %7, %%ecx\n" \
145 "2:\tcall __lll_robust_lock_wait\n" \
146 "18:" \
147 : "=a" (result), "=c" (ignore1), "=m" (futex), \
148 "=&d" (ignore2) \
149 : "0" (0), "1" (id), "m" (futex), "g" ((int) (private))\
150 : "memory"); \
151 result; })
154 /* Special version of lll_lock which causes the unlock function to
155 always wakeup waiters. */
156 #define lll_cond_lock(futex, private) \
157 (void) \
158 ({ int ignore1, ignore2, ignore3; \
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_lock_wait\n" \
164 "18:" \
165 : "=a" (ignore1), "=c" (ignore2), "=m" (futex), \
166 "=&d" (ignore3) \
167 : "0" (0), "1" (2), "m" (futex), "g" ((int) (private))\
168 : "memory"); \
172 #define lll_robust_cond_lock(futex, id, private) \
173 ({ int result, ignore1, ignore2; \
174 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
175 "jz 18f\n\t" \
176 "1:\tleal %2, %%edx\n" \
177 "0:\tmovl %7, %%ecx\n" \
178 "2:\tcall __lll_robust_lock_wait\n" \
179 "18:" \
180 : "=a" (result), "=c" (ignore1), "=m" (futex), \
181 "=&d" (ignore2) \
182 : "0" (0), "1" (id | FUTEX_WAITERS), "m" (futex), \
183 "g" ((int) (private)) \
184 : "memory"); \
185 result; })
188 #define lll_timedlock(futex, timeout, private) \
189 ({ int result, ignore1, ignore2, ignore3; \
190 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
191 "jz 18f\n\t" \
192 "1:\tleal %3, %%ecx\n" \
193 "0:\tmovl %8, %%edx\n" \
194 "2:\tcall __lll_timedlock_wait\n" \
195 "18:" \
196 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
197 "=m" (futex), "=S" (ignore3) \
198 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
199 "4" ((int) (private)) \
200 : "memory"); \
201 result; })
203 extern int __lll_timedlock_elision (int *futex, short *adapt_count,
204 const struct timespec *timeout,
205 int private) attribute_hidden;
207 #define lll_timedlock_elision(futex, adapt_count, timeout, private) \
208 __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
210 #define lll_robust_timedlock(futex, timeout, id, private) \
211 ({ int result, ignore1, ignore2, ignore3; \
212 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
213 "jz 18f\n\t" \
214 "1:\tleal %3, %%ecx\n" \
215 "0:\tmovl %8, %%edx\n" \
216 "2:\tcall __lll_robust_timedlock_wait\n" \
217 "18:" \
218 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
219 "=m" (futex), "=S" (ignore3) \
220 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
221 "4" ((int) (private)) \
222 : "memory"); \
223 result; })
225 #if !IS_IN (libc) || defined UP
226 # define __lll_unlock_asm LOCK_INSTR "subl $1, %0\n\t"
227 #else
228 # define __lll_unlock_asm "cmpl $0, %%gs:%P3\n\t" \
229 "je 0f\n\t" \
230 "lock\n" \
231 "0:\tsubl $1,%0\n\t"
232 #endif
234 #define lll_unlock(futex, private) \
235 (void) \
236 ({ int ignore; \
237 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
238 __asm __volatile (__lll_unlock_asm \
239 "je 18f\n\t" \
240 "1:\tleal %0, %%eax\n" \
241 "2:\tcall __lll_unlock_wake_private\n" \
242 "18:" \
243 : "=m" (futex), "=&a" (ignore) \
244 : "m" (futex), "i" (MULTIPLE_THREADS_OFFSET) \
245 : "memory"); \
246 else \
248 int ignore2; \
249 __asm __volatile (__lll_unlock_asm \
250 "je 18f\n\t" \
251 "1:\tleal %0, %%eax\n" \
252 "0:\tmovl %5, %%ecx\n" \
253 "2:\tcall __lll_unlock_wake\n" \
254 "18:" \
255 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
256 : "i" (MULTIPLE_THREADS_OFFSET), "m" (futex), \
257 "g" ((int) (private)) \
258 : "memory"); \
262 #define lll_robust_unlock(futex, private) \
263 (void) \
264 ({ int ignore, ignore2; \
265 __asm __volatile (LOCK_INSTR "andl %3, %0\n\t" \
266 "je 18f\n\t" \
267 "1:\tleal %0, %%eax\n" \
268 "0:\tmovl %5, %%ecx\n" \
269 "2:\tcall __lll_unlock_wake\n" \
270 "18:" \
271 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
272 : "i" (FUTEX_WAITERS), "m" (futex), \
273 "g" ((int) (private)) \
274 : "memory"); \
278 #define lll_islocked(futex) \
279 (futex != LLL_LOCK_INITIALIZER)
281 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
282 wake-up when the clone terminates. The memory location contains the
283 thread ID while the clone is running and is reset to zero by the kernel
284 afterwards. The kernel up to version 3.16.3 does not use the private futex
285 operations for futex wake-up when the clone terminates. */
286 #define lll_wait_tid(tid) \
287 do { \
288 __typeof (tid) __tid; \
289 while ((__tid = (tid)) != 0) \
290 lll_futex_wait (&(tid), __tid, LLL_SHARED);\
291 } while (0)
293 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
294 __attribute__ ((regparm (2))) attribute_hidden;
296 /* As lll_wait_tid, but with a timeout. If the timeout occurs then return
297 ETIMEDOUT. If ABSTIME is invalid, return EINVAL.
298 XXX Note that this differs from the generic version in that we do the
299 error checking here and not in __lll_timedwait_tid. */
300 #define lll_timedwait_tid(tid, abstime) \
301 ({ \
302 int __result = 0; \
303 if ((tid) != 0) \
305 if ((abstime)->tv_nsec < 0 || (abstime)->tv_nsec >= 1000000000) \
306 __result = EINVAL; \
307 else \
308 __result = __lll_timedwait_tid (&(tid), (abstime)); \
310 __result; })
313 extern int __lll_lock_elision (int *futex, short *adapt_count, int private)
314 attribute_hidden;
316 extern int __lll_unlock_elision(int *lock, int private)
317 attribute_hidden;
319 extern int __lll_trylock_elision(int *lock, short *adapt_count)
320 attribute_hidden;
322 #define lll_lock_elision(futex, adapt_count, private) \
323 __lll_lock_elision (&(futex), &(adapt_count), private)
324 #define lll_unlock_elision(futex, private) \
325 __lll_unlock_elision (&(futex), private)
326 #define lll_trylock_elision(futex, adapt_count) \
327 __lll_trylock_elision(&(futex), &(adapt_count))
329 #endif /* !__ASSEMBLER__ */
331 #endif /* lowlevellock.h */