2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / unix / sysv / linux / alpha / lowlevellock.h
blob93188234cb23a1e244c136397df15ecd6535daef
1 /* Copyright (C) 2003, 2004, 2006, 2007 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 Libr \ary; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
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 __NR_futex 394
31 #define FUTEX_WAIT 0
32 #define FUTEX_WAKE 1
33 #define FUTEX_REQUEUE 3
34 #define FUTEX_CMP_REQUEUE 4
35 #define FUTEX_WAKE_OP 5
36 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
37 #define FUTEX_LOCK_PI 6
38 #define FUTEX_UNLOCK_PI 7
39 #define FUTEX_TRYLOCK_PI 8
40 #define FUTEX_PRIVATE_FLAG 128
42 /* Values for 'private' parameter of locking macros. Yes, the
43 definition seems to be backwards. But it is not. The bit will be
44 reversed before passing to the system call. */
45 #define LLL_PRIVATE 0
46 #define LLL_SHARED FUTEX_PRIVATE_FLAG
49 #if !defined NOT_IN_libc || defined IS_IN_rtld
50 /* In libc.so or ld.so all futexes are private. */
51 # ifdef __ASSUME_PRIVATE_FUTEX
52 # define __lll_private_flag(fl, private) \
53 ((fl) | FUTEX_PRIVATE_FLAG)
54 # else
55 # define __lll_private_flag(fl, private) \
56 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
57 # endif
58 #else
59 # ifdef __ASSUME_PRIVATE_FUTEX
60 # define __lll_private_flag(fl, private) \
61 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
62 # else
63 # define __lll_private_flag(fl, private) \
64 (__builtin_constant_p (private) \
65 ? ((private) == 0 \
66 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
67 : (fl)) \
68 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
69 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
70 # endif
71 #endif
74 #define lll_futex_wait(futexp, val, private) \
75 lll_futex_timed_wait (futexp, val, NULL, private)
77 #define lll_futex_timed_wait(futexp, val, timespec, private) \
78 ({ \
79 INTERNAL_SYSCALL_DECL (__err); \
80 long int __ret; \
81 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
82 __lll_private_flag (FUTEX_WAIT, private), \
83 (val), (timespec)); \
84 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret; \
87 #define lll_futex_wake(futexp, nr, private) \
88 ({ \
89 INTERNAL_SYSCALL_DECL (__err); \
90 long int __ret; \
91 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
92 __lll_private_flag (FUTEX_WAKE, private), \
93 (nr), 0); \
94 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret; \
97 #define lll_robust_dead(futexv, private) \
98 do \
99 { \
100 int *__futexp = &(futexv); \
101 atomic_or (__futexp, FUTEX_OWNER_DIED); \
102 lll_futex_wake (__futexp, 1, private); \
104 while (0)
106 /* Returns non-zero if error happened, zero if success. */
107 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
108 ({ \
109 INTERNAL_SYSCALL_DECL (__err); \
110 long int __ret; \
111 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
112 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
113 (nr_wake), (nr_move), (mutex), (val)); \
114 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
117 /* Returns non-zero if error happened, zero if success. */
118 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
119 ({ \
120 INTERNAL_SYSCALL_DECL (__err); \
121 long int __ret; \
122 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
123 __lll_private_flag (FUTEX_WAKE_OP, private), \
124 (nr_wake), (nr_wake2), (futexp2), \
125 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
126 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
132 static inline int __attribute__((always_inline))
133 __lll_trylock(int *futex)
135 return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
137 #define lll_trylock(lock) __lll_trylock (&(lock))
140 static inline int __attribute__((always_inline))
141 __lll_cond_trylock(int *futex)
143 return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
145 #define lll_cond_trylock(lock) __lll_cond_trylock (&(lock))
148 static inline int __attribute__((always_inline))
149 __lll_robust_trylock(int *futex, int id)
151 return atomic_compare_and_exchange_val_acq (futex, id, 0) != 0;
153 #define lll_robust_trylock(lock, id) \
154 __lll_robust_trylock (&(lock), id)
156 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
157 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
158 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
160 static inline void __attribute__((always_inline))
161 __lll_lock(int *futex, int private)
163 if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
165 if (__builtin_constant_p (private) && private == LLL_PRIVATE)
166 __lll_lock_wait_private (futex);
167 else
168 __lll_lock_wait (futex, private);
171 #define lll_lock(futex, private) __lll_lock (&(futex), private)
174 static inline int __attribute__ ((always_inline))
175 __lll_robust_lock (int *futex, int id, int private)
177 int result = 0;
178 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
179 result = __lll_robust_lock_wait (futex, private);
180 return result;
182 #define lll_robust_lock(futex, id, private) \
183 __lll_robust_lock (&(futex), id, private)
186 static inline void __attribute__ ((always_inline))
187 __lll_cond_lock (int *futex, int private)
189 if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0)
190 __lll_lock_wait (futex, private);
192 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
195 #define lll_robust_cond_lock(futex, id, private) \
196 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
199 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
200 int private) attribute_hidden;
201 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
202 int private) attribute_hidden;
204 static inline int __attribute__ ((always_inline))
205 __lll_timedlock (int *futex, const struct timespec *abstime, int private)
207 int result = 0;
208 if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
209 result = __lll_timedlock_wait (futex, abstime, private);
210 return result;
212 #define lll_timedlock(futex, abstime, private) \
213 __lll_timedlock (&(futex), abstime, private)
216 static inline int __attribute__ ((always_inline))
217 __lll_robust_timedlock (int *futex, const struct timespec *abstime,
218 int id, int private)
220 int result = 0;
221 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
222 result = __lll_robust_timedlock_wait (futex, abstime, private);
223 return result;
225 #define lll_robust_timedlock(futex, abstime, id, private) \
226 __lll_robust_timedlock (&(futex), abstime, id, private)
229 #define __lll_unlock(futex, private) \
230 (void) \
231 ({ int *__futex = (futex); \
232 int __oldval = atomic_exchange_rel (__futex, 0); \
233 if (__builtin_expect (__oldval > 1, 0)) \
234 lll_futex_wake (__futex, 1, private); \
236 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
239 #define __lll_robust_unlock(futex, private) \
240 (void) \
241 ({ int *__futex = (futex); \
242 int __oldval = atomic_exchange_rel (__futex, 0); \
243 if (__builtin_expect (__oldval & FUTEX_WAITERS, 0)) \
244 lll_futex_wake (__futex, 1, private); \
246 #define lll_robust_unlock(futex, private) \
247 __lll_robust_unlock(&(futex), private)
250 #define lll_islocked(futex) \
251 (futex != 0)
253 /* Initializers for lock. */
254 #define LLL_LOCK_INITIALIZER (0)
255 #define LLL_LOCK_INITIALIZER_LOCKED (1)
258 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
259 wakeup when the clone terminates. The memory location contains the
260 thread ID while the clone is running and is reset to zero
261 afterwards. */
262 #define lll_wait_tid(tid) \
263 do { \
264 __typeof (tid) __tid; \
265 while ((__tid = (tid)) != 0) \
266 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
267 } while (0)
269 extern int __lll_timedwait_tid (int *, const struct timespec *)
270 attribute_hidden;
272 #define lll_timedwait_tid(tid, abstime) \
273 ({ \
274 int __res = 0; \
275 if ((tid) != 0) \
276 __res = __lll_timedwait_tid (&(tid), (abstime)); \
277 __res; \
280 #endif /* lowlevellock.h */