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