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