Define lll_mutex_cond_trylock.
[kopensolaris-gnu/glibc.git] / nptl / sysdeps / unix / sysv / linux / s390 / lowlevellock.h
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Martin Schwidefsky <schwidefsky@de.ibm.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 <atomic.h>
27
28 #define SYS_futex               238
29 #define FUTEX_WAIT              0
30 #define FUTEX_WAKE              1
31 #define FUTEX_REQUEUE           3
32
33 /* Initializer for compatibility lock.  */
34 #define LLL_MUTEX_LOCK_INITIALIZER (0)
35
36 #define lll_futex_wait(futex, val) \
37   ({                                                                          \
38      register unsigned long int __r2 asm ("2") = (unsigned long int) (futex); \
39      register unsigned long int __r3 asm ("3") = FUTEX_WAIT;                  \
40      register unsigned long int __r4 asm ("4") = (unsigned long int) (val);   \
41      register unsigned long int __r5 asm ("5") = 0ul;                         \
42      register unsigned long __result asm ("2");                               \
43                                                                               \
44     __asm __volatile ("svc %b1"                                               \
45                       : "=d" (__result)                                       \
46                       : "i" (SYS_futex), "0" (__r2), "d" (__r3),              \
47                         "d" (__r4), "d" (__r5)                                \
48                       : "cc", "memory" );                                     \
49     __result;                                                                 \
50   })
51
52
53 #define lll_futex_timed_wait(futex, val, timespec) \
54   ({                                                                          \
55     register unsigned long int __r2 asm ("2") = (unsigned long int) (futex);  \
56     register unsigned long int __r3 asm ("3") = FUTEX_WAIT;                   \
57     register unsigned long int __r4 asm ("4") = (unsigned long int) (val);    \
58     register unsigned long int __r5 asm ("5") = (unsigned long int)(timespec);\
59     register unsigned long int __result asm ("2");                            \
60                                                                               \
61     __asm __volatile ("svc %b1"                                               \
62                       : "=d" (__result)                                       \
63                       : "i" (SYS_futex), "0" (__r2), "d" (__r3),              \
64                         "d" (__r4), "d" (__r5)                                \
65                       : "cc", "memory" );                                     \
66     __result;                                                                 \
67   })
68
69
70 #define lll_futex_wake(futex, nr) \
71   ({                                                                          \
72     register unsigned long int __r2 asm ("2") = (unsigned long int) (futex);  \
73     register unsigned long int __r3 asm ("3") = FUTEX_WAKE;                   \
74     register unsigned long int __r4 asm ("4") = (unsigned long int) (nr);     \
75     register unsigned long int __result asm ("2");                            \
76                                                                               \
77     __asm __volatile ("svc %b1"                                               \
78                       : "=d" (__result)                                       \
79                       : "i" (SYS_futex), "0" (__r2), "d" (__r3), "d" (__r4)   \
80                       : "cc", "memory" );                                     \
81     __result;                                                                 \
82   })
83
84
85 #define lll_futex_requeue(futex, nr_wake, nr_move, mutex) \
86   ({                                                                          \
87      register unsigned long int __r2 asm ("2") = (unsigned long int) (futex); \
88      register unsigned long int __r3 asm ("3") = FUTEX_REQUEUE;               \
89      register unsigned long int __r4 asm ("4") = (long int) (nr_wake);        \
90      register unsigned long int __r5 asm ("5") = (long int) (nr_move);        \
91      register unsigned long int __r6 asm ("6") = (unsigned long int) (mutex); \
92      register unsigned long __result asm ("2");                               \
93                                                                               \
94     __asm __volatile ("svc %b1"                                               \
95                       : "=d" (__result)                                       \
96                       : "i" (SYS_futex), "0" (__r2), "d" (__r3),              \
97                         "d" (__r4), "d" (__r5), "d" (__r6)                    \
98                       : "cc", "memory" );                                     \
99     __result;                                                                 \
100   })
101
102
103 #define lll_compare_and_swap(futex, oldval, newval, operation) \
104   do {                                                                        \
105     __typeof (futex) __futex = (futex);                                       \
106     __asm __volatile ("   l   %1,%0\n"                                        \
107                       "0: " operation "\n"                                    \
108                       "   cs  %1,%2,%0\n"                                     \
109                       "   jl  0b\n"                                           \
110                       "1:"                                                    \
111                       : "=Q" (*__futex), "=&d" (oldval), "=&d" (newval)       \
112                       : "m" (*__futex) : "cc" );                              \
113   } while (0)
114
115
116 static inline int
117 __attribute__ ((always_inline))
118 __lll_mutex_trylock (int *futex)
119 {
120     unsigned int old;
121
122     __asm __volatile ("cs %0,%3,%1"
123                        : "=d" (old), "=Q" (*futex)
124                        : "0" (0), "d" (1), "m" (*futex) : "cc" );
125     return old != 0;
126 }
127 #define lll_mutex_trylock(futex) __lll_mutex_trylock (&(futex))
128
129
130 static inline int
131 __attribute__ ((always_inline))
132 __lll_mutex_cond_trylock (int *futex)
133 {
134     unsigned int old;
135
136     __asm __volatile ("cs %0,%3,%1"
137                        : "=d" (old), "=Q" (*futex)
138                        : "0" (0), "d" (2), "m" (*futex) : "cc" );
139     return old != 0;
140 }
141 #define lll_mutex_cond_trylock(futex) __lll_mutex_cond_trylock (&(futex))
142
143
144 extern void __lll_lock_wait (int *futex) attribute_hidden;
145
146 static inline void
147 __attribute__ ((always_inline))
148 __lll_mutex_lock (int *futex)
149 {
150   if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
151     __lll_lock_wait (futex);
152 }
153 #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
154
155 static inline void
156 __attribute__ ((always_inline))
157 __lll_mutex_cond_lock (int *futex)
158 {
159   if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0)
160     __lll_lock_wait (futex);
161 }
162 #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
163
164 extern int __lll_timedlock_wait
165   (int *futex, const struct timespec *) attribute_hidden;
166
167 static inline int
168 __attribute__ ((always_inline))
169 __lll_mutex_timedlock (int *futex, const struct timespec *abstime)
170 {
171   int result = 0;
172   if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
173     result = __lll_timedlock_wait (futex, abstime);
174   return result;
175 }
176 #define lll_mutex_timedlock(futex, abstime) \
177   __lll_mutex_timedlock (&(futex), abstime)
178
179
180 static inline void
181 __attribute__ ((always_inline))
182 __lll_mutex_unlock (int *futex)
183 {
184   int oldval;
185   int newval = 0;
186
187   lll_compare_and_swap (futex, oldval, newval, "slr %2,%2");
188   if (oldval > 1)
189     lll_futex_wake (futex, 1);
190 }
191 #define lll_mutex_unlock(futex) \
192   __lll_mutex_unlock(&(futex))
193
194
195 static inline void
196 __attribute__ ((always_inline))
197 __lll_mutex_unlock_force (int *futex)
198 {
199   *futex = 0;
200   lll_futex_wake (futex, 1);
201 }
202 #define lll_mutex_unlock_force(futex) \
203   __lll_mutex_unlock_force(&(futex))
204
205 #define lll_mutex_islocked(futex) \
206   (futex != 0)
207
208
209 /* We have a separate internal lock implementation which is not tied
210    to binary compatibility.  We can use the lll_mutex_*.  */
211
212 /* Type for lock object.  */
213 typedef int lll_lock_t;
214
215 /* Initializers for lock.  */
216 #define LLL_LOCK_INITIALIZER            (0)
217 #define LLL_LOCK_INITIALIZER_LOCKED     (1)
218
219 #define lll_trylock(futex)      lll_mutex_trylock (futex)
220 #define lll_lock(futex)         lll_mutex_lock (futex)
221 #define lll_unlock(futex)       lll_mutex_unlock (futex)
222 #define lll_islocked(futex)     lll_mutex_islocked (futex)
223
224 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
225
226 /* The states of a lock are:
227     1  -  untaken
228     0  -  taken by one user
229    <0  -  taken by more users */
230
231
232 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
233    wakeup when the clone terminates.  The memory location contains the
234    thread ID while the clone is running and is reset to zero
235    afterwards.  */
236 static inline void
237 __attribute__ ((always_inline))
238 __lll_wait_tid (int *ptid)
239 {
240   int tid;
241
242   while ((tid = *ptid) != 0)
243     lll_futex_wait (ptid, tid);
244 }
245 #define lll_wait_tid(tid) __lll_wait_tid(&(tid))
246
247 extern int __lll_timedwait_tid (int *, const struct timespec *)
248      attribute_hidden;
249
250 #define lll_timedwait_tid(tid, abstime) \
251   ({                                                                          \
252     int __res = 0;                                                            \
253     if ((tid) != 0)                                                           \
254       __res = __lll_timedwait_tid (&(tid), (abstime));                        \
255     __res;                                                                    \
256   })
257
258 /* Conditional variable handling.  */
259
260 extern void __lll_cond_wait (pthread_cond_t *cond)
261      attribute_hidden;
262 extern int __lll_cond_timedwait (pthread_cond_t *cond,
263                                  const struct timespec *abstime)
264      attribute_hidden;
265 extern void __lll_cond_wake (pthread_cond_t *cond)
266      attribute_hidden;
267 extern void __lll_cond_broadcast (pthread_cond_t *cond)
268      attribute_hidden;
269
270 #define lll_cond_wait(cond) \
271   __lll_cond_wait (cond)
272 #define lll_cond_timedwait(cond, abstime) \
273   __lll_cond_timedwait (cond, abstime)
274 #define lll_cond_wake(cond) \
275   __lll_cond_wake (cond)
276 #define lll_cond_broadcast(cond) \
277   __lll_cond_broadcast (cond)
278
279 #endif  /* lowlevellock.h */