Introduce time64 support.
[uclibc-ng.git] / libpthread / nptl / sysdeps / unix / sysv / linux / lowlevellock.h
blobe72fe5234810db5bb5d5c80b4399cab4867f8774
1 /* Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3 The GNU C Library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
8 The GNU C Library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
13 You should have received a copy of the GNU Lesser General Public
14 License along with the GNU C Library; if not, see
15 <http://www.gnu.org/licenses/>. */
17 #ifndef _LOWLEVELLOCK_H
18 #define _LOWLEVELLOCK_H 1
20 #include <time.h>
21 #include <sys/param.h>
22 #include <bits/pthreadtypes.h>
23 #include <atomic.h>
24 #include <sysdep.h>
25 #include <bits/kernel-features.h>
27 #define FUTEX_WAIT 0
28 #define FUTEX_WAKE 1
29 #define FUTEX_REQUEUE 3
30 #define FUTEX_CMP_REQUEUE 4
31 #define FUTEX_WAKE_OP 5
32 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
33 #define FUTEX_LOCK_PI 6
34 #define FUTEX_UNLOCK_PI 7
35 #define FUTEX_TRYLOCK_PI 8
36 #define FUTEX_WAIT_BITSET 9
37 #define FUTEX_WAKE_BITSET 10
38 #define FUTEX_PRIVATE_FLAG 128
39 #define FUTEX_CLOCK_REALTIME 256
41 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
43 /* Values for 'private' parameter of locking macros. Yes, the
44 definition seems to be backwards. But it is not. The bit will be
45 reversed before passing to the system call. */
46 #define LLL_PRIVATE 0
47 #define LLL_SHARED FUTEX_PRIVATE_FLAG
50 #if !defined NOT_IN_libc || defined IS_IN_rtld
51 /* In libc.so or ld.so all futexes are private. */
52 # ifdef __ASSUME_PRIVATE_FUTEX
53 # define __lll_private_flag(fl, private) \
54 ((fl) | FUTEX_PRIVATE_FLAG)
55 # else
56 # define __lll_private_flag(fl, private) \
57 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
58 # endif
59 #else
60 # ifdef __ASSUME_PRIVATE_FUTEX
61 # define __lll_private_flag(fl, private) \
62 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
63 # else
64 # define __lll_private_flag(fl, private) \
65 (__builtin_constant_p (private) \
66 ? ((private) == 0 \
67 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
68 : (fl)) \
69 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
70 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
71 # endif
72 #endif
74 #define lll_futex_wait(futexp, val, private) \
75 lll_futex_timed_wait(futexp, val, NULL, private)
77 #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
79 #define lll_futex_timed_wait(futexp, val, timespec, private) \
80 ({ \
81 INTERNAL_SYSCALL_DECL (__err); \
82 long int __ret; \
83 __ret = INTERNAL_SYSCALL (futex_time64, __err, 4, (futexp), \
84 __lll_private_flag (FUTEX_WAIT, private), \
85 (val), (timespec)); \
86 __ret; \
89 #define lll_futex_wake(futexp, nr, private) \
90 ({ \
91 INTERNAL_SYSCALL_DECL (__err); \
92 long int __ret; \
93 __ret = INTERNAL_SYSCALL (futex_time64, __err, 4, (futexp), \
94 __lll_private_flag (FUTEX_WAKE, private), \
95 (nr), 0); \
96 __ret; \
99 #else
101 #define lll_futex_timed_wait(futexp, val, timespec, private) \
102 ({ \
103 INTERNAL_SYSCALL_DECL (__err); \
104 long int __ret; \
105 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
106 __lll_private_flag (FUTEX_WAIT, private), \
107 (val), (timespec)); \
108 __ret; \
111 #define lll_futex_wake(futexp, nr, private) \
112 ({ \
113 INTERNAL_SYSCALL_DECL (__err); \
114 long int __ret; \
115 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
116 __lll_private_flag (FUTEX_WAKE, private), \
117 (nr), 0); \
118 __ret; \
121 #endif
123 #define lll_robust_dead(futexv, private) \
124 do \
126 int *__futexp = &(futexv); \
127 atomic_or (__futexp, FUTEX_OWNER_DIED); \
128 lll_futex_wake (__futexp, 1, private); \
130 while (0)
132 /* Returns non-zero if error happened, zero if success. */
134 #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
136 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
137 ({ \
138 INTERNAL_SYSCALL_DECL (__err); \
139 long int __ret; \
140 __ret = INTERNAL_SYSCALL (futex_time64, __err, 6, (futexp), \
141 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
142 (nr_wake), (nr_move), (mutex), (val)); \
143 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
147 /* Returns non-zero if error happened, zero if success. */
148 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
149 ({ \
150 INTERNAL_SYSCALL_DECL (__err); \
151 long int __ret; \
152 __ret = INTERNAL_SYSCALL (futex_time64, __err, 6, (futexp), \
153 __lll_private_flag (FUTEX_WAKE_OP, private), \
154 (nr_wake), (nr_wake2), (futexp2), \
155 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
156 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
159 #else
162 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
163 ({ \
164 INTERNAL_SYSCALL_DECL (__err); \
165 long int __ret; \
166 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
167 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
168 (nr_wake), (nr_move), (mutex), (val)); \
169 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
173 /* Returns non-zero if error happened, zero if success. */
174 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
175 ({ \
176 INTERNAL_SYSCALL_DECL (__err); \
177 long int __ret; \
178 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
179 __lll_private_flag (FUTEX_WAKE_OP, private), \
180 (nr_wake), (nr_wake2), (futexp2), \
181 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
182 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
185 #endif
188 #define lll_trylock(lock) \
189 atomic_compare_and_exchange_val_acq(&(lock), 1, 0)
191 #define lll_cond_trylock(lock) \
192 atomic_compare_and_exchange_val_acq(&(lock), 2, 0)
194 #define __lll_robust_trylock(futex, id) \
195 (atomic_compare_and_exchange_val_acq (futex, id, 0) != 0)
196 #define lll_robust_trylock(lock, id) \
197 __lll_robust_trylock (&(lock), id)
199 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
200 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
201 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
203 #define __lll_lock(futex, private) \
204 ((void) ({ \
205 int *__futex = (futex); \
206 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, \
207 1, 0), 0)) \
209 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
210 __lll_lock_wait_private (__futex); \
211 else \
212 __lll_lock_wait (__futex, private); \
215 #define lll_lock(futex, private) __lll_lock (&(futex), private)
218 #define __lll_robust_lock(futex, id, private) \
219 ({ \
220 int *__futex = (futex); \
221 int __val = 0; \
223 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
224 0), 0)) \
225 __val = __lll_robust_lock_wait (__futex, private); \
226 __val; \
228 #define lll_robust_lock(futex, id, private) \
229 __lll_robust_lock (&(futex), id, private)
232 #define __lll_cond_lock(futex, private) \
233 ((void) ({ \
234 int *__futex = (futex); \
235 if (__builtin_expect (atomic_exchange_acq (__futex, 2), 0)) \
236 __lll_lock_wait (__futex, private); \
238 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
241 #define lll_robust_cond_lock(futex, id, private) \
242 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
245 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
246 int private) attribute_hidden;
247 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
248 int private) attribute_hidden;
250 #define __lll_timedlock(futex, abstime, private) \
251 ({ \
252 int *__futex = (futex); \
253 int __val = 0; \
255 if (__builtin_expect (atomic_exchange_acq (__futex, 1), 0)) \
256 __val = __lll_timedlock_wait (__futex, abstime, private); \
257 __val; \
259 #define lll_timedlock(futex, abstime, private) \
260 __lll_timedlock (&(futex), abstime, private)
263 #define __lll_robust_timedlock(futex, abstime, id, private) \
264 ({ \
265 int *__futex = (futex); \
266 int __val = 0; \
268 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
269 0), 0)) \
270 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
271 __val; \
273 #define lll_robust_timedlock(futex, abstime, id, private) \
274 __lll_robust_timedlock (&(futex), abstime, id, private)
277 #define __lll_unlock(futex, private) \
278 (void) \
279 ({ int *__futex = (futex); \
280 int __oldval = atomic_exchange_rel (__futex, 0); \
281 if (__builtin_expect (__oldval > 1, 0)) \
282 lll_futex_wake (__futex, 1, private); \
284 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
287 #define __lll_robust_unlock(futex, private) \
288 (void) \
289 ({ int *__futex = (futex); \
290 int __oldval = atomic_exchange_rel (__futex, 0); \
291 if (__builtin_expect (__oldval & FUTEX_WAITERS, 0)) \
292 lll_futex_wake (__futex, 1, private); \
294 #define lll_robust_unlock(futex, private) \
295 __lll_robust_unlock(&(futex), private)
298 #define lll_islocked(futex) \
299 (futex != 0)
302 /* Our internal lock implementation is identical to the binary-compatible
303 mutex implementation. */
305 /* Initializers for lock. */
306 #define LLL_LOCK_INITIALIZER (0)
307 #define LLL_LOCK_INITIALIZER_LOCKED (1)
309 /* The states of a lock are:
310 0 - untaken
311 1 - taken by one user
312 >1 - taken by more users */
314 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
315 wakeup when the clone terminates. The memory location contains the
316 thread ID while the clone is running and is reset to zero
317 afterwards. */
318 #define lll_wait_tid(tid) \
319 do { \
320 __typeof (tid) __tid; \
321 while ((__tid = (tid)) != 0) \
322 lll_futex_wait (&(tid), __tid, LLL_SHARED);\
323 } while (0)
325 extern int __lll_timedwait_tid (int *, const struct timespec *)
326 attribute_hidden;
328 #define lll_timedwait_tid(tid, abstime) \
329 ({ \
330 int __res = 0; \
331 if ((tid) != 0) \
332 __res = __lll_timedwait_tid (&(tid), (abstime)); \
333 __res; \
336 #endif /* lowlevellock.h */