Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / hppa / lowlevellock.h
blob74389477ff3512ca47453a7bdc6e8d55272830e7
1 /* Copyright (C) 2003-2015 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 <sysdep.h>
25 #include <atomic.h>
26 #include <kernel-features.h> /* Need __ASSUME_PRIVATE_FUTEX. */
27 #include <tls.h> /* Need THREAD_*, and header.*. */
29 /* HPPA only has one atomic read and modify memory operation,
30 load and clear, so hppa uses a kernel helper routine to implement
31 compare_and_exchange. See atomic.h for the userspace calling
32 sequence. */
34 #define FUTEX_WAIT 0
35 #define FUTEX_WAKE 1
36 #define FUTEX_REQUEUE 3
37 #define FUTEX_CMP_REQUEUE 4
38 #define FUTEX_WAKE_OP 5
39 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
40 #define FUTEX_LOCK_PI 6
41 #define FUTEX_UNLOCK_PI 7
42 #define FUTEX_TRYLOCK_PI 8
43 #define FUTEX_WAIT_BITSET 9
44 #define FUTEX_WAKE_BITSET 10
45 #define FUTEX_WAIT_REQUEUE_PI 11
46 #define FUTEX_CMP_REQUEUE_PI 12
47 #define FUTEX_PRIVATE_FLAG 128
48 #define FUTEX_CLOCK_REALTIME 256
50 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
52 /* Values for 'private' parameter of locking macros. Yes, the
53 definition seems to be backwards. But it is not. The bit will be
54 reversed before passing to the system call. */
55 #define LLL_PRIVATE 0
56 #define LLL_SHARED FUTEX_PRIVATE_FLAG
58 /* Initialize locks to zero. */
59 #define LLL_MUTEX_LOCK_INITIALIZER (0)
61 #if IS_IN (libc) || IS_IN (rtld)
62 /* In libc.so or ld.so all futexes are private. */
63 # ifdef __ASSUME_PRIVATE_FUTEX
64 # define __lll_private_flag(fl, private) \
65 ((fl) | FUTEX_PRIVATE_FLAG)
66 # else
67 # define __lll_private_flag(fl, private) \
68 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
69 # endif
70 #else
71 # ifdef __ASSUME_PRIVATE_FUTEX
72 # define __lll_private_flag(fl, private) \
73 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
74 # else
75 # define __lll_private_flag(fl, private) \
76 (__builtin_constant_p (private) \
77 ? ((private) == 0 \
78 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
79 : (fl)) \
80 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
81 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
82 # endif
83 #endif
85 /* Type for lock object. */
86 typedef int lll_lock_t;
88 #define lll_futex_wait(futexp, val, private) \
89 lll_futex_timed_wait (futexp, val, 0, private)
91 #define lll_futex_timed_wait(futexp, val, timespec, private) \
92 ({ \
93 INTERNAL_SYSCALL_DECL (__err); \
94 long int __ret; \
95 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
96 __lll_private_flag (FUTEX_WAIT, private), \
97 (val), (timespec)); \
98 __ret; \
101 #define lll_futex_wake(futexp, nr, private) \
102 ({ \
103 INTERNAL_SYSCALL_DECL (__err); \
104 long int __ret; \
105 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
106 __lll_private_flag (FUTEX_WAKE, private), \
107 (nr), 0); \
108 __ret; \
111 #define lll_private_futex_wait(futex, val) \
112 lll_private_futex_timed_wait (futex, val, NULL)
114 #ifdef __ASSUME_PRIVATE_FUTEX
115 # define lll_private_futex_timed_wait(futexp, val, timespec) \
116 ({ \
117 INTERNAL_SYSCALL_DECL (__err); \
118 long int __ret; \
119 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
120 (futexp), FUTEX_WAIT | FUTEX_PRIVATE_FLAG, \
121 (val), (timespec)); \
122 __ret; \
124 #else
125 # define lll_private_futex_timed_wait(futexp, val, timespec) \
126 ({ \
127 INTERNAL_SYSCALL_DECL (__err); \
128 long int __ret, __op; \
129 __op = FUTEX_WAIT | THREAD_GETMEM (THREAD_SELF, header.private_futex); \
130 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
131 (futexp), __op, (val), (timespec)); \
132 __ret; \
134 #endif
136 /* Returns non-zero if error happened, zero if success. */
137 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
138 ({ \
139 INTERNAL_SYSCALL_DECL (__err); \
140 long int __ret; \
141 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
142 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
143 (nr_wake), (nr_move), (mutex), (val)); \
144 __ret; \
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, __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 __ret; \
159 /* Priority Inheritance support. */
160 #define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
161 lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
163 #define lll_futex_timed_wait_requeue_pi(futexp, val, timespec, clockbit, \
164 mutex, private) \
165 ({ \
166 INTERNAL_SYSCALL_DECL (__err); \
167 long int __ret; \
168 int __op = FUTEX_WAIT_REQUEUE_PI | clockbit; \
170 __ret = INTERNAL_SYSCALL (futex, __err, 5, (futexp), \
171 __lll_private_flag (__op, private), \
172 (val), (timespec), mutex); \
173 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
176 #define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, val, priv) \
177 ({ \
178 INTERNAL_SYSCALL_DECL (__err); \
179 long int __ret; \
181 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
182 __lll_private_flag (FUTEX_CMP_REQUEUE_PI, priv),\
183 (nr_wake), (nr_move), (mutex), (val)); \
184 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
187 static inline int
188 __attribute__ ((always_inline))
189 __lll_cond_trylock (int *futex)
191 return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
193 #define lll_cond_trylock(futex) __lll_cond_trylock (&(futex))
195 static inline int
196 __attribute__ ((always_inline))
197 __lll_trylock (int *futex)
199 return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
201 #define lll_trylock(futex) __lll_trylock (&(futex))
203 extern void __lll_lock_wait (lll_lock_t *futex, int private) attribute_hidden;
204 extern void __lll_lock_wait_private (lll_lock_t *futex) attribute_hidden;
206 static inline void __attribute__((always_inline))
207 __lll_mutex_lock(lll_lock_t *futex, int private)
209 int val = atomic_compare_and_exchange_val_acq (futex, 1, 0);
211 if (__builtin_expect (val != 0, 0))
213 if (__builtin_constant_p (private) && private == LLL_PRIVATE)
214 __lll_lock_wait_private (futex);
215 else
216 __lll_lock_wait (futex, private);
219 #define lll_mutex_lock(futex, private) __lll_mutex_lock (&(futex), private)
220 #define lll_lock(lock, private) lll_mutex_lock (lock, private)
222 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
224 static inline int
225 __attribute__ ((always_inline))
226 __lll_robust_lock (int *futex, int id, int private)
228 int result = 0;
229 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
230 result = __lll_robust_lock_wait (futex, private);
231 return result;
233 #define lll_robust_lock(futex, id, private) \
234 __lll_robust_lock (&(futex), id, private)
236 #define lll_robust_cond_lock(futex, id, private) \
237 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
239 static inline void
240 __attribute__ ((always_inline))
241 __lll_cond_lock (int *futex, int private)
243 int val = atomic_compare_and_exchange_val_acq (futex, 2, 0);
245 if (__builtin_expect (val != 0, 0))
246 __lll_lock_wait (futex, private);
248 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
250 extern int __lll_timedlock_wait (lll_lock_t *futex, const struct timespec *,
251 int private) attribute_hidden;
252 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
253 int private) attribute_hidden;
255 static inline int
256 __attribute__ ((always_inline))
257 __lll_timedlock (int *futex, const struct timespec *abstime, int private)
259 int val = atomic_compare_and_exchange_val_acq (futex, 1, 0);
260 int result = 0;
262 if (__builtin_expect (val != 0, 0))
263 result = __lll_timedlock_wait (futex, abstime, private);
264 return result;
266 #define lll_timedlock(futex, abstime, private) \
267 __lll_timedlock (&(futex), abstime, private)
269 static inline int __attribute__ ((always_inline))
270 __lll_robust_timedlock (int *futex, const struct timespec *abstime,
271 int id, int private)
273 int result = 0;
274 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
275 result = __lll_robust_timedlock_wait (futex, abstime, private);
276 return result;
278 #define lll_robust_timedlock(futex, abstime, id, private) \
279 __lll_robust_timedlock (&(futex), abstime, id, private)
281 #define __lll_unlock(futex, private) \
282 (void) \
283 ({ int val = atomic_exchange_rel (futex, 0); \
284 if (__builtin_expect (val > 1, 0)) \
285 lll_futex_wake (futex, 1, private); \
287 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
289 #define __lll_robust_unlock(futex,private) \
290 (void) \
291 ({ int val = atomic_exchange_rel (futex, 0); \
292 if (__builtin_expect (val & FUTEX_WAITERS, 0)) \
293 lll_futex_wake (futex, 1, private); \
295 #define lll_robust_unlock(futex, private) \
296 __lll_robust_unlock(&(futex), private)
298 #define lll_islocked(futex) \
299 (futex != 0)
301 /* Our internal lock implementation is identical to the binary-compatible
302 mutex implementation. */
303 #define LLL_LOCK_INITIALIZER (0)
304 #define LLL_LOCK_INITIALIZER_CONST (0)
305 #define LLL_LOCK_INITIALIZER_LOCKED (1)
307 #define THREAD_INIT_LOCK(PD, LOCK) \
308 (PD)->LOCK = LLL_LOCK_INITIALIZER
310 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
311 wakeup when the clone terminates. The memory location contains the
312 thread ID while the clone is running and is reset to zero
313 afterwards. */
314 #define lll_wait_tid(tid) \
315 do \
317 __typeof (tid) __tid; \
318 while ((__tid = (tid)) != 0) \
319 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
321 while (0)
323 extern int __lll_timedwait_tid (int *, const struct timespec *)
324 attribute_hidden;
326 #define lll_timedwait_tid(tid, abstime) \
327 ({ \
328 int __res = 0; \
329 if ((tid) != 0) \
330 __res = __lll_timedwait_tid (&(tid), (abstime)); \
331 __res; \
334 #endif /* lowlevellock.h */