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