Move all files into ports/ subdirectory in preparation for merge with glibc
[glibc.git] / ports / sysdeps / unix / sysv / linux / alpha / nptl / lowlevellock.h
blobac364dae18f381ef26fb3585d8502a1478fa19f0
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 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>
29 #define __NR_futex 394
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 long int __ret; \
85 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
86 __lll_private_flag (FUTEX_WAIT, private), \
87 (val), (timespec)); \
88 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret; \
91 #define lll_futex_wake(futexp, nr, private) \
92 ({ \
93 INTERNAL_SYSCALL_DECL (__err); \
94 long int __ret; \
95 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
96 __lll_private_flag (FUTEX_WAKE, private), \
97 (nr), 0); \
98 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret; \
101 #define lll_robust_dead(futexv, private) \
102 do \
104 int *__futexp = &(futexv); \
105 atomic_or (__futexp, FUTEX_OWNER_DIED); \
106 lll_futex_wake (__futexp, 1, private); \
108 while (0)
110 /* Returns non-zero if error happened, zero if success. */
111 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
112 ({ \
113 INTERNAL_SYSCALL_DECL (__err); \
114 long int __ret; \
115 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
116 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
117 (nr_wake), (nr_move), (mutex), (val)); \
118 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
121 /* Returns non-zero if error happened, zero if success. */
122 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
123 ({ \
124 INTERNAL_SYSCALL_DECL (__err); \
125 long int __ret; \
126 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
127 __lll_private_flag (FUTEX_WAKE_OP, private), \
128 (nr_wake), (nr_wake2), (futexp2), \
129 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
130 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
136 static inline int __attribute__((always_inline))
137 __lll_trylock(int *futex)
139 return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
141 #define lll_trylock(lock) __lll_trylock (&(lock))
144 static inline int __attribute__((always_inline))
145 __lll_cond_trylock(int *futex)
147 return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
149 #define lll_cond_trylock(lock) __lll_cond_trylock (&(lock))
152 static inline int __attribute__((always_inline))
153 __lll_robust_trylock(int *futex, int id)
155 return atomic_compare_and_exchange_val_acq (futex, id, 0) != 0;
157 #define lll_robust_trylock(lock, id) \
158 __lll_robust_trylock (&(lock), id)
160 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
161 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
162 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
164 static inline void __attribute__((always_inline))
165 __lll_lock(int *futex, int private)
167 if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
169 if (__builtin_constant_p (private) && private == LLL_PRIVATE)
170 __lll_lock_wait_private (futex);
171 else
172 __lll_lock_wait (futex, private);
175 #define lll_lock(futex, private) __lll_lock (&(futex), private)
178 static inline int __attribute__ ((always_inline))
179 __lll_robust_lock (int *futex, int id, int private)
181 int result = 0;
182 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
183 result = __lll_robust_lock_wait (futex, private);
184 return result;
186 #define lll_robust_lock(futex, id, private) \
187 __lll_robust_lock (&(futex), id, private)
190 static inline void __attribute__ ((always_inline))
191 __lll_cond_lock (int *futex, int private)
193 if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0)
194 __lll_lock_wait (futex, private);
196 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
199 #define lll_robust_cond_lock(futex, id, private) \
200 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
203 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
204 int private) attribute_hidden;
205 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
206 int private) attribute_hidden;
208 static inline int __attribute__ ((always_inline))
209 __lll_timedlock (int *futex, const struct timespec *abstime, int private)
211 int result = 0;
212 if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
213 result = __lll_timedlock_wait (futex, abstime, private);
214 return result;
216 #define lll_timedlock(futex, abstime, private) \
217 __lll_timedlock (&(futex), abstime, private)
220 static inline int __attribute__ ((always_inline))
221 __lll_robust_timedlock (int *futex, const struct timespec *abstime,
222 int id, int private)
224 int result = 0;
225 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
226 result = __lll_robust_timedlock_wait (futex, abstime, private);
227 return result;
229 #define lll_robust_timedlock(futex, abstime, id, private) \
230 __lll_robust_timedlock (&(futex), abstime, id, private)
233 #define __lll_unlock(futex, private) \
234 (void) \
235 ({ int *__futex = (futex); \
236 int __oldval = atomic_exchange_rel (__futex, 0); \
237 if (__builtin_expect (__oldval > 1, 0)) \
238 lll_futex_wake (__futex, 1, private); \
240 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
243 #define __lll_robust_unlock(futex, private) \
244 (void) \
245 ({ int *__futex = (futex); \
246 int __oldval = atomic_exchange_rel (__futex, 0); \
247 if (__builtin_expect (__oldval & FUTEX_WAITERS, 0)) \
248 lll_futex_wake (__futex, 1, private); \
250 #define lll_robust_unlock(futex, private) \
251 __lll_robust_unlock(&(futex), private)
254 #define lll_islocked(futex) \
255 (futex != 0)
257 /* Initializers for lock. */
258 #define LLL_LOCK_INITIALIZER (0)
259 #define LLL_LOCK_INITIALIZER_LOCKED (1)
262 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
263 wakeup when the clone terminates. The memory location contains the
264 thread ID while the clone is running and is reset to zero
265 afterwards. */
266 #define lll_wait_tid(tid) \
267 do { \
268 __typeof (tid) __tid; \
269 while ((__tid = (tid)) != 0) \
270 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
271 } while (0)
273 extern int __lll_timedwait_tid (int *, const struct timespec *)
274 attribute_hidden;
276 #define lll_timedwait_tid(tid, abstime) \
277 ({ \
278 int __res = 0; \
279 if ((tid) != 0) \
280 __res = __lll_timedwait_tid (&(tid), (abstime)); \
281 __res; \
284 #endif /* lowlevellock.h */