Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / sparc / lowlevellock.h
blobdb67ff9580b769c2fbacfaa5bcd12e5438d9ed13
1 /* Copyright (C) 2003-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.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>
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
48 /* Values for 'private' parameter of locking macros. Yes, the
49 definition seems to be backwards. But it is not. The bit will be
50 reversed before passing to the system call. */
51 #define LLL_PRIVATE 0
52 #define LLL_SHARED FUTEX_PRIVATE_FLAG
54 #ifndef __sparc32_atomic_do_lock
55 /* Delay in spinlock loop. */
56 extern void __cpu_relax (void);
57 #define BUSY_WAIT_NOP __cpu_relax ()
58 #endif
60 #if IS_IN (libc) || IS_IN (rtld)
61 /* In libc.so or ld.so all futexes are private. */
62 # ifdef __ASSUME_PRIVATE_FUTEX
63 # define __lll_private_flag(fl, private) \
64 ((fl) | FUTEX_PRIVATE_FLAG)
65 # else
66 # define __lll_private_flag(fl, private) \
67 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
68 # endif
69 #else
70 # ifdef __ASSUME_PRIVATE_FUTEX
71 # define __lll_private_flag(fl, private) \
72 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
73 # else
74 # define __lll_private_flag(fl, private) \
75 (__builtin_constant_p (private) \
76 ? ((private) == 0 \
77 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
78 : (fl)) \
79 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
80 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
81 # endif
82 #endif
85 #define lll_futex_wait(futexp, val, private) \
86 lll_futex_timed_wait (futexp, val, NULL, private)
88 #define lll_futex_timed_wait(futexp, val, timespec, private) \
89 ({ \
90 INTERNAL_SYSCALL_DECL (__err); \
91 long int __ret; \
93 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
94 __lll_private_flag (FUTEX_WAIT, private), \
95 (val), (timespec)); \
96 __ret; \
99 #define lll_futex_timed_wait_bitset(futexp, val, timespec, clockbit, private) \
100 ({ \
101 INTERNAL_SYSCALL_DECL (__err); \
102 long int __ret; \
103 int __op = FUTEX_WAIT_BITSET | clockbit; \
105 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
106 __lll_private_flag (__op, private), \
107 (val), (timespec), NULL /* Unused. */, \
108 FUTEX_BITSET_MATCH_ANY); \
109 __ret; \
112 #define lll_futex_wake(futexp, nr, private) \
113 ({ \
114 INTERNAL_SYSCALL_DECL (__err); \
115 long int __ret; \
117 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
118 __lll_private_flag (FUTEX_WAKE, private), \
119 (nr), 0); \
120 __ret; \
123 /* Returns non-zero if error happened, zero if success. */
124 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
125 ({ \
126 INTERNAL_SYSCALL_DECL (__err); \
127 long int __ret; \
129 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
130 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
131 (nr_wake), (nr_move), (mutex), (val)); \
132 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
135 /* Returns non-zero if error happened, zero if success. */
136 #ifdef __sparc32_atomic_do_lock
137 /* Avoid FUTEX_WAKE_OP if supporting pre-v9 CPUs. */
138 # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) 1
139 #else
140 # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
141 ({ \
142 INTERNAL_SYSCALL_DECL (__err); \
143 long int __ret; \
145 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
146 __lll_private_flag (FUTEX_WAKE_OP, private), \
147 (nr_wake), (nr_wake2), (futexp2), \
148 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
149 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
151 #endif
153 /* Priority Inheritance support. */
154 #define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
155 lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
157 #define lll_futex_timed_wait_requeue_pi(futexp, val, timespec, clockbit, \
158 mutex, private) \
159 ({ \
160 INTERNAL_SYSCALL_DECL (__err); \
161 long int __ret; \
162 int __op = FUTEX_WAIT_REQUEUE_PI | clockbit; \
164 __ret = INTERNAL_SYSCALL (futex, __err, 5, (futexp), \
165 __lll_private_flag (__op, private), \
166 (val), (timespec), mutex); \
167 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
170 #define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, val, priv) \
171 ({ \
172 INTERNAL_SYSCALL_DECL (__err); \
173 long int __ret; \
175 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
176 __lll_private_flag (FUTEX_CMP_REQUEUE_PI, priv),\
177 (nr_wake), (nr_move), (mutex), (val)); \
178 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
181 static inline int
182 __attribute__ ((always_inline))
183 __lll_trylock (int *futex)
185 return atomic_compare_and_exchange_val_24_acq (futex, 1, 0) != 0;
187 #define lll_trylock(futex) __lll_trylock (&(futex))
189 static inline int
190 __attribute__ ((always_inline))
191 __lll_cond_trylock (int *futex)
193 return atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0;
195 #define lll_cond_trylock(futex) __lll_cond_trylock (&(futex))
198 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
199 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
200 extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
202 static inline void
203 __attribute__ ((always_inline))
204 __lll_lock (int *futex, int private)
206 int val = atomic_compare_and_exchange_val_24_acq (futex, 1, 0);
208 if (__glibc_unlikely (val != 0))
210 if (__builtin_constant_p (private) && private == LLL_PRIVATE)
211 __lll_lock_wait_private (futex);
212 else
213 __lll_lock_wait (futex, private);
216 #define lll_lock(futex, private) __lll_lock (&(futex), private)
218 static inline int
219 __attribute__ ((always_inline))
220 __lll_robust_lock (int *futex, int id, int private)
222 int result = 0;
223 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
224 result = __lll_robust_lock_wait (futex, private);
225 return result;
227 #define lll_robust_lock(futex, id, private) \
228 __lll_robust_lock (&(futex), id, private)
230 static inline void
231 __attribute__ ((always_inline))
232 __lll_cond_lock (int *futex, int private)
234 int val = atomic_compare_and_exchange_val_24_acq (futex, 2, 0);
236 if (__glibc_unlikely (val != 0))
237 __lll_lock_wait (futex, private);
239 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
241 #define lll_robust_cond_lock(futex, id, private) \
242 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
245 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
246 int private) attribute_hidden;
247 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
248 int private) attribute_hidden;
250 static inline int
251 __attribute__ ((always_inline))
252 __lll_timedlock (int *futex, const struct timespec *abstime, int private)
254 int val = atomic_compare_and_exchange_val_24_acq (futex, 1, 0);
255 int result = 0;
257 if (__glibc_unlikely (val != 0))
258 result = __lll_timedlock_wait (futex, abstime, private);
259 return result;
261 #define lll_timedlock(futex, abstime, private) \
262 __lll_timedlock (&(futex), abstime, private)
264 static inline int
265 __attribute__ ((always_inline))
266 __lll_robust_timedlock (int *futex, const struct timespec *abstime,
267 int id, int private)
269 int result = 0;
270 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
271 result = __lll_robust_timedlock_wait (futex, abstime, private);
272 return result;
274 #define lll_robust_timedlock(futex, abstime, id, private) \
275 __lll_robust_timedlock (&(futex), abstime, id, private)
277 #define lll_unlock(lock, private) \
278 ((void) ({ \
279 int *__futex = &(lock); \
280 int __val = atomic_exchange_24_rel (__futex, 0); \
281 if (__glibc_unlikely (__val > 1)) \
282 lll_futex_wake (__futex, 1, private); \
285 #define lll_robust_unlock(lock, private) \
286 ((void) ({ \
287 int *__futex = &(lock); \
288 int __val = atomic_exchange_rel (__futex, 0); \
289 if (__glibc_unlikely (__val & FUTEX_WAITERS)) \
290 lll_futex_wake (__futex, 1, private); \
293 #define lll_islocked(futex) \
294 (futex != 0)
296 /* Initializers for lock. */
297 #define LLL_LOCK_INITIALIZER (0)
298 #define LLL_LOCK_INITIALIZER_LOCKED (1)
300 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
301 wakeup when the clone terminates. The memory location contains the
302 thread ID while the clone is running and is reset to zero
303 afterwards. */
304 #define lll_wait_tid(tid) \
305 do \
307 __typeof (tid) __tid; \
308 while ((__tid = (tid)) != 0) \
309 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
311 while (0)
313 extern int __lll_timedwait_tid (int *, const struct timespec *)
314 attribute_hidden;
316 #define lll_timedwait_tid(tid, abstime) \
317 ({ \
318 int __res = 0; \
319 if ((tid) != 0) \
320 __res = __lll_timedwait_tid (&(tid), (abstime)); \
321 __res; \
324 #endif /* lowlevellock.h */