Add Changelog ...
[glibc.git] / sysdeps / unix / sysv / linux / arm / nptl / lowlevellock.h
blobb9fa69ce37eb65e72003381b538376e9420c0b86
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. If not, see
16 <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 <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 INTERNAL_SYSCALL_DECL (__err); \
82 long int __ret; \
83 __ret = INTERNAL_SYSCALL (futex, __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, __err, 4, (futexp), \
94 __lll_private_flag (FUTEX_WAKE, private), \
95 (nr), 0); \
96 __ret; \
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 INTERNAL_SYSCALL_DECL (__err); \
112 long int __ret; \
113 __ret = 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 (__ret, __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 INTERNAL_SYSCALL_DECL (__err); \
124 long int __ret; \
125 __ret = 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 (__ret, __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 (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, \
152 1, 0), 0)) \
154 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
155 __lll_lock_wait_private (__futex); \
156 else \
157 __lll_lock_wait (__futex, private); \
160 #define lll_lock(futex, private) __lll_lock (&(futex), private)
163 #define __lll_robust_lock(futex, id, private) \
164 ({ \
165 int *__futex = (futex); \
166 int __val = 0; \
168 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
169 0), 0)) \
170 __val = __lll_robust_lock_wait (__futex, private); \
171 __val; \
173 #define lll_robust_lock(futex, id, private) \
174 __lll_robust_lock (&(futex), id, private)
177 #define __lll_cond_lock(futex, private) \
178 ((void) ({ \
179 int *__futex = (futex); \
180 if (__builtin_expect (atomic_exchange_acq (__futex, 2), 0)) \
181 __lll_lock_wait (__futex, private); \
183 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
186 #define lll_robust_cond_lock(futex, id, private) \
187 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
190 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
191 int private) attribute_hidden;
192 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
193 int private) attribute_hidden;
195 #define __lll_timedlock(futex, abstime, private) \
196 ({ \
197 int *__futex = (futex); \
198 int __val = 0; \
200 if (__builtin_expect (atomic_exchange_acq (__futex, 1), 0)) \
201 __val = __lll_timedlock_wait (__futex, abstime, private); \
202 __val; \
204 #define lll_timedlock(futex, abstime, private) \
205 __lll_timedlock (&(futex), abstime, private)
208 #define __lll_robust_timedlock(futex, abstime, id, private) \
209 ({ \
210 int *__futex = (futex); \
211 int __val = 0; \
213 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
214 0), 0)) \
215 __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
216 __val; \
218 #define lll_robust_timedlock(futex, abstime, id, private) \
219 __lll_robust_timedlock (&(futex), abstime, id, private)
222 #define __lll_unlock(futex, private) \
223 (void) \
224 ({ int *__futex = (futex); \
225 int __oldval = atomic_exchange_rel (__futex, 0); \
226 if (__builtin_expect (__oldval > 1, 0)) \
227 lll_futex_wake (__futex, 1, private); \
229 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
232 #define __lll_robust_unlock(futex, private) \
233 (void) \
234 ({ int *__futex = (futex); \
235 int __oldval = atomic_exchange_rel (__futex, 0); \
236 if (__builtin_expect (__oldval & FUTEX_WAITERS, 0)) \
237 lll_futex_wake (__futex, 1, private); \
239 #define lll_robust_unlock(futex, private) \
240 __lll_robust_unlock(&(futex), private)
243 #define lll_islocked(futex) \
244 (futex != 0)
247 /* Our internal lock implementation is identical to the binary-compatible
248 mutex implementation. */
250 /* Initializers for lock. */
251 #define LLL_LOCK_INITIALIZER (0)
252 #define LLL_LOCK_INITIALIZER_LOCKED (1)
254 /* The states of a lock are:
255 0 - untaken
256 1 - taken by one user
257 >1 - taken by more users */
259 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
260 wakeup when the clone terminates. The memory location contains the
261 thread ID while the clone is running and is reset to zero
262 afterwards. */
263 #define lll_wait_tid(tid) \
264 do { \
265 __typeof (tid) __tid; \
266 while ((__tid = (tid)) != 0) \
267 lll_futex_wait (&(tid), __tid, LLL_SHARED);\
268 } while (0)
270 extern int __lll_timedwait_tid (int *, const struct timespec *)
271 attribute_hidden;
273 #define lll_timedwait_tid(tid, abstime) \
274 ({ \
275 int __res = 0; \
276 if ((tid) != 0) \
277 __res = __lll_timedwait_tid (&(tid), (abstime)); \
278 __res; \
281 #endif /* lowlevellock.h */