Update copyright notices with scripts/update-copyrights.
[glibc.git] / ports / sysdeps / unix / sysv / linux / tile / nptl / lowlevellock.h
blobe6f5d1905ecb08274a0ddd878ff82a79eb04f891
1 /* Copyright (C) 2011-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
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>
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_PRIVATE_FLAG 128
42 #define FUTEX_CLOCK_REALTIME 256
44 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
46 /* Values for 'private' parameter of locking macros. Yes, the
47 definition seems to be backwards. But it is not. The bit will be
48 reversed before passing to the system call. */
49 #define LLL_PRIVATE 0
50 #define LLL_SHARED FUTEX_PRIVATE_FLAG
53 #if !defined NOT_IN_libc || defined IS_IN_rtld
54 /* In libc.so or ld.so all futexes are private. */
55 # ifdef __ASSUME_PRIVATE_FUTEX
56 # define __lll_private_flag(fl, private) \
57 ((fl) | FUTEX_PRIVATE_FLAG)
58 # else
59 # define __lll_private_flag(fl, private) \
60 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
61 # endif
62 #else
63 # ifdef __ASSUME_PRIVATE_FUTEX
64 # define __lll_private_flag(fl, private) \
65 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
66 # else
67 # define __lll_private_flag(fl, private) \
68 (__builtin_constant_p (private) \
69 ? ((private) == 0 \
70 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
71 : (fl)) \
72 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
73 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
74 # endif
75 #endif
78 #define lll_futex_wait(futexp, val, private) \
79 lll_futex_timed_wait (futexp, val, NULL, private)
81 #define lll_futex_timed_wait(futexp, val, timespec, private) \
82 ({ \
83 INTERNAL_SYSCALL_DECL (__err); \
84 INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
85 __lll_private_flag (FUTEX_WAIT, private), \
86 (val), (timespec)); \
89 #define lll_futex_timed_wait_bitset(futexp, val, timespec, clockbit, private) \
90 ({ \
91 INTERNAL_SYSCALL_DECL (__err); \
92 long int __ret; \
93 int __op = FUTEX_WAIT_BITSET | clockbit; \
95 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
96 __lll_private_flag (__op, private), \
97 (val), (timespec), NULL /* Unused. */, \
98 FUTEX_BITSET_MATCH_ANY); \
99 __ret; \
102 #define lll_futex_wake(futexp, nr, private) \
103 ({ \
104 INTERNAL_SYSCALL_DECL (__err); \
105 INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
106 __lll_private_flag (FUTEX_WAKE, private), \
107 (nr), 0); \
110 #define lll_robust_dead(futexv, private) \
111 do \
113 int *__futexp = &(futexv); \
114 atomic_or (__futexp, FUTEX_OWNER_DIED); \
115 lll_futex_wake (__futexp, 1, private); \
117 while (0)
119 /* Returns non-zero if error happened, zero if success. */
120 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
121 ({ \
122 INTERNAL_SYSCALL_DECL (__err); \
123 long int __ret; \
124 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
125 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
126 (nr_wake), (nr_move), (mutex), (val)); \
127 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
130 /* Returns non-zero if error happened, zero if success. */
131 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
132 ({ \
133 INTERNAL_SYSCALL_DECL (__err); \
134 long int __ret; \
135 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
136 __lll_private_flag (FUTEX_WAKE_OP, private), \
137 (nr_wake), (nr_wake2), (futexp2), \
138 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
139 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
145 static inline int __attribute__ ((always_inline))
146 __lll_trylock (int *futex)
148 return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
150 #define lll_trylock(lock) __lll_trylock (&(lock))
153 static inline int __attribute__ ((always_inline))
154 __lll_cond_trylock (int *futex)
156 return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
158 #define lll_cond_trylock(lock) __lll_cond_trylock (&(lock))
161 static inline int __attribute__ ((always_inline))
162 __lll_robust_trylock (int *futex, int id)
164 return atomic_compare_and_exchange_val_acq (futex, id, 0) != 0;
166 #define lll_robust_trylock(lock, id) \
167 __lll_robust_trylock (&(lock), id)
169 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
170 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
171 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
173 static inline void __attribute__ ((always_inline))
174 __lll_lock (int *futex, int private)
176 if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
178 if (__builtin_constant_p (private) && private == LLL_PRIVATE)
179 __lll_lock_wait_private (futex);
180 else
181 __lll_lock_wait (futex, private);
184 #define lll_lock(futex, private) __lll_lock (&(futex), private)
187 static inline int __attribute__ ((always_inline))
188 __lll_robust_lock (int *futex, int id, int private)
190 int result = 0;
191 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
192 result = __lll_robust_lock_wait (futex, private);
193 return result;
195 #define lll_robust_lock(futex, id, private) \
196 __lll_robust_lock (&(futex), id, private)
199 static inline void __attribute__ ((always_inline))
200 __lll_cond_lock (int *futex, int private)
202 if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0)
203 __lll_lock_wait (futex, private);
205 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
208 #define lll_robust_cond_lock(futex, id, private) \
209 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
212 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
213 int private) attribute_hidden;
214 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
215 int private) attribute_hidden;
217 static inline int __attribute__ ((always_inline))
218 __lll_timedlock (int *futex, const struct timespec *abstime, int private)
220 int result = 0;
221 if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
222 result = __lll_timedlock_wait (futex, abstime, private);
223 return result;
225 #define lll_timedlock(futex, abstime, private) \
226 __lll_timedlock (&(futex), abstime, private)
229 static inline int __attribute__ ((always_inline))
230 __lll_robust_timedlock (int *futex, const struct timespec *abstime,
231 int id, int private)
233 int result = 0;
234 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
235 result = __lll_robust_timedlock_wait (futex, abstime, private);
236 return result;
238 #define lll_robust_timedlock(futex, abstime, id, private) \
239 __lll_robust_timedlock (&(futex), abstime, id, private)
242 #define __lll_unlock(futex, private) \
243 (void) \
244 ({ int *__futex = (futex); \
245 int __oldval = atomic_exchange_rel (__futex, 0); \
246 if (__builtin_expect (__oldval > 1, 0)) \
247 lll_futex_wake (__futex, 1, private); \
249 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
252 #define __lll_robust_unlock(futex, private) \
253 (void) \
254 ({ int *__futex = (futex); \
255 int __oldval = atomic_exchange_rel (__futex, 0); \
256 if (__builtin_expect (__oldval & FUTEX_WAITERS, 0)) \
257 lll_futex_wake (__futex, 1, private); \
259 #define lll_robust_unlock(futex, private) \
260 __lll_robust_unlock(&(futex), private)
263 #define lll_islocked(futex) \
264 (futex != 0)
266 /* Initializers for lock. */
267 #define LLL_LOCK_INITIALIZER (0)
268 #define LLL_LOCK_INITIALIZER_LOCKED (1)
271 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
272 wakeup when the clone terminates. The memory location contains the
273 thread ID while the clone is running and is reset to zero
274 afterwards. */
275 #define lll_wait_tid(tid) \
276 do { \
277 __typeof (tid) __tid; \
278 while ((__tid = (tid)) != 0) \
279 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
280 } while (0)
282 extern int __lll_timedwait_tid (int *, const struct timespec *)
283 attribute_hidden;
285 #define lll_timedwait_tid(tid, abstime) \
286 ({ \
287 int __res = 0; \
288 if ((tid) != 0) \
289 __res = __lll_timedwait_tid (&(tid), (abstime)); \
290 __res; \
293 #endif /* lowlevellock.h */