]>
git.ipfire.org Git - thirdparty/glibc.git/blob - 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.
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
24 # include <sys/param.h>
25 # include <bits/pthreadtypes.h>
26 # include <kernel-features.h>
27 /* <tcb-offsets.h> is generated from tcb-offsets.sym to define offsets
28 and sizes of types in <tls.h> as well as <pthread.h> which includes
29 <lowlevellock.h> via nptl/descr.h. Don't include <tcb-offsets.h>
30 when generating <tcb-offsets.h> to avoid circular dependency which
31 may lead to build hang on a many-core machine. */
32 # ifndef GEN_AS_CONST_HEADERS
33 # include <tcb-offsets.h>
38 # define LOCK_INSTR /* nothing */
40 # define LOCK_INSTR "lock;"
53 #include <lowlevellock-futex.h>
55 /* XXX Remove when no assembler code uses futexes anymore. */
56 #define SYS_futex __NR_futex
60 /* Initializer for compatibility lock. */
61 #define LLL_LOCK_INITIALIZER (0)
62 #define LLL_LOCK_INITIALIZER_LOCKED (1)
63 #define LLL_LOCK_INITIALIZER_WAITERS (2)
66 /* NB: in the lll_trylock macro we simply return the value in %eax
67 after the cmpxchg instruction. In case the operation succeded this
68 value is zero. In case the operation failed, the cmpxchg instruction
69 has loaded the current value of the memory work which is guaranteed
71 #if !IS_IN (libc) || defined UP
72 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
74 # define __lll_trylock_asm "cmpl $0, %%gs:%P5\n\t" \
80 #define lll_trylock(futex) \
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 "i" (MULTIPLE_THREADS_OFFSET) \
91 #define lll_cond_trylock(futex) \
93 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
94 : "=a" (ret), "=m" (futex) \
95 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
96 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
100 #if !IS_IN (libc) || defined UP
101 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %1, %2\n\t"
103 # define __lll_lock_asm_start "cmpl $0, %%gs:%P6\n\t" \
106 "0:\tcmpxchgl %1, %2\n\t"
109 #define lll_lock(futex, private) \
111 ({ int ignore1, ignore2; \
112 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
113 __asm __volatile (__lll_lock_asm_start \
115 "1:\tleal %2, %%ecx\n" \
116 "2:\tcall __lll_lock_wait_private\n" \
118 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
119 : "0" (0), "1" (1), "m" (futex), \
120 "i" (MULTIPLE_THREADS_OFFSET) \
125 __asm __volatile (__lll_lock_asm_start \
127 "1:\tleal %2, %%edx\n" \
128 "0:\tmovl %8, %%ecx\n" \
129 "2:\tcall __lll_lock_wait\n" \
131 : "=a" (ignore1), "=c" (ignore2), \
132 "=m" (futex), "=&d" (ignore3) \
133 : "1" (1), "m" (futex), \
134 "i" (MULTIPLE_THREADS_OFFSET), "0" (0), \
135 "g" ((int) (private)) \
141 /* Special version of lll_lock which causes the unlock function to
142 always wakeup waiters. */
143 #define lll_cond_lock(futex, private) \
145 ({ int ignore1, ignore2, ignore3; \
146 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
148 "1:\tleal %2, %%edx\n" \
149 "0:\tmovl %7, %%ecx\n" \
150 "2:\tcall __lll_lock_wait\n" \
152 : "=a" (ignore1), "=c" (ignore2), "=m" (futex), \
154 : "0" (0), "1" (2), "m" (futex), "g" ((int) (private))\
159 #define lll_timedlock(futex, timeout, private) \
160 ({ int result, ignore1, ignore2, ignore3; \
161 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
163 "1:\tleal %3, %%ecx\n" \
164 "0:\tmovl %8, %%edx\n" \
165 "2:\tcall __lll_timedlock_wait\n" \
167 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
168 "=m" (futex), "=S" (ignore3) \
169 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
170 "4" ((int) (private)) \
174 extern int __lll_timedlock_elision (int *futex
, short *adapt_count
,
175 const struct timespec
*timeout
,
176 int private) attribute_hidden
;
178 #define lll_timedlock_elision(futex, adapt_count, timeout, private) \
179 __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
181 #if !IS_IN (libc) || defined UP
182 # define __lll_unlock_asm LOCK_INSTR "subl $1, %0\n\t"
184 # define __lll_unlock_asm "cmpl $0, %%gs:%P3\n\t" \
190 #define lll_unlock(futex, private) \
193 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
194 __asm __volatile (__lll_unlock_asm \
196 "1:\tleal %0, %%eax\n" \
197 "2:\tcall __lll_unlock_wake_private\n" \
199 : "=m" (futex), "=&a" (ignore) \
200 : "m" (futex), "i" (MULTIPLE_THREADS_OFFSET) \
205 __asm __volatile (__lll_unlock_asm \
207 "1:\tleal %0, %%eax\n" \
208 "0:\tmovl %5, %%ecx\n" \
209 "2:\tcall __lll_unlock_wake\n" \
211 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
212 : "i" (MULTIPLE_THREADS_OFFSET), "m" (futex), \
213 "g" ((int) (private)) \
219 #define lll_islocked(futex) \
220 (futex != LLL_LOCK_INITIALIZER)
222 extern int __lll_lock_elision (int *futex
, short *adapt_count
, int private)
225 extern int __lll_unlock_elision(int *lock
, int private)
228 extern int __lll_trylock_elision(int *lock
, short *adapt_count
)
231 #define lll_lock_elision(futex, adapt_count, private) \
232 __lll_lock_elision (&(futex), &(adapt_count), private)
233 #define lll_unlock_elision(futex, adapt_count, private) \
234 __lll_unlock_elision (&(futex), private)
235 #define lll_trylock_elision(futex, adapt_count) \
236 __lll_trylock_elision(&(futex), &(adapt_count))
238 #endif /* !__ASSEMBLER__ */
240 #endif /* lowlevellock.h */