Define lll_mutex_cond_trylock.
[kopensolaris-gnu/glibc.git] / nptl / sysdeps / unix / sysv / linux / alpha / lowlevellock.h
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Libr    \ary; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
21
22 #include <time.h>
23 #include <sys/param.h>
24 #include <bits/pthreadtypes.h>
25 #include <atomic.h>
26 #include <sysdep.h>
27
28
29 #define __NR_futex              394
30 #define FUTEX_WAIT              0
31 #define FUTEX_WAKE              1
32 #define FUTEX_REQUEUE           3
33
34 /* Initializer for compatibility lock.  */
35 #define LLL_MUTEX_LOCK_INITIALIZER (0)
36
37 #define lll_futex_wait(futexp, val) \
38   ({                                                                          \
39     INTERNAL_SYSCALL_DECL (__err);                                            \
40     long int __ret;                                                           \
41     __ret = INTERNAL_SYSCALL (futex, __err, 4,                                \
42                               (futexp), FUTEX_WAIT, (val), 0);                \
43     INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret;                  \
44   })
45
46 #define lll_futex_timed_wait(futexp, val, timespec) \
47   ({                                                                          \
48     INTERNAL_SYSCALL_DECL (__err);                                            \
49     long int __ret;                                                           \
50     __ret = INTERNAL_SYSCALL (futex, __err, 4,                                \
51                               (futexp), FUTEX_WAIT, (val), (timespec));       \
52     INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret;                  \
53   })
54
55 #define lll_futex_wake(futexp, nr) \
56   ({                                                                          \
57     INTERNAL_SYSCALL_DECL (__err);                                            \
58     long int __ret;                                                           \
59     __ret = INTERNAL_SYSCALL (futex, __err, 4,                                \
60                               (futexp), FUTEX_WAKE, (nr), 0);                 \
61     INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret;                  \
62   })
63
64 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex) \
65   ({                                                                          \
66     INTERNAL_SYSCALL_DECL (__err);                                            \
67     long int __ret;                                                           \
68     __ret = INTERNAL_SYSCALL (futex, __err, 5,                                \
69                               (futexp), FUTEX_REQUEUE, (nr_wake), (nr_move),  \
70                               (mutex));                                       \
71     INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret;                  \
72   })
73
74
75 static inline int __attribute__((always_inline))
76 __lll_mutex_trylock(int *futex)
77 {
78   return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
79 }
80 #define lll_mutex_trylock(lock) __lll_mutex_trylock (&(lock))
81
82
83 static inline int __attribute__((always_inline))
84 __lll_mutex_cond_trylock(int *futex)
85 {
86   return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
87 }
88 #define lll_mutex_cond_trylock(lock)    __lll_mutex_cond_trylock (&(lock))
89
90
91 extern void __lll_lock_wait (int *futex) attribute_hidden;
92
93 static inline void __attribute__((always_inline))
94 __lll_mutex_lock(int *futex)
95 {
96   if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
97     __lll_lock_wait (futex);
98 }
99 #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
100
101
102 static inline void __attribute__ ((always_inline))
103 __lll_mutex_cond_lock (int *futex)
104 {
105   if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0)
106     __lll_lock_wait (futex);
107 }
108 #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
109
110
111 extern int __lll_timedlock_wait (int *futex, const struct timespec *)
112         attribute_hidden;
113
114 static inline int __attribute__ ((always_inline))
115 __lll_mutex_timedlock (int *futex, const struct timespec *abstime)
116 {
117   int result = 0;
118   if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
119     result = __lll_timedlock_wait (futex, abstime);
120   return result;
121 }
122 #define lll_mutex_timedlock(futex, abstime) \
123   __lll_mutex_timedlock (&(futex), abstime)
124
125
126 static inline void __attribute__ ((always_inline))
127 __lll_mutex_unlock (int *futex)
128 {
129   int val = atomic_exchange_rel (futex, 0);
130   if (__builtin_expect (val > 1, 0))
131     lll_futex_wake (futex, 1);
132 }
133 #define lll_mutex_unlock(futex) __lll_mutex_unlock(&(futex))
134
135
136 static inline void __attribute__ ((always_inline))
137 __lll_mutex_unlock_force (int *futex)
138 {
139   (void) atomic_exchange_rel (futex, 0);
140   lll_futex_wake (futex, 1);
141 }
142 #define lll_mutex_unlock_force(futex) __lll_mutex_unlock_force(&(futex))
143
144
145 #define lll_mutex_islocked(futex) \
146   (futex != 0)
147
148
149 /* Our internal lock implementation is identical to the binary-compatible
150    mutex implementation. */
151
152 /* Type for lock object.  */
153 typedef int lll_lock_t;
154
155 /* Initializers for lock.  */
156 #define LLL_LOCK_INITIALIZER            (0)
157 #define LLL_LOCK_INITIALIZER_LOCKED     (1)
158
159 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
160
161 /* The states of a lock are:
162     0  -  untaken
163     1  -  taken by one user
164    >1  -  taken by more users */
165
166 #define lll_trylock(lock)       lll_mutex_trylock (lock)
167 #define lll_lock(lock)          lll_mutex_lock (lock)
168 #define lll_unlock(lock)        lll_mutex_unlock (lock)
169 #define lll_islocked(lock)      lll_mutex_islocked (lock)
170
171 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
172    wakeup when the clone terminates.  The memory location contains the
173    thread ID while the clone is running and is reset to zero
174    afterwards.  */
175 #define lll_wait_tid(tid) \
176   do {                                  \
177     __typeof (tid) __tid;               \
178     while ((__tid = (tid)) != 0)        \
179       lll_futex_wait (&(tid), __tid);   \
180   } while (0)
181
182 extern int __lll_timedwait_tid (int *, const struct timespec *)
183      attribute_hidden;
184
185 #define lll_timedwait_tid(tid, abstime) \
186   ({                                                    \
187     int __res = 0;                                      \
188     if ((tid) != 0)                                     \
189       __res = __lll_timedwait_tid (&(tid), (abstime));  \
190     __res;                                              \
191   })
192
193
194 /* Conditional variable handling.  */
195
196 extern void __lll_cond_wait (pthread_cond_t *cond)
197      attribute_hidden;
198 extern int __lll_cond_timedwait (pthread_cond_t *cond,
199                                  const struct timespec *abstime)
200      attribute_hidden;
201 extern void __lll_cond_wake (pthread_cond_t *cond)
202      attribute_hidden;
203 extern void __lll_cond_broadcast (pthread_cond_t *cond)
204      attribute_hidden;
205
206 #define lll_cond_wait(cond) \
207   __lll_cond_wait (cond)
208 #define lll_cond_timedwait(cond, abstime) \
209   __lll_cond_timedwait (cond, abstime)
210 #define lll_cond_wake(cond) \
211   __lll_cond_wake (cond)
212 #define lll_cond_broadcast(cond) \
213   __lll_cond_broadcast (cond)
214
215 #endif  /* lowlevellock.h */