]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/sparc/lowlevellock.h
1 /* Copyright (C) 2003-2014 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
23 #include <sys/param.h>
24 #include <bits/pthreadtypes.h>
26 #include <kernel-features.h>
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. */
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 ()
60 #if !defined NOT_IN_libc || defined 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)
66 # define __lll_private_flag(fl, private) \
67 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
70 # ifdef __ASSUME_PRIVATE_FUTEX
71 # define __lll_private_flag(fl, private) \
72 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
74 # define __lll_private_flag(fl, private) \
75 (__builtin_constant_p (private) \
77 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
79 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
80 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
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) \
90 INTERNAL_SYSCALL_DECL (__err); \
93 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
94 __lll_private_flag (FUTEX_WAIT, private), \
99 #define lll_futex_timed_wait_bitset(futexp, val, timespec, clockbit, private) \
101 INTERNAL_SYSCALL_DECL (__err); \
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); \
112 #define lll_futex_wake(futexp, nr, private) \
114 INTERNAL_SYSCALL_DECL (__err); \
117 __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
118 __lll_private_flag (FUTEX_WAKE, private), \
123 /* Returns non-zero if error happened, zero if success. */
124 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
126 INTERNAL_SYSCALL_DECL (__err); \
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 #define lll_robust_dead(futexv, private) \
138 int *__futexp = &(futexv); \
139 atomic_or (__futexp, FUTEX_OWNER_DIED); \
140 lll_futex_wake (__futexp, 1, private); \
144 /* Returns non-zero if error happened, zero if success. */
145 #ifdef __sparc32_atomic_do_lock
146 /* Avoid FUTEX_WAKE_OP if supporting pre-v9 CPUs. */
147 # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) 1
149 # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
151 INTERNAL_SYSCALL_DECL (__err); \
154 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
155 __lll_private_flag (FUTEX_WAKE_OP, private), \
156 (nr_wake), (nr_wake2), (futexp2), \
157 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
158 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
162 /* Priority Inheritance support. */
163 #define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
164 lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
166 #define lll_futex_timed_wait_requeue_pi(futexp, val, timespec, clockbit, \
169 INTERNAL_SYSCALL_DECL (__err); \
171 int __op = FUTEX_WAIT_REQUEUE_PI | clockbit; \
173 __ret = INTERNAL_SYSCALL (futex, __err, 5, (futexp), \
174 __lll_private_flag (__op, private), \
175 (val), (timespec), mutex); \
176 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
179 #define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, val, priv) \
181 INTERNAL_SYSCALL_DECL (__err); \
184 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
185 __lll_private_flag (FUTEX_CMP_REQUEUE_PI, priv),\
186 (nr_wake), (nr_move), (mutex), (val)); \
187 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
191 __attribute__ ((always_inline
))
192 __lll_trylock (int *futex
)
194 return atomic_compare_and_exchange_val_24_acq (futex
, 1, 0) != 0;
196 #define lll_trylock(futex) __lll_trylock (&(futex))
199 __attribute__ ((always_inline
))
200 __lll_cond_trylock (int *futex
)
202 return atomic_compare_and_exchange_val_24_acq (futex
, 2, 0) != 0;
204 #define lll_cond_trylock(futex) __lll_cond_trylock (&(futex))
207 __attribute__ ((always_inline
))
208 __lll_robust_trylock (int *futex
, int id
)
210 return atomic_compare_and_exchange_val_acq (futex
, id
, 0) != 0;
212 #define lll_robust_trylock(futex, id) \
213 __lll_robust_trylock (&(futex), id)
216 extern void __lll_lock_wait_private (int *futex
) attribute_hidden
;
217 extern void __lll_lock_wait (int *futex
, int private) attribute_hidden
;
218 extern int __lll_robust_lock_wait (int *futex
, int private) attribute_hidden
;
221 __attribute__ ((always_inline
))
222 __lll_lock (int *futex
, int private)
224 int val
= atomic_compare_and_exchange_val_24_acq (futex
, 1, 0);
226 if (__glibc_unlikely (val
!= 0))
228 if (__builtin_constant_p (private) && private == LLL_PRIVATE
)
229 __lll_lock_wait_private (futex
);
231 __lll_lock_wait (futex
, private);
234 #define lll_lock(futex, private) __lll_lock (&(futex), private)
237 __attribute__ ((always_inline
))
238 __lll_robust_lock (int *futex
, int id
, int private)
241 if (atomic_compare_and_exchange_bool_acq (futex
, id
, 0) != 0)
242 result
= __lll_robust_lock_wait (futex
, private);
245 #define lll_robust_lock(futex, id, private) \
246 __lll_robust_lock (&(futex), id, private)
249 __attribute__ ((always_inline
))
250 __lll_cond_lock (int *futex
, int private)
252 int val
= atomic_compare_and_exchange_val_24_acq (futex
, 2, 0);
254 if (__glibc_unlikely (val
!= 0))
255 __lll_lock_wait (futex
, private);
257 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
259 #define lll_robust_cond_lock(futex, id, private) \
260 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
263 extern int __lll_timedlock_wait (int *futex
, const struct timespec
*,
264 int private) attribute_hidden
;
265 extern int __lll_robust_timedlock_wait (int *futex
, const struct timespec
*,
266 int private) attribute_hidden
;
269 __attribute__ ((always_inline
))
270 __lll_timedlock (int *futex
, const struct timespec
*abstime
, int private)
272 int val
= atomic_compare_and_exchange_val_24_acq (futex
, 1, 0);
275 if (__glibc_unlikely (val
!= 0))
276 result
= __lll_timedlock_wait (futex
, abstime
, private);
279 #define lll_timedlock(futex, abstime, private) \
280 __lll_timedlock (&(futex), abstime, private)
283 __attribute__ ((always_inline
))
284 __lll_robust_timedlock (int *futex
, const struct timespec
*abstime
,
288 if (atomic_compare_and_exchange_bool_acq (futex
, id
, 0) != 0)
289 result
= __lll_robust_timedlock_wait (futex
, abstime
, private);
292 #define lll_robust_timedlock(futex, abstime, id, private) \
293 __lll_robust_timedlock (&(futex), abstime, id, private)
295 #define lll_unlock(lock, private) \
297 int *__futex = &(lock); \
298 int __val = atomic_exchange_24_rel (__futex, 0); \
299 if (__glibc_unlikely (__val > 1)) \
300 lll_futex_wake (__futex, 1, private); \
303 #define lll_robust_unlock(lock, private) \
305 int *__futex = &(lock); \
306 int __val = atomic_exchange_rel (__futex, 0); \
307 if (__glibc_unlikely (__val & FUTEX_WAITERS)) \
308 lll_futex_wake (__futex, 1, private); \
311 #define lll_islocked(futex) \
314 /* Initializers for lock. */
315 #define LLL_LOCK_INITIALIZER (0)
316 #define LLL_LOCK_INITIALIZER_LOCKED (1)
318 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
319 wakeup when the clone terminates. The memory location contains the
320 thread ID while the clone is running and is reset to zero
322 #define lll_wait_tid(tid) \
325 __typeof (tid) __tid; \
326 while ((__tid = (tid)) != 0) \
327 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
331 extern int __lll_timedwait_tid (int *, const struct timespec
*)
334 #define lll_timedwait_tid(tid, abstime) \
338 __res = __lll_timedwait_tid (&(tid), (abstime)); \
342 #endif /* lowlevellock.h */