[BZ #6020]
[kopensolaris-gnu/glibc.git] / nptl / sysdeps / unix / sysv / linux / sparc / lowlevellock.h
1 /* Copyright (C) 2003, 2004, 2006, 2007, 2008 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 Libr    \ary; 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 #include <kernel-features.h>
28
29
30 #define FUTEX_WAIT              0
31 #define FUTEX_WAKE              1
32 #define FUTEX_REQUEUE           3
33 #define FUTEX_CMP_REQUEUE       4
34 #define FUTEX_WAKE_OP           5
35 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE   ((4 << 24) | 1)
36 #define FUTEX_LOCK_PI           6
37 #define FUTEX_UNLOCK_PI         7
38 #define FUTEX_TRYLOCK_PI        8
39 #define FUTEX_PRIVATE_FLAG      128
40
41
42 /* Values for 'private' parameter of locking macros.  Yes, the
43    definition seems to be backwards.  But it is not.  The bit will be
44    reversed before passing to the system call.  */
45 #define LLL_PRIVATE     0
46 #define LLL_SHARED      FUTEX_PRIVATE_FLAG
47
48
49 #if !defined NOT_IN_libc || defined IS_IN_rtld
50 /* In libc.so or ld.so all futexes are private.  */
51 # ifdef __ASSUME_PRIVATE_FUTEX
52 #  define __lll_private_flag(fl, private) \
53   ((fl) | FUTEX_PRIVATE_FLAG)
54 # else
55 #  define __lll_private_flag(fl, private) \
56   ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
57 # endif
58 #else
59 # ifdef __ASSUME_PRIVATE_FUTEX
60 #  define __lll_private_flag(fl, private) \
61   (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
62 # else
63 #  define __lll_private_flag(fl, private) \
64   (__builtin_constant_p (private)                                             \
65    ? ((private) == 0                                                          \
66       ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))            \
67       : (fl))                                                                 \
68    : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG)                                \
69               & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
70 # endif       
71 #endif
72
73
74 #define lll_futex_wait(futexp, val, private) \
75   lll_futex_timed_wait (futexp, val, NULL, private)
76
77 #define lll_futex_timed_wait(futexp, val, timespec, private) \
78   ({                                                                          \
79     INTERNAL_SYSCALL_DECL (__err);                                            \
80     long int __ret;                                                           \
81                                                                               \
82     __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp),                      \
83                               __lll_private_flag (FUTEX_WAIT, private),       \
84                               (val), (timespec));                             \
85     __ret;                                                                    \
86   })
87
88 #define lll_futex_wake(futexp, nr, private) \
89   ({                                                                          \
90     INTERNAL_SYSCALL_DECL (__err);                                            \
91     long int __ret;                                                           \
92                                                                               \
93     __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp),                      \
94                               __lll_private_flag (FUTEX_WAKE, private),       \
95                               (nr), 0);                                       \
96     __ret;                                                                    \
97   })
98
99 /* Returns non-zero if error happened, zero if success.  */
100 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
101   ({                                                                          \
102     INTERNAL_SYSCALL_DECL (__err);                                            \
103     long int __ret;                                                           \
104                                                                               \
105     __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp),                      \
106                               __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
107                               (nr_wake), (nr_move), (mutex), (val));          \
108     INTERNAL_SYSCALL_ERROR_P (__ret, __err);                                  \
109   })
110
111 #define lll_robust_dead(futexv, private) \
112   do                                                                          \
113     {                                                                         \
114       int *__futexp = &(futexv);                                              \
115       atomic_or (__futexp, FUTEX_OWNER_DIED);                                 \
116       lll_futex_wake (__futexp, 1, private);                                  \
117     }                                                                         \
118   while (0)
119
120 /* Returns non-zero if error happened, zero if success.  */
121 #ifdef __sparc32_atomic_do_lock
122 /* Avoid FUTEX_WAKE_OP if supporting pre-v9 CPUs.  */
123 # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) 1
124 #else
125 # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
126   ({                                                                          \
127     INTERNAL_SYSCALL_DECL (__err);                                            \
128     long int __ret;                                                           \
129                                                                               \
130     __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp),                      \
131                               __lll_private_flag (FUTEX_WAKE_OP, private),    \
132                               (nr_wake), (nr_wake2), (futexp2),               \
133                               FUTEX_OP_CLEAR_WAKE_IF_GT_ONE);                 \
134     INTERNAL_SYSCALL_ERROR_P (__ret, __err);                                  \
135   })
136 #endif
137
138 static inline int
139 __attribute__ ((always_inline))
140 __lll_trylock (int *futex)
141 {
142   return atomic_compare_and_exchange_val_24_acq (futex, 1, 0) != 0;
143 }
144 #define lll_trylock(futex) __lll_trylock (&(futex))
145
146 static inline int
147 __attribute__ ((always_inline))
148 __lll_cond_trylock (int *futex)
149 {
150   return atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0;
151 }
152 #define lll_cond_trylock(futex) __lll_cond_trylock (&(futex))
153
154 static inline int
155 __attribute__ ((always_inline))
156 __lll_robust_trylock (int *futex, int id)
157 {
158   return atomic_compare_and_exchange_val_acq (futex, id, 0) != 0;
159 }
160 #define lll_robust_trylock(futex, id) \
161   __lll_robust_trylock (&(futex), id)
162
163
164 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
165 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
166 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
167
168 static inline void
169 __attribute__ ((always_inline))
170 __lll_lock (int *futex, int private)
171 {
172   int val = atomic_compare_and_exchange_val_24_acq (futex, 1, 0);
173
174   if (__builtin_expect (val != 0, 0))
175     {
176       if (__builtin_constant_p (private) && private == LLL_PRIVATE)
177         __lll_lock_wait_private (futex);
178       else
179         __lll_lock_wait (futex, private);
180     }
181 }
182 #define lll_lock(futex, private) __lll_lock (&(futex), private)
183
184 static inline int
185 __attribute__ ((always_inline))
186 __lll_robust_lock (int *futex, int id, int private)
187 {
188   int result = 0;
189   if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
190     result = __lll_robust_lock_wait (futex, private);
191   return result;
192 }
193 #define lll_robust_lock(futex, id, private) \
194   __lll_robust_lock (&(futex), id, private)
195
196 static inline void
197 __attribute__ ((always_inline))
198 __lll_cond_lock (int *futex, int private)
199 {
200   int val = atomic_compare_and_exchange_val_24_acq (futex, 2, 0);
201
202   if (__builtin_expect (val != 0, 0))
203     __lll_lock_wait (futex, private);
204 }
205 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
206
207 #define lll_robust_cond_lock(futex, id, private) \
208   __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
209
210
211 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
212                                  int private) attribute_hidden;
213 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
214                                         int private) attribute_hidden;
215
216 static inline int
217 __attribute__ ((always_inline))
218 __lll_timedlock (int *futex, const struct timespec *abstime, int private)
219 {
220   int val = atomic_compare_and_exchange_val_24_acq (futex, 1, 0);
221   int result = 0;
222
223   if (__builtin_expect (val != 0, 0))
224     result = __lll_timedlock_wait (futex, abstime, private);
225   return result;
226 }
227 #define lll_timedlock(futex, abstime, private) \
228   __lll_timedlock (&(futex), abstime, private)
229
230 static inline int
231 __attribute__ ((always_inline))
232 __lll_robust_timedlock (int *futex, const struct timespec *abstime,
233                         int id, int private)
234 {
235   int result = 0;
236   if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
237     result = __lll_robust_timedlock_wait (futex, abstime, private);
238   return result;
239 }
240 #define lll_robust_timedlock(futex, abstime, id, private) \
241   __lll_robust_timedlock (&(futex), abstime, id, private)
242
243 #define lll_unlock(lock, private) \
244   ((void) ({                                                                  \
245     int *__futex = &(lock);                                                   \
246     int __val = atomic_exchange_24_rel (__futex, 0);                          \
247     if (__builtin_expect (__val > 1, 0))                                      \
248       lll_futex_wake (__futex, 1, private);                                   \
249   }))
250
251 #define lll_robust_unlock(lock, private) \
252   ((void) ({                                                                  \
253     int *__futex = &(lock);                                                   \
254     int __val = atomic_exchange_rel (__futex, 0);                             \
255     if (__builtin_expect (__val & FUTEX_WAITERS, 0))                          \
256       lll_futex_wake (__futex, 1, private);                                   \
257   }))
258
259 #define lll_islocked(futex) \
260   (futex != 0)
261
262 /* Initializers for lock.  */
263 #define LLL_LOCK_INITIALIZER            (0)
264 #define LLL_LOCK_INITIALIZER_LOCKED     (1)
265
266 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
267    wakeup when the clone terminates.  The memory location contains the
268    thread ID while the clone is running and is reset to zero
269    afterwards.  */
270 #define lll_wait_tid(tid) \
271   do                                                    \
272     {                                                   \
273       __typeof (tid) __tid;                             \
274       while ((__tid = (tid)) != 0)                      \
275         lll_futex_wait (&(tid), __tid, LLL_SHARED);     \
276     }                                                   \
277   while (0)
278
279 extern int __lll_timedwait_tid (int *, const struct timespec *)
280      attribute_hidden;
281
282 #define lll_timedwait_tid(tid, abstime) \
283   ({                                                    \
284     int __res = 0;                                      \
285     if ((tid) != 0)                                     \
286       __res = __lll_timedwait_tid (&(tid), (abstime));  \
287     __res;                                              \
288   })
289
290 #endif  /* lowlevellock.h */