Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / microblaze / lowlevellock.h
blob57a2fd1416ca7ae40ae019d1c035472d6e974fc6
1 /* Copyright (C) 2005-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
22 #include <time.h>
23 #include <sys/param.h>
24 #include <bits/pthreadtypes.h>
25 #include <atomic.h>
26 #include <sysdep.h>
27 #include <kernel-features.h>
28 #include <tls.h> /* Need THREAD_*, and header.*. */
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_WAIT_BITSET 9
40 #define FUTEX_WAKE_BITSET 10
41 #define FUTEX_WAIT_REQUEUE_PI 11
42 #define FUTEX_CMP_REQUEUE_PI 12
43 #define FUTEX_PRIVATE_FLAG 128
44 #define FUTEX_CLOCK_REALTIME 256
46 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
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
55 #if IS_IN (libc) || 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
80 #define lll_futex_wait(futexp, val, private) \
81 lll_futex_timed_wait (futexp, val, NULL, private)
83 #define lll_futex_timed_wait(futexp, val, timespec, private) \
84 ({ \
85 INTERNAL_SYSCALL_DECL (__err); \
86 long int __ret; \
87 __ret = INTERNAL_SYSCALL (futex, __err, 4, (long) (futexp), \
88 __lll_private_flag (FUTEX_WAIT, private), \
89 (val), (timespec)); \
90 __ret; \
93 #define lll_futex_timed_wait_bitset(futexp, val, timespec, clockbit, private) \
94 ({ \
95 INTERNAL_SYSCALL_DECL (__err); \
96 long int __ret; \
97 int __op = FUTEX_WAIT_BITSET | clockbit; \
98 __ret = INTERNAL_SYSCALL (futex, __err, 6, (long) (futexp), \
99 __lll_private_flag (__op, private), \
100 (val), (timespec), NULL /* Unused. */, \
101 FUTEX_BITSET_MATCH_ANY); \
102 __ret; \
105 #define lll_futex_wake(futexp, nr, private) \
106 ({ \
107 INTERNAL_SYSCALL_DECL (__err); \
108 long int __ret; \
109 __ret = INTERNAL_SYSCALL (futex, __err, 4, (long) (futexp), \
110 __lll_private_flag (FUTEX_WAKE, private), \
111 (nr), 0); \
112 __ret; \
115 /* Returns non-zero if error happened, zero if success. */
116 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
117 ({ \
118 INTERNAL_SYSCALL_DECL (__err); \
119 long int __ret; \
120 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
121 __lll_private_flag (FUTEX_CMP_REQUEUE, private), \
122 (nr_wake), (nr_move), (mutex), (val)); \
123 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
127 /* Returns non-zero if error happened, zero if success. */
128 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
129 ({ \
130 INTERNAL_SYSCALL_DECL (__err); \
131 long int __ret; \
133 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
134 __lll_private_flag (FUTEX_WAKE_OP, private), \
135 (nr_wake), (nr_wake2), (futexp2), \
136 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
137 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
140 /* Priority Inheritance support. */
141 #define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
142 lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
144 #define lll_futex_timed_wait_requeue_pi(futexp, val, timespec, clockbit, \
145 mutex, private) \
146 ({ \
147 INTERNAL_SYSCALL_DECL (__err); \
148 long int __ret; \
149 int __op = FUTEX_WAIT_REQUEUE_PI | clockbit; \
151 __ret = INTERNAL_SYSCALL (futex, __err, 5, (futexp), \
152 __lll_private_flag (__op, private), \
153 (val), (timespec), mutex); \
154 __ret; \
157 #define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, val, priv) \
158 ({ \
159 INTERNAL_SYSCALL_DECL (__err); \
160 long int __ret; \
162 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
163 __lll_private_flag (FUTEX_CMP_REQUEUE_PI, priv), \
164 (nr_wake), (nr_move), (mutex), (val)); \
165 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
168 #define lll_trylock(lock) \
169 atomic_compare_and_exchange_val_acq(&(lock), 1, 0)
171 #define lll_cond_trylock(lock) \
172 atomic_compare_and_exchange_val_acq(&(lock), 2, 0)
174 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
175 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
176 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
178 #define __lll_lock(futex, private) \
179 ((void) ({ \
180 int *__futex = (futex); \
181 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0), \
182 0) != 0) \
184 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
185 __lll_lock_wait_private (__futex); \
186 else \
187 __lll_lock_wait (__futex, private); \
190 #define lll_lock(futex, private) __lll_lock (&(futex), private)
192 #define __lll_robust_lock(futex, id, private) \
193 ({ \
194 int *__futex = (futex); \
195 int __val = 0; \
197 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
198 0), 0)) \
199 __val = __lll_robust_lock_wait (__futex, private); \
200 __val; \
202 #define lll_robust_lock(futex, id, private) \
203 __lll_robust_lock (&(futex), id, private)
205 static inline void __attribute__ ((always_inline))
206 __lll_cond_lock (int *futex, int private)
208 if (__builtin_expect (atomic_exchange_acq (futex, 2), 0))
209 __lll_lock_wait (futex, private);
211 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
213 #define lll_robust_cond_lock(futex, id, private) \
214 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
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;
222 static inline int __attribute__ ((always_inline))
223 __lll_timedlock (int *futex, const struct timespec *abstime, int private)
225 int result = 0;
226 if (__builtin_expect (atomic_compare_and_exchange_val_acq (futex, 1, 0), 0) != 0)
227 result = __lll_timedlock_wait (futex, abstime, private);
228 return result;
230 #define lll_timedlock(futex, abstime, private) \
231 __lll_timedlock (&(futex), abstime, private)
233 static inline int __attribute__ ((always_inline))
234 __lll_robust_timedlock (int *futex, const struct timespec *abstime,
235 int id, int private)
237 int result = 0;
238 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (futex, id, 0), 0))
239 result = __lll_robust_timedlock_wait (futex, abstime, private);
240 return result;
242 #define lll_robust_timedlock(futex, abstime, id, private) \
243 __lll_robust_timedlock (&(futex), abstime, id, private)
245 #define __lll_unlock(futex, private) \
246 ((void) ({ \
247 int *__futex = (futex); \
248 int __val = atomic_exchange_rel (__futex, 0); \
250 if (__builtin_expect (__val > 1, 0)) \
251 lll_futex_wake (__futex, 1, private); \
253 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
255 #define __lll_robust_unlock(futex, private) \
256 ((void) ({ \
257 int *__futex = (futex); \
258 int __val = atomic_exchange_rel (__futex, 0); \
260 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
261 lll_futex_wake (__futex, 1, private); \
263 #define lll_robust_unlock(futex, private) \
264 __lll_robust_unlock(&(futex), private)
266 #define lll_islocked(futex) \
267 (futex != 0)
269 /* Our internal lock implementation is identical to the binary-compatible
270 mutex implementation. */
272 /* Initializers for lock. */
273 #define LLL_LOCK_INITIALIZER (0)
274 #define LLL_LOCK_INITIALIZER_LOCKED (1)
276 /* The states of a lock are:
277 0 - untaken
278 1 - taken by one user
279 >1 - taken by more users. */
281 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
282 wakeup when the clone terminates. The memory location contains the
283 thread ID while the clone is running and is reset to zero
284 afterwards. */
285 #define lll_wait_tid(tid) \
286 do { \
287 __typeof (tid) __tid; \
288 while ((__tid = (tid)) != 0) \
289 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
290 } while (0)
292 extern int __lll_timedwait_tid (int *, const struct timespec *)
293 attribute_hidden;
295 #define lll_timedwait_tid(tid, abstime) \
296 ({ \
297 int __res = 0; \
298 if ((tid) != 0) \
299 __res = __lll_timedwait_tid (&(tid), (abstime)); \
300 __res; \
303 #endif /* lowlevellock.h. */