Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / sysdeps / unix / sysv / linux / s390 / lowlevellock.h
blob80dc90543be4ed89d0f10d0795536d408e14a8bf
1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
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 <kernel-features.h>
28 #define SYS_futex 238
29 #define FUTEX_WAIT 0
30 #define FUTEX_WAKE 1
31 #define FUTEX_REQUEUE 3
32 #define FUTEX_CMP_REQUEUE 4
33 #define FUTEX_WAKE_OP 5
34 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
35 #define FUTEX_LOCK_PI 6
36 #define FUTEX_UNLOCK_PI 7
37 #define FUTEX_TRYLOCK_PI 8
38 #define FUTEX_WAIT_BITSET 9
39 #define FUTEX_WAKE_BITSET 10
40 #define FUTEX_WAIT_REQUEUE_PI 11
41 #define FUTEX_CMP_REQUEUE_PI 12
42 #define FUTEX_PRIVATE_FLAG 128
43 #define FUTEX_CLOCK_REALTIME 256
45 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
47 /* Values for 'private' parameter of locking macros. Yes, the
48 definition seems to be backwards. But it is not. The bit will be
49 reversed before passing to the system call. */
50 #define LLL_PRIVATE 0
51 #define LLL_SHARED FUTEX_PRIVATE_FLAG
54 #if !defined NOT_IN_libc || defined IS_IN_rtld
55 /* In libc.so or ld.so all futexes are private. */
56 # ifdef __ASSUME_PRIVATE_FUTEX
57 # define __lll_private_flag(fl, private) \
58 ((fl) | FUTEX_PRIVATE_FLAG)
59 # else
60 # define __lll_private_flag(fl, private) \
61 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
62 # endif
63 #else
64 # ifdef __ASSUME_PRIVATE_FUTEX
65 # define __lll_private_flag(fl, private) \
66 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
67 # else
68 # define __lll_private_flag(fl, private) \
69 (__builtin_constant_p (private) \
70 ? ((private) == 0 \
71 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
72 : (fl)) \
73 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
74 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
75 # endif
76 #endif
78 #define lll_futex_wait(futex, val, private) \
79 lll_futex_timed_wait (futex, val, NULL, private)
81 #define lll_futex_timed_wait(futexp, val, timespec, private) \
82 ({ \
83 INTERNAL_SYSCALL_DECL (__err); \
85 INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
86 __lll_private_flag (FUTEX_WAIT, private), \
87 (val), (timespec)); \
90 #define lll_futex_timed_wait_bitset(futexp, val, timespec, clockbit, private) \
91 ({ \
92 INTERNAL_SYSCALL_DECL (__err); \
93 int __op = FUTEX_WAIT_BITSET | clockbit; \
95 INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
96 __lll_private_flag (__op, private), \
97 (val), (timespec), NULL /* Unused. */, \
98 FUTEX_BITSET_MATCH_ANY); \
101 #define lll_futex_wake(futexp, nr, private) \
102 ({ \
103 INTERNAL_SYSCALL_DECL (__err); \
105 INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
106 __lll_private_flag (FUTEX_WAKE, private), \
107 (nr), 0); \
110 #define lll_robust_dead(futexv, private) \
111 do \
113 int *__futexp = &(futexv); \
115 atomic_or (__futexp, FUTEX_OWNER_DIED); \
116 lll_futex_wake (__futexp, 1, private); \
118 while (0)
121 /* Returns non-zero if error happened, zero if success. */
122 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
123 ({ \
124 INTERNAL_SYSCALL_DECL (__err); \
125 long int __ret; \
127 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
128 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
129 (nr_wake), (nr_move), (mutex), (val)); \
130 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
133 /* Returns non-zero if error happened, zero if success. */
134 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
135 ({ \
136 INTERNAL_SYSCALL_DECL (__err); \
137 long int __ret; \
139 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
140 __lll_private_flag (FUTEX_WAKE_OP, private), \
141 (nr_wake), (nr_wake2), (futexp2), \
142 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
143 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
146 /* Priority Inheritance support. */
147 #define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
148 lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
150 #define lll_futex_timed_wait_requeue_pi(futexp, val, timespec, clockbit, \
151 mutex, private) \
152 ({ \
153 INTERNAL_SYSCALL_DECL (__err); \
154 int __op = FUTEX_WAIT_REQUEUE_PI | clockbit; \
156 INTERNAL_SYSCALL (futex, __err, 5, (futexp), \
157 __lll_private_flag (__op, private), \
158 (val), (timespec), mutex); \
161 #define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, val, priv) \
162 ({ \
163 INTERNAL_SYSCALL_DECL (__err); \
164 long int __ret; \
166 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
167 __lll_private_flag (FUTEX_CMP_REQUEUE_PI, priv),\
168 (nr_wake), (nr_move), (mutex), (val)); \
169 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
172 #define lll_compare_and_swap(futex, oldval, newval, operation) \
173 do { \
174 __typeof (futex) __futex = (futex); \
175 __asm __volatile (" l %1,%0\n" \
176 "0: " operation "\n" \
177 " cs %1,%2,%0\n" \
178 " jl 0b\n" \
179 "1:" \
180 : "=Q" (*__futex), "=&d" (oldval), "=&d" (newval) \
181 : "m" (*__futex) : "cc", "memory" ); \
182 } while (0)
185 static inline int
186 __attribute__ ((always_inline))
187 __lll_trylock (int *futex)
189 unsigned int old;
191 __asm __volatile ("cs %0,%3,%1"
192 : "=d" (old), "=Q" (*futex)
193 : "0" (0), "d" (1), "m" (*futex) : "cc", "memory" );
194 return old != 0;
196 #define lll_trylock(futex) __lll_trylock (&(futex))
199 static inline int
200 __attribute__ ((always_inline))
201 __lll_cond_trylock (int *futex)
203 unsigned int old;
205 __asm __volatile ("cs %0,%3,%1"
206 : "=d" (old), "=Q" (*futex)
207 : "0" (0), "d" (2), "m" (*futex) : "cc", "memory" );
208 return old != 0;
210 #define lll_cond_trylock(futex) __lll_cond_trylock (&(futex))
213 static inline int
214 __attribute__ ((always_inline))
215 __lll_robust_trylock (int *futex, int id)
217 unsigned int old;
219 __asm __volatile ("cs %0,%3,%1"
220 : "=d" (old), "=Q" (*futex)
221 : "0" (0), "d" (id), "m" (*futex) : "cc", "memory" );
222 return old != 0;
224 #define lll_robust_trylock(futex, id) \
225 __lll_robust_trylock (&(futex), id)
228 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
229 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
230 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
232 static inline void
233 __attribute__ ((always_inline))
234 __lll_lock (int *futex, int private)
236 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (futex, 1, 0), 0))
238 if (__builtin_constant_p (private) && private == LLL_PRIVATE)
239 __lll_lock_wait_private (futex);
240 else
241 __lll_lock_wait (futex, private);
244 #define lll_lock(futex, private) __lll_lock (&(futex), private)
246 static inline int
247 __attribute__ ((always_inline))
248 __lll_robust_lock (int *futex, int id, int private)
250 int result = 0;
251 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (futex, id, 0),
253 result = __lll_robust_lock_wait (futex, private);
254 return result;
256 #define lll_robust_lock(futex, id, private) \
257 __lll_robust_lock (&(futex), id, private)
259 static inline void
260 __attribute__ ((always_inline))
261 __lll_cond_lock (int *futex, int private)
263 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (futex, 2, 0), 0))
264 __lll_lock_wait (futex, private);
266 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
268 #define lll_robust_cond_lock(futex, id, private) \
269 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
271 extern int __lll_timedlock_wait
272 (int *futex, const struct timespec *, int private) attribute_hidden;
273 extern int __lll_robust_timedlock_wait
274 (int *futex, const struct timespec *, int private) attribute_hidden;
276 static inline int
277 __attribute__ ((always_inline))
278 __lll_timedlock (int *futex, const struct timespec *abstime, int private)
280 int result = 0;
281 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (futex, 1, 0), 0))
282 result = __lll_timedlock_wait (futex, abstime, private);
283 return result;
285 #define lll_timedlock(futex, abstime, private) \
286 __lll_timedlock (&(futex), abstime, private)
288 static inline int
289 __attribute__ ((always_inline))
290 __lll_robust_timedlock (int *futex, const struct timespec *abstime,
291 int id, int private)
293 int result = 0;
294 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (futex, id, 0),
296 result = __lll_robust_timedlock_wait (futex, abstime, private);
297 return result;
299 #define lll_robust_timedlock(futex, abstime, id, private) \
300 __lll_robust_timedlock (&(futex), abstime, id, private)
303 #define __lll_unlock(futex, private) \
304 (void) \
305 ({ int __oldval; \
306 int __newval = 0; \
307 int *__futexp = (futex); \
309 lll_compare_and_swap (__futexp, __oldval, __newval, "slr %2,%2"); \
310 if (__builtin_expect (__oldval > 1, 0)) \
311 lll_futex_wake (__futexp, 1, private); \
313 #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
316 #define __lll_robust_unlock(futex, private) \
317 (void) \
318 ({ int __oldval; \
319 int __newval = 0; \
320 int *__futexp = (futex); \
322 lll_compare_and_swap (__futexp, __oldval, __newval, "slr %2,%2"); \
323 if (__builtin_expect (__oldval & FUTEX_WAITERS, 0)) \
324 lll_futex_wake (__futexp, 1, private); \
326 #define lll_robust_unlock(futex, private) \
327 __lll_robust_unlock(&(futex), private)
329 #define lll_islocked(futex) \
330 (futex != 0)
333 /* Initializers for lock. */
334 #define LLL_LOCK_INITIALIZER (0)
335 #define LLL_LOCK_INITIALIZER_LOCKED (1)
337 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
338 wakeup when the clone terminates. The memory location contains the
339 thread ID while the clone is running and is reset to zero
340 afterwards. */
341 #define __lll_wait_tid(ptid) \
342 do \
344 int __tid; \
346 while ((__tid = *ptid) != 0) \
347 lll_futex_wait (ptid, __tid, LLL_SHARED); \
349 while (0)
350 #define lll_wait_tid(tid) __lll_wait_tid(&(tid))
352 extern int __lll_timedwait_tid (int *, const struct timespec *)
353 attribute_hidden;
355 #define lll_timedwait_tid(tid, abstime) \
356 ({ \
357 int __res = 0; \
358 if ((tid) != 0) \
359 __res = __lll_timedwait_tid (&(tid), (abstime)); \
360 __res; \
363 #endif /* lowlevellock.h */