156d1f7c646d813320a8c89d6df0fde32a26859d
[kopensolaris-gnu/glibc.git] / nptl / sysdeps / unix / sysv / linux / x86_64 / lowlevellock.h
1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
22
23 #include <time.h>
24 #include <sys/param.h>
25 #include <bits/pthreadtypes.h>
26
27 #ifndef LOCK_INSTR
28 # ifdef UP
29 #  define LOCK_INSTR    /* nothing */
30 # else
31 #  define LOCK_INSTR "lock;"
32 # endif
33 #endif
34
35 #define SYS_futex               202
36 #define FUTEX_WAIT              0
37 #define FUTEX_WAKE              1
38
39
40 /* Initializer for compatibility lock.  */
41 #define LLL_MUTEX_LOCK_INITIALIZER              (0)
42 #define LLL_MUTEX_LOCK_INITIALIZER_LOCKED       (1)
43
44
45 #define lll_futex_wait(futex, val) \
46   do {                                                                        \
47     int __ignore;                                                             \
48     register __typeof (val) _val asm ("edx") = (val);                         \
49     __asm __volatile ("xorq %%r10, %%r10\n\t"                                 \
50                       "syscall"                                               \
51                       : "=a" (__ignore)                                       \
52                       : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAIT),       \
53                         "d" (_val)                                            \
54                       : "memory", "cc", "r10", "r11", "cx");                  \
55   } while (0)
56
57
58 #define lll_futex_wake(futex, nr) \
59   do {                                                                        \
60     int __ignore;                                                             \
61     register __typeof (nr) _nr asm ("edx") = (nr);                            \
62     __asm __volatile ("syscall"                                               \
63                       : "=a" (__ignore)                                       \
64                       : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAKE),       \
65                         "d" (_nr)                                             \
66                       : "memory", "cc", "r10", "r11", "cx");                  \
67   } while (0)
68
69
70 /* Does not preserve %eax and %ecx.  */
71 extern int __lll_mutex_lock_wait (int *__futex, int __val) attribute_hidden;
72 /* Does not preserver %eax, %ecx, and %edx.  */
73 extern int __lll_mutex_timedlock_wait (int *__futex, int __val,
74                                        const struct timespec *__abstime)
75      attribute_hidden;
76 /* Preserves all registers but %eax.  */
77 extern int __lll_mutex_unlock_wait (int *__futex) attribute_hidden;
78
79
80 #define lll_mutex_trylock(futex) \
81   ({ unsigned char ret;                                                       \
82      __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1; setne %0"                 \
83                        : "=a" (ret), "=m" (futex)                             \
84                        : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
85                          "0" (LLL_MUTEX_LOCK_INITIALIZER)                     \
86                        : "memory");                                           \
87      ret; })
88
89
90 #define lll_mutex_lock(futex) \
91   (void) ({ int ignore1, ignore2, ignore3;                                    \
92             __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t"                \
93                               "jnz 1f\n\t"                                    \
94                               ".subsection 1\n"                               \
95                               "1:\tleaq %2, %%rdi\n\t"                        \
96                               "subq $128, %%rsp\n\t"                          \
97                               "callq __lll_mutex_lock_wait\n\t"               \
98                               "addq $128, %%rsp\n\t"                          \
99                               "jmp 2f\n\t"                                    \
100                               ".previous\n"                                   \
101                               "2:"                                            \
102                               : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
103                                 "=a" (ignore3)                                \
104                               : "0" (1), "m" (futex), "3" (0)                 \
105                               : "cx", "r11", "cc", "memory"); })
106
107
108 #define lll_mutex_cond_lock(futex) \
109   (void) ({ int ignore1, ignore2, ignore3;                                    \
110             __asm __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t"                \
111                               "jnz 1f\n\t"                                    \
112                               ".subsection 1\n"                               \
113                               "1:\tleaq %2, %%rdi\n\t"                        \
114                               "subq $128, %%rsp\n\t"                          \
115                               "callq __lll_mutex_lock_wait\n\t"               \
116                               "addq $128, %%rsp\n\t"                          \
117                               "jmp 2f\n\t"                                    \
118                               ".previous\n"                                   \
119                               "2:"                                            \
120                               : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
121                                 "=a" (ignore3)                                \
122                               : "0" (2), "m" (futex), "3" (0)                 \
123                               : "cx", "r11", "cc", "memory"); })
124
125
126 #define lll_mutex_timedlock(futex, timeout) \
127   ({ int result, ignore1, ignore2, ignore3;                                   \
128      __asm __volatile (LOCK_INSTR "cmpxchgl %2, %4\n\t"                       \
129                        "jnz 1f\n\t"                                           \
130                        ".subsection 1\n"                                      \
131                        "1:\tleaq %4, %%rdi\n\t"                               \
132                        "movq %8, %%rdx\n\t"                                   \
133                        "subq $128, %%rsp\n\t"                                 \
134                        "callq __lll_mutex_timedlock_wait\n\t"                 \
135                        "addq $128, %%rsp\n\t"                                 \
136                        "jmp 2f\n\t"                                           \
137                        ".previous\n"                                          \
138                        "2:"                                                   \
139                        : "=a" (result), "=&D" (ignore1), "=S" (ignore2),      \
140                          "=&d" (ignore3), "=m" (futex)                        \
141                        : "0" (0), "2" (1), "m" (futex), "m" (timeout)         \
142                        : "memory", "cx", "cc", "r10", "r11");                 \
143      result; })
144
145
146 #define lll_mutex_unlock(futex) \
147   (void) ({ int ignore;                                                       \
148             __asm __volatile (LOCK_INSTR "decl %0\n\t"                        \
149                               "jne 1f\n\t"                                    \
150                               ".subsection 1\n"                               \
151                               "1:\tleaq %0, %%rdi\n\t"                        \
152                               "subq $128, %%rsp\n\t"                          \
153                               "callq __lll_mutex_unlock_wake\n\t"             \
154                               "addq $128, %%rsp\n\t"                          \
155                               "jmp 2f\n\t"                                    \
156                               ".previous\n"                                   \
157                               "2:"                                            \
158                               : "=m" (futex), "=&D" (ignore)                  \
159                               : "m" (futex)                                   \
160                               : "ax", "cx", "r11", "cc", "memory"); })
161
162
163 #define lll_mutex_islocked(futex) \
164   (futex != LLL_MUTEX_LOCK_INITIALIZER)
165
166
167 /* We have a separate internal lock implementation which is not tied
168    to binary compatibility.  */
169
170 /* Type for lock object.  */
171 typedef int lll_lock_t;
172
173 /* Initializers for lock.  */
174 #define LLL_LOCK_INITIALIZER            (0)
175 #define LLL_LOCK_INITIALIZER_LOCKED     (1)
176
177
178 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
179
180
181 /* The states of a lock are:
182     0  -  untaken
183     1  -  taken by one user
184     2  -  taken by more users */
185
186
187 #if defined NOT_IN_libc || defined UP
188 # define lll_trylock(futex) lll_mutex_trylock (futex)
189 # define lll_lock(futex) lll_mutex_lock (futex)
190 # define lll_unlock(futex) lll_mutex_unlock (futex)
191 #else
192 /* Special versions of the macros for use in libc itself.  They avoid
193    the lock prefix when the thread library is not used.
194
195    The code sequence to avoid unnecessary lock prefixes is what the AMD
196    guys suggested.  If you do not like it, bring it up with AMD.
197
198    XXX In future we might even want to avoid it on UP machines.  */
199
200 # define lll_trylock(futex) \
201   ({ unsigned char ret;                                                       \
202      __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t"          \
203                        "je 0f\n\t"                                            \
204                        "lock; cmpxchgl %2, %1\n\t"                            \
205                        "jmp 1f\n"                                             \
206                        "0:\tcmpxchgl %2, %1\n\t"                              \
207                        "1:setne %0"                                           \
208                        : "=a" (ret), "=m" (futex)                             \
209                        : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
210                          "0" (LLL_MUTEX_LOCK_INITIALIZER)                     \
211                        : "memory");                                           \
212      ret; })
213
214
215 # define lll_lock(futex) \
216   (void) ({ int ignore1, ignore2, ignore3;                                    \
217             __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t"   \
218                               "je 0f\n\t"                                     \
219                               "lock; cmpxchgl %0, %2\n\t"                     \
220                               "jnz 1f\n\t"                                    \
221                               "jmp 2f\n"                                      \
222                               "0:\tcmpxchgl %0, %2\n\t"                       \
223                               "jnz 1f\n\t"                                    \
224                               ".subsection 1\n"                               \
225                               "1:\tleaq %2, %%rdi\n\t"                        \
226                               "subq $128, %%rsp\n\t"                          \
227                               "callq __lll_mutex_lock_wait\n\t"               \
228                               "addq $128, %%rsp\n\t"                          \
229                               "jmp 2f\n\t"                                    \
230                               ".previous\n"                                   \
231                               "2:"                                            \
232                               : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
233                                 "=a" (ignore3)                                \
234                               : "0" (1), "m" (futex), "3" (0)                 \
235                               : "cx", "r11", "cc", "memory"); })
236
237
238 # define lll_unlock(futex) \
239   (void) ({ int ignore;                                                       \
240             __asm __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t"   \
241                               "je 0f\n\t"                                     \
242                               "lock; decl %0\n\t"                             \
243                               "jne 1f\n\t"                                    \
244                               "jmp 2f\n"                                      \
245                               "0:\tdecl %0\n\t"                               \
246                               "jne 1f\n\t"                                    \
247                               ".subsection 1\n"                               \
248                               "1:\tleaq %0, %%rdi\n\t"                        \
249                               "subq $128, %%rsp\n\t"                          \
250                               "callq __lll_mutex_unlock_wake\n\t"             \
251                               "addq $128, %%rsp\n\t"                          \
252                               "jmp 2f\n\t"                                    \
253                               ".previous\n"                                   \
254                               "2:"                                            \
255                               : "=m" (futex), "=&D" (ignore)                  \
256                               : "0" (futex)                                   \
257                               : "ax", "cx", "r11", "cc", "memory"); })
258 #endif
259
260
261 #define lll_islocked(futex) \
262   (futex != LLL_MUTEX_LOCK_INITIALIZER)
263
264
265 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
266    wakeup when the clone terminates.  The memory location contains the
267    thread ID while the clone is running and is reset to zero
268    afterwards.
269
270    The macro parameter must not have any side effect.  */
271 #define lll_wait_tid(tid) \
272   do {                                                                        \
273     int __ignore;                                                             \
274     register __typeof (tid) _tid asm ("edx") = (tid);                         \
275     if (_tid != 0)                                                            \
276       __asm __volatile ("xorq %%r10, %%r10\n\t"                               \
277                         "1:\tmovq %2, %%rax\n\t"                              \
278                         "syscall\n\t"                                         \
279                         "cmpl $0, (%%rdi)\n\t"                                \
280                         "jne 1b"                                              \
281                         : "=&a" (__ignore)                                    \
282                         : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid),      \
283                           "d" (_tid)                                          \
284                         : "memory", "cc", "r10", "r11", "cx");                \
285   } while (0)
286
287 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
288      attribute_hidden;
289 #define lll_timedwait_tid(tid, abstime) \
290   ({                                                                          \
291     int __result = 0;                                                         \
292     if (tid != 0)                                                             \
293       {                                                                       \
294         if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)           \
295           __result = EINVAL;                                                  \
296         else                                                                  \
297           __result = __lll_timedwait_tid (&tid, abstime);                     \
298       }                                                                       \
299     __result; })
300
301
302 /* Conditional variable handling.  */
303
304 extern void __lll_cond_wait (pthread_cond_t *cond) attribute_hidden;
305 extern int __lll_cond_timedwait (pthread_cond_t *cond,
306                                  const struct timespec *abstime)
307      attribute_hidden;
308 extern void __lll_cond_wake (pthread_cond_t *cond) attribute_hidden;
309 extern void __lll_cond_broadcast (pthread_cond_t *cond) attribute_hidden;
310
311
312 #define lll_cond_wait(cond) \
313   __lll_cond_wait (cond)
314 #define lll_cond_timedwait(cond, abstime) \
315   __lll_cond_timedwait (cond, abstime)
316 #define lll_cond_wake(cond) \
317   __lll_cond_wake (cond)
318 #define lll_cond_broadcast(cond) \
319   __lll_cond_broadcast (cond)
320
321
322 #endif  /* lowlevellock.h */