microblaze: fixup gcc warnings
[uclibc-ng.git] / libpthread / nptl / sysdeps / unix / sysv / linux / microblaze / lowlevellock.h
blob5b06e6fd10aa6357d0f49706456dc80e4c4a9d92
1 /* Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, see <http://www.gnu.org/licenses/>.  */
18 #ifndef _LOWLEVELLOCK_H
19 #define _LOWLEVELLOCK_H 1
21 #include <time.h>
22 #include <sys/param.h>
23 #include <bits/pthreadtypes.h>
24 #include <atomic.h>
25 #include <sysdep.h>
26 #include <bits/kernel-features.h>
28 #define FUTEX_WAIT 0
29 #define FUTEX_WAKE 1
30 #define FUTEX_REQUEUE 3
31 #define FUTEX_CMP_REQUEUE 4
32 #define FUTEX_WAKE_OP 5
33 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
34 #define FUTEX_LOCK_PI 6
35 #define FUTEX_UNLOCK_PI 7
36 #define FUTEX_TRYLOCK_PI 8
37 #define FUTEX_WAIT_BITSET 9
38 #define FUTEX_WAKE_BITSET 10
39 #define FUTEX_PRIVATE_FLAG 128
40 #define FUTEX_CLOCK_REALTIME 256
42 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
44 /* Values for 'private' parameter of locking macros. Yes, the
45 definition seems to be backwards. But it is not. The bit will be
46 reversed before passing to the system call. */
47 #define LLL_PRIVATE 0
48 #define LLL_SHARED FUTEX_PRIVATE_FLAG
51 #if !defined NOT_IN_libc || defined IS_IN_rtld
52 /* In libc.so or ld.so all futexes are private. */
53 # ifdef __ASSUME_PRIVATE_FUTEX
54 # define __lll_private_flag(fl, private) \
55 ((fl) | FUTEX_PRIVATE_FLAG)
56 # else
57 # define __lll_private_flag(fl, private) \
58 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
59 # endif
60 #else
61 # ifdef __ASSUME_PRIVATE_FUTEX
62 # define __lll_private_flag(fl, private) \
63 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
64 # else
65 # define __lll_private_flag(fl, private) \
66 (__builtin_constant_p (private) \
67 ? ((private) == 0 \
68 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
69 : (fl)) \
70 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
71 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
72 # endif
73 #endif
76 #define lll_futex_wait(futexp, val, private) \
77 lll_futex_timed_wait(futexp, val, NULL, private)
79 #define lll_futex_timed_wait(futexp, val, timespec, private) \
80 ({ \
81 long int __sysret; \
82 INTERNAL_SYSCALL_DECL (__err); \
83 __sysret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
84 __lll_private_flag (FUTEX_WAIT, private), \
85 (val), (timespec)); \
86 __sysret; \
89 #define lll_futex_wake(futexp, nr, private) \
90 ({ \
91 long int __sysret; \
92 INTERNAL_SYSCALL_DECL (__err); \
93 __sysret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
94 __lll_private_flag (FUTEX_WAKE, private), \
95 (nr), 0); \
96 __sysret; \
99 #define lll_robust_dead(futexv, private) \
100 do \
102 int *__futexp = &(futexv); \
103 atomic_or (__futexp, FUTEX_OWNER_DIED); \
104 lll_futex_wake (__futexp, 1, private); \
106 while (0)
108 /* Returns non-zero if error happened, zero if success. */
109 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
110 ({ \
111 long int __sysret; \
112 INTERNAL_SYSCALL_DECL (__err); \
113 __sysret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
114 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
115 (nr_wake), (nr_move), (mutex), (val)); \
116 INTERNAL_SYSCALL_ERROR_P (__sysret, __err); \
120 /* Returns non-zero if error happened, zero if success. */
121 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
122 ({ \
123 long int __sysret; \
124 INTERNAL_SYSCALL_DECL (__err); \
125 __sysret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
126 __lll_private_flag (FUTEX_WAKE_OP, private), \
127 (nr_wake), (nr_wake2), (futexp2), \
128 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
129 INTERNAL_SYSCALL_ERROR_P (__sysret, __err); \
133 #define lll_trylock(lock) \
134 atomic_compare_and_exchange_val_acq(&(lock), 1, 0)
136 #define lll_cond_trylock(lock) \
137 atomic_compare_and_exchange_val_acq(&(lock), 2, 0)
139 #define __lll_robust_trylock(futex, id) \
140 (atomic_compare_and_exchange_val_acq (futex, id, 0) != 0)
141 #define lll_robust_trylock(lock, id) \
142 __lll_robust_trylock (&(lock), id)
144 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
145 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
146 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
148 #define __lll_lock(futex, private) \
149 ((void) ({ \
150 int *__futex = (futex); \
151 if (unlikely(atomic_compare_and_exchange_val_acq (__futex, 1, 0))) \
153 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
154 __lll_lock_wait_private (__futex); \
155 else \
156 __lll_lock_wait (__futex, private); \
159 #define lll_lock(futex, private) __lll_lock (&(futex), private)
162 #define __lll_robust_lock(futex, id, private) \
163 ({ \
164 int *__futex = (futex); \
165 int __val = 0; \
167 if (unlikely(atomic_compare_and_exchange_bool_acq (__futex, id, 0))) \
168 __val = __lll_robust_lock_wait (__futex, private); \
169 __val; \
171 #define lll_robust_lock(futex, id, private) \
172 __lll_robust_lock (&(futex), id, private)
175 #define __lll_cond_lock(futex, private) \
176 ((void) ({ \
177 int *__futex = (futex); \
178 if (unlikely(atomic_exchange_acq (__futex, 2))) \
179 __lll_lock_wait (__futex, private); \
181 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
184 #define lll_robust_cond_lock(futex, id, private) \
185 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
188 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
189 int private) attribute_hidden;
190 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
191 int private) attribute_hidden;
193 #define __lll_timedlock(futex, abstime, private) \
194 ({ \
195 int *__futex = (futex); \
196 int __val = 0; \
198 if (unlikely(atomic_exchange_acq (__futex, 1))) \
199 __val = __lll_timedlock_wait (__futex, abstime, private); \
200 __val; \
202 #define lll_timedlock(futex, abstime, private) \
203 __lll_timedlock (&(futex), abstime, private)
206 #define __lll_robust_timedlock(futex, abstime, id, private) \
207 ({ \
208 int *__futex = (futex); \
209 int __val = 0; \
211 if (unlikely(atomic_compare_and_exchange_bool_acq (__futex, id, 0))) \
212 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
213 __val; \
215 #define lll_robust_timedlock(futex, abstime, id, private) \
216 __lll_robust_timedlock (&(futex), abstime, id, private)
219 #define __lll_unlock(futex, private) \
220 (void) \
221 ({ int *__futex = (futex); \
222 int __oldval = atomic_exchange_rel (__futex, 0); \
223 if (unlikely(__oldval > 1)) \
224 lll_futex_wake (__futex, 1, private); \
226 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
229 #define __lll_robust_unlock(futex, private) \
230 (void) \
231 ({ int *__futex = (futex); \
232 int __oldval = atomic_exchange_rel (__futex, 0); \
233 if (unlikely(__oldval & FUTEX_WAITERS)) \
234 lll_futex_wake (__futex, 1, private); \
236 #define lll_robust_unlock(futex, private) \
237 __lll_robust_unlock(&(futex), private)
240 #define lll_islocked(futex) \
241 (futex != 0)
244 /* Our internal lock implementation is identical to the binary-compatible
245 mutex implementation. */
247 /* Initializers for lock. */
248 #define LLL_LOCK_INITIALIZER (0)
249 #define LLL_LOCK_INITIALIZER_LOCKED (1)
251 /* The states of a lock are:
252 0 - untaken
253 1 - taken by one user
254 >1 - taken by more users */
256 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
257 wakeup when the clone terminates. The memory location contains the
258 thread ID while the clone is running and is reset to zero
259 afterwards. */
260 #define lll_wait_tid(tid) \
261 do { \
262 __typeof (tid) __tid; \
263 while ((__tid = (tid)) != 0) \
264 lll_futex_wait (&(tid), __tid, LLL_SHARED);\
265 } while (0)
267 extern int __lll_timedwait_tid (int *, const struct timespec *)
268 attribute_hidden;
270 #define lll_timedwait_tid(tid, abstime) \
271 ({ \
272 int __res = 0; \
273 if ((tid) != 0) \
274 __res = __lll_timedwait_tid (&(tid), (abstime)); \
275 __res; \
278 #endif /* lowlevellock.h */