Define lll_mutex_cond_trylock.
[kopensolaris-gnu/glibc.git] / nptl / sysdeps / unix / sysv / linux / ia64 / lowlevellock.h
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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 #include <ia64intrin.h>
27 #include <atomic.h>
28
29 #define __NR_futex              1230
30 #define FUTEX_WAIT              0
31 #define FUTEX_WAKE              1
32 #define FUTEX_REQUEUE           3
33
34 /* Delay in spinlock loop.  */
35 #define BUSY_WAIT_NOP          asm ("hint @pause")
36
37 /* Initializer for compatibility lock.  */
38 #define LLL_MUTEX_LOCK_INITIALIZER (0)
39
40 #define lll_futex_wait(futex, val) lll_futex_timed_wait (futex, val, 0)
41
42 #define lll_futex_timed_wait(ftx, val, timespec)                        \
43 ({                                                                      \
44    DO_INLINE_SYSCALL(futex, 4, (long) (ftx), FUTEX_WAIT, (int) (val),   \
45                      (long) (timespec));                                \
46    _r10 == -1 ? -_retval : _retval;                                     \
47 })
48
49 #define lll_futex_wake(ftx, nr)                                         \
50 ({                                                                      \
51    DO_INLINE_SYSCALL(futex, 3, (long) (ftx), FUTEX_WAKE, (int) (nr));   \
52    _r10 == -1 ? -_retval : _retval;                                     \
53 })
54
55 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex)                      \
56 ({                                                                           \
57    DO_INLINE_SYSCALL(futex, 5, (long) (ftx), FUTEX_REQUEUE, (int) (nr_wake), \
58                      (int) (nr_move), (long) (mutex));                       \
59    _r10 == -1 ? -_retval : _retval;                                          \
60 })
61
62
63 #define __lll_mutex_trylock(futex) \
64   (atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0)
65 #define lll_mutex_trylock(futex) __lll_mutex_trylock (&(futex))
66
67
68 #define __lll_mutex_cond_trylock(futex) \
69   (atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0)
70 #define lll_mutex_cond_trylock(futex) __lll_mutex_cond_trylock (&(futex))
71
72
73 extern void __lll_lock_wait (int *futex) attribute_hidden;
74
75
76 #define __lll_mutex_lock(futex)                                         \
77   ((void) ({                                                            \
78     int *__futex = (futex);                                             \
79     if (atomic_compare_and_exchange_bool_acq (__futex, 1, 0) != 0)      \
80       __lll_lock_wait (__futex);                                        \
81   }))
82 #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
83
84
85 #define __lll_mutex_cond_lock(futex)                                    \
86   ((void) ({                                                            \
87     int *__futex = (futex);                                             \
88     if (atomic_compare_and_exchange_bool_acq (__futex, 2, 0) != 0)      \
89       __lll_lock_wait (__futex);                                        \
90   }))
91 #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
92
93
94 extern int __lll_timedlock_wait (int *futex, const struct timespec *)
95      attribute_hidden;
96
97
98 #define __lll_mutex_timedlock(futex, abstime)                           \
99   ({                                                                    \
100      int *__futex = (futex);                                            \
101      int __val = 0;                                                     \
102                                                                         \
103      if (atomic_compare_and_exchange_bool_acq (__futex, 1, 0) != 0)     \
104        __val = __lll_timedlock_wait (__futex, abstime);                 \
105      __val;                                                             \
106   })
107 #define lll_mutex_timedlock(futex, abstime) \
108   __lll_mutex_timedlock (&(futex), abstime)
109
110
111 #define __lll_mutex_unlock(futex)                       \
112   ((void) ({                                            \
113     int *__futex = (futex);                             \
114     int __val = atomic_exchange_rel (__futex, 0);       \
115                                                         \
116     if (__builtin_expect (__val > 1, 0))                \
117       lll_futex_wake (__futex, 1);                      \
118   }))
119 #define lll_mutex_unlock(futex) \
120   __lll_mutex_unlock(&(futex))
121
122
123 #define __lll_mutex_unlock_force(futex)         \
124   ((void) ({                                    \
125     int *__futex = (futex);                     \
126     (void) atomic_exchange_rel (__futex, 0);    \
127     lll_futex_wake (__futex, 1);                \
128   }))
129 #define lll_mutex_unlock_force(futex) \
130   __lll_mutex_unlock_force(&(futex))
131
132
133 #define lll_mutex_islocked(futex) \
134   (futex != 0)
135
136
137 /* We have a separate internal lock implementation which is not tied
138    to binary compatibility.  We can use the lll_mutex_*.  */
139
140 /* Type for lock object.  */
141 typedef int lll_lock_t;
142
143 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
144
145 /* Initializers for lock.  */
146 #define LLL_LOCK_INITIALIZER            (0)
147 #define LLL_LOCK_INITIALIZER_LOCKED     (1)
148
149 #define lll_trylock(futex)      lll_mutex_trylock (futex)
150 #define lll_lock(futex)         lll_mutex_lock (futex)
151 #define lll_unlock(futex)       lll_mutex_unlock (futex)
152 #define lll_islocked(futex)     lll_mutex_islocked (futex)
153
154
155 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
156    wakeup when the clone terminates.  The memory location contains the
157    thread ID while the clone is running and is reset to zero
158    afterwards.  */
159 #define lll_wait_tid(tid) \
160   do                                            \
161     {                                           \
162       __typeof (tid) __tid;                     \
163       while ((__tid = (tid)) != 0)              \
164         lll_futex_wait (&(tid), __tid);         \
165     }                                           \
166   while (0)
167
168 extern int __lll_timedwait_tid (int *, const struct timespec *)
169      attribute_hidden;
170
171 #define lll_timedwait_tid(tid, abstime) \
172   ({                                                    \
173     int __res = 0;                                      \
174     if ((tid) != 0)                                     \
175       __res = __lll_timedwait_tid (&(tid), (abstime));  \
176     __res;                                              \
177   })
178
179
180 /* Conditional variable handling.  */
181
182 extern void __lll_cond_wait (pthread_cond_t *cond)
183      attribute_hidden;
184 extern int __lll_cond_timedwait (pthread_cond_t *cond,
185                                  const struct timespec *abstime)
186      attribute_hidden;
187 extern void __lll_cond_wake (pthread_cond_t *cond)
188      attribute_hidden;
189 extern void __lll_cond_broadcast (pthread_cond_t *cond)
190      attribute_hidden;
191
192 #define lll_cond_wait(cond) \
193   __lll_cond_wait (cond)
194 #define lll_cond_timedwait(cond, abstime) \
195   __lll_cond_timedwait (cond, abstime)
196 #define lll_cond_wake(cond) \
197   __lll_cond_wake (cond)
198 #define lll_cond_broadcast(cond) \
199   __lll_cond_broadcast (cond)
200
201 #endif  /* lowlevellock.h */