]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/x86_64/lowlevellock.h
8cbc1caa5b0370f75bfc5a57beb9b1dd7796eec9
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>
30 # define LOCK_INSTR /* nothing */
32 # define LOCK_INSTR "lock;"
45 #include <lowlevellock-futex.h>
47 /* XXX Remove when no assembler code uses futexes anymore. */
48 #define SYS_futex __NR_futex
52 /* Initializer for lock. */
53 #define LLL_LOCK_INITIALIZER (0)
54 #define LLL_LOCK_INITIALIZER_LOCKED (1)
55 #define LLL_LOCK_INITIALIZER_WAITERS (2)
58 /* NB: in the lll_trylock macro we simply return the value in %eax
59 after the cmpxchg instruction. In case the operation succeded this
60 value is zero. In case the operation failed, the cmpxchg instruction
61 has loaded the current value of the memory work which is guaranteed
63 #if !IS_IN (libc) || defined UP
64 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
66 # define __lll_trylock_asm "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
68 "lock; cmpxchgl %2, %1\n\t" \
70 "0:\tcmpxchgl %2, %1\n\t" \
74 #define lll_trylock(futex) \
76 __asm __volatile (__lll_trylock_asm \
77 : "=a" (ret), "=m" (futex) \
78 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
79 "0" (LLL_LOCK_INITIALIZER) \
83 #define lll_cond_trylock(futex) \
85 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
86 : "=a" (ret), "=m" (futex) \
87 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
88 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
92 #if !IS_IN (libc) || defined UP
93 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %4, %2\n\t" \
96 # define __lll_lock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
98 "lock; cmpxchgl %4, %2\n\t" \
101 "0:\tcmpxchgl %4, %2\n\t" \
105 #define lll_lock(futex, private) \
107 ({ int ignore1, ignore2, ignore3; \
108 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
109 __asm __volatile (__lll_lock_asm_start \
110 "1:\tlea %2, %%" RDI_LP "\n" \
111 "2:\tsub $128, %%" RSP_LP "\n" \
112 ".cfi_adjust_cfa_offset 128\n" \
113 "3:\tcallq __lll_lock_wait_private\n" \
114 "4:\tadd $128, %%" RSP_LP "\n" \
115 ".cfi_adjust_cfa_offset -128\n" \
117 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
119 : "0" (1), "m" (futex), "3" (0) \
120 : "cx", "r11", "cc", "memory"); \
122 __asm __volatile (__lll_lock_asm_start \
123 "1:\tlea %2, %%" RDI_LP "\n" \
124 "2:\tsub $128, %%" RSP_LP "\n" \
125 ".cfi_adjust_cfa_offset 128\n" \
126 "3:\tcallq __lll_lock_wait\n" \
127 "4:\tadd $128, %%" RSP_LP "\n" \
128 ".cfi_adjust_cfa_offset -128\n" \
130 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
132 : "1" (1), "m" (futex), "3" (0), "0" (private) \
133 : "cx", "r11", "cc", "memory"); \
136 #define lll_cond_lock(futex, private) \
138 ({ int ignore1, ignore2, ignore3; \
139 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
141 "1:\tlea %2, %%" RDI_LP "\n" \
142 "2:\tsub $128, %%" RSP_LP "\n" \
143 ".cfi_adjust_cfa_offset 128\n" \
144 "3:\tcallq __lll_lock_wait\n" \
145 "4:\tadd $128, %%" RSP_LP "\n" \
146 ".cfi_adjust_cfa_offset -128\n" \
148 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
150 : "1" (2), "m" (futex), "3" (0), "0" (private) \
151 : "cx", "r11", "cc", "memory"); \
154 #define lll_timedlock(futex, timeout, private) \
155 ({ int result, ignore1, ignore2, ignore3; \
156 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
158 "1:\tlea %4, %%" RDI_LP "\n" \
159 "0:\tmov %8, %%" RDX_LP "\n" \
160 "2:\tsub $128, %%" RSP_LP "\n" \
161 ".cfi_adjust_cfa_offset 128\n" \
162 "3:\tcallq __lll_timedlock_wait\n" \
163 "4:\tadd $128, %%" RSP_LP "\n" \
164 ".cfi_adjust_cfa_offset -128\n" \
166 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
167 "=&d" (ignore3), "=m" (futex) \
168 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
170 : "memory", "cx", "cc", "r10", "r11"); \
173 extern int __lll_timedlock_elision (int *futex
, short *adapt_count
,
174 const struct timespec
*timeout
,
175 int private) attribute_hidden
;
177 #define lll_timedlock_elision(futex, adapt_count, timeout, private) \
178 __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
180 #if !IS_IN (libc) || defined UP
181 # define __lll_unlock_asm_start LOCK_INSTR "decl %0\n\t" \
184 # define __lll_unlock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
186 "lock; decl %0\n\t" \
193 #define lll_unlock(futex, private) \
196 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
197 __asm __volatile (__lll_unlock_asm_start \
198 "1:\tlea %0, %%" RDI_LP "\n" \
199 "2:\tsub $128, %%" RSP_LP "\n" \
200 ".cfi_adjust_cfa_offset 128\n" \
201 "3:\tcallq __lll_unlock_wake_private\n" \
202 "4:\tadd $128, %%" RSP_LP "\n" \
203 ".cfi_adjust_cfa_offset -128\n" \
205 : "=m" (futex), "=&D" (ignore) \
207 : "ax", "cx", "r11", "cc", "memory"); \
209 __asm __volatile (__lll_unlock_asm_start \
210 "1:\tlea %0, %%" RDI_LP "\n" \
211 "2:\tsub $128, %%" RSP_LP "\n" \
212 ".cfi_adjust_cfa_offset 128\n" \
213 "3:\tcallq __lll_unlock_wake\n" \
214 "4:\tadd $128, %%" RSP_LP "\n" \
215 ".cfi_adjust_cfa_offset -128\n" \
217 : "=m" (futex), "=&D" (ignore) \
218 : "m" (futex), "S" (private) \
219 : "ax", "cx", "r11", "cc", "memory"); \
222 #define lll_islocked(futex) \
223 (futex != LLL_LOCK_INITIALIZER)
225 extern int __lll_lock_elision (int *futex
, short *adapt_count
, int private)
228 extern int __lll_unlock_elision (int *lock
, int private)
231 extern int __lll_trylock_elision (int *lock
, short *adapt_count
)
234 #define lll_lock_elision(futex, adapt_count, private) \
235 __lll_lock_elision (&(futex), &(adapt_count), private)
236 #define lll_unlock_elision(futex, adapt_count, private) \
237 __lll_unlock_elision (&(futex), private)
238 #define lll_trylock_elision(futex, adapt_count) \
239 __lll_trylock_elision (&(futex), &(adapt_count))
241 #endif /* !__ASSEMBLER__ */
243 #endif /* lowlevellock.h */