]> sourceware.org Git - glibc.git/blame - ports/sysdeps/unix/sysv/linux/mips/nptl/lowlevellock.h
Add generic versions of pthread_spin_lock and pthread_spin_trylock.
[glibc.git] / ports / sysdeps / unix / sysv / linux / mips / nptl / lowlevellock.h
CommitLineData
bb3b3056
JM
1/* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008,
2 2009 Free Software Foundation, Inc.
2568b674
AJ
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
ab84e3ff
PE
16 License along with the GNU C Library. If not, see
17 <http://www.gnu.org/licenses/>. */
2568b674
AJ
18
19#ifndef _LOWLEVELLOCK_H
20#define _LOWLEVELLOCK_H 1
21
22#include <time.h>
23#include <sys/param.h>
24#include <bits/pthreadtypes.h>
25#include <atomic.h>
26#include <sysdep.h>
8c276674 27#include <kernel-features.h>
2568b674
AJ
28
29#define FUTEX_WAIT 0
30#define FUTEX_WAKE 1
31#define FUTEX_REQUEUE 3
32#define FUTEX_CMP_REQUEUE 4
13d7881a
DJ
33#define FUTEX_WAKE_OP 5
34#define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
0ad4d3b0
DJ
35#define FUTEX_LOCK_PI 6
36#define FUTEX_UNLOCK_PI 7
37#define FUTEX_TRYLOCK_PI 8
9a9863b4
JM
38#define FUTEX_WAIT_BITSET 9
39#define FUTEX_WAKE_BITSET 10
30efab51 40#define FUTEX_PRIVATE_FLAG 128
bb3b3056
JM
41#define FUTEX_CLOCK_REALTIME 256
42
43#define FUTEX_BITSET_MATCH_ANY 0xffffffff
2568b674 44
8c276674
DJ
45/* Values for 'private' parameter of locking macros. Yes, the
46 definition seems to be backwards. But it is not. The bit will be
47 reversed before passing to the system call. */
48#define LLL_PRIVATE 0
49#define LLL_SHARED FUTEX_PRIVATE_FLAG
50
51
52#if !defined NOT_IN_libc || defined IS_IN_rtld
53/* In libc.so or ld.so all futexes are private. */
54# ifdef __ASSUME_PRIVATE_FUTEX
55# define __lll_private_flag(fl, private) \
56 ((fl) | FUTEX_PRIVATE_FLAG)
57# else
58# define __lll_private_flag(fl, private) \
59 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
60# endif
61#else
62# ifdef __ASSUME_PRIVATE_FUTEX
63# define __lll_private_flag(fl, private) \
64 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
65# else
66# define __lll_private_flag(fl, private) \
67 (__builtin_constant_p (private) \
68 ? ((private) == 0 \
69 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
70 : (fl)) \
71 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
72 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
73# endif
74#endif
75
76
77#define lll_futex_wait(futexp, val, private) \
78 lll_futex_timed_wait(futexp, val, NULL, private)
79
80#define lll_futex_timed_wait(futexp, val, timespec, private) \
2568b674
AJ
81 ({ \
82 INTERNAL_SYSCALL_DECL (__err); \
83 long int __ret; \
8c276674
DJ
84 __ret = INTERNAL_SYSCALL (futex, __err, 4, (long) (futexp), \
85 __lll_private_flag (FUTEX_WAIT, private), \
86 (val), (timespec)); \
2568b674
AJ
87 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
88 })
89
8c276674 90#define lll_futex_wake(futexp, nr, private) \
2568b674
AJ
91 ({ \
92 INTERNAL_SYSCALL_DECL (__err); \
93 long int __ret; \
8c276674
DJ
94 __ret = INTERNAL_SYSCALL (futex, __err, 4, (long) (futexp), \
95 __lll_private_flag (FUTEX_WAKE, private), \
96 (nr), 0); \
2568b674
AJ
97 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
98 })
99
8c276674 100#define lll_robust_dead(futexv, private) \
13d7881a
DJ
101 do \
102 { \
103 int *__futexp = &(futexv); \
104 atomic_or (__futexp, FUTEX_OWNER_DIED); \
8c276674 105 lll_futex_wake (__futexp, 1, private); \
13d7881a
DJ
106 } \
107 while (0)
108
2568b674 109/* Returns non-zero if error happened, zero if success. */
8c276674 110#define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
2568b674
AJ
111 ({ \
112 INTERNAL_SYSCALL_DECL (__err); \
113 long int __ret; \
8c276674
DJ
114 __ret = INTERNAL_SYSCALL (futex, __err, 6, (long) (futexp), \
115 __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
116 (nr_wake), (nr_move), (mutex), (val)); \
2568b674
AJ
117 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
118 })
119
13d7881a 120/* Returns non-zero if error happened, zero if success. */
8c276674 121#define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
13d7881a
DJ
122 ({ \
123 INTERNAL_SYSCALL_DECL (__err); \
124 long int __ret; \
125 \
8c276674
DJ
126 __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
127 __lll_private_flag (FUTEX_WAKE_OP, private), \
128 (nr_wake), (nr_wake2), (futexp2), \
13d7881a
DJ
129 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
130 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
131 })
2568b674
AJ
132
133static inline int __attribute__((always_inline))
8c276674 134__lll_trylock(int *futex)
2568b674
AJ
135{
136 return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
137}
8c276674 138#define lll_trylock(lock) __lll_trylock (&(lock))
2568b674
AJ
139
140
141static inline int __attribute__((always_inline))
8c276674 142__lll_cond_trylock(int *futex)
2568b674
AJ
143{
144 return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
145}
8c276674 146#define lll_cond_trylock(lock) __lll_cond_trylock (&(lock))
2568b674
AJ
147
148
13d7881a 149static inline int __attribute__((always_inline))
8c276674 150__lll_robust_trylock(int *futex, int id)
13d7881a
DJ
151{
152 return atomic_compare_and_exchange_val_acq (futex, id, 0) != 0;
153}
8c276674
DJ
154#define lll_robust_trylock(lock, id) \
155 __lll_robust_trylock (&(lock), id)
156
157extern void __lll_lock_wait_private (int *futex) attribute_hidden;
158extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
159extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
160
161#define __lll_lock(futex, private) \
162 ((void) ({ \
163 int *__futex = (futex); \
164 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, \
165 1, 0), 0)) \
166 { \
167 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
168 __lll_lock_wait_private (__futex); \
169 else \
170 __lll_lock_wait (__futex, private); \
171 } \
172 }))
173#define lll_lock(futex, private) __lll_lock (&(futex), private)
174
175
176#define __lll_robust_lock(futex, id, private) \
177 ({ \
178 int *__futex = (futex); \
179 int __val = 0; \
180 \
181 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
182 0), 0)) \
183 __val = __lll_robust_lock_wait (__futex, private); \
184 __val; \
185 })
186#define lll_robust_lock(futex, id, private) \
187 __lll_robust_lock (&(futex), id, private)
13d7881a
DJ
188
189
2568b674 190static inline void __attribute__ ((always_inline))
8c276674 191__lll_cond_lock (int *futex, int private)
2568b674
AJ
192{
193 if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0)
8c276674 194 __lll_lock_wait (futex, private);
2568b674 195}
8c276674 196#define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
2568b674
AJ
197
198
8c276674
DJ
199#define lll_robust_cond_lock(futex, id, private) \
200 __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
13d7881a
DJ
201
202
8c276674
DJ
203extern int __lll_timedlock_wait (int *futex, const struct timespec *,
204 int private) attribute_hidden;
205extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
206 int private) attribute_hidden;
2568b674
AJ
207
208static inline int __attribute__ ((always_inline))
8c276674 209__lll_timedlock (int *futex, const struct timespec *abstime, int private)
2568b674
AJ
210{
211 int result = 0;
212 if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
8c276674 213 result = __lll_timedlock_wait (futex, abstime, private);
2568b674
AJ
214 return result;
215}
8c276674
DJ
216#define lll_timedlock(futex, abstime, private) \
217 __lll_timedlock (&(futex), abstime, private)
2568b674
AJ
218
219
13d7881a 220static inline int __attribute__ ((always_inline))
8c276674
DJ
221__lll_robust_timedlock (int *futex, const struct timespec *abstime,
222 int id, int private)
13d7881a
DJ
223{
224 int result = 0;
225 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
8c276674 226 result = __lll_robust_timedlock_wait (futex, abstime, private);
13d7881a
DJ
227 return result;
228}
8c276674
DJ
229#define lll_robust_timedlock(futex, abstime, id, private) \
230 __lll_robust_timedlock (&(futex), abstime, id, private)
13d7881a
DJ
231
232
8c276674
DJ
233#define __lll_unlock(futex, private) \
234 ((void) ({ \
235 int *__futex = (futex); \
236 int __val = atomic_exchange_rel (__futex, 0); \
237 \
238 if (__builtin_expect (__val > 1, 0)) \
239 lll_futex_wake (__futex, 1, private); \
240 }))
241#define lll_unlock(futex, private) __lll_unlock(&(futex), private)
13d7881a
DJ
242
243
8c276674
DJ
244#define __lll_robust_unlock(futex, private) \
245 ((void) ({ \
246 int *__futex = (futex); \
247 int __val = atomic_exchange_rel (__futex, 0); \
248 \
249 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
250 lll_futex_wake (__futex, 1, private); \
251 }))
252#define lll_robust_unlock(futex, private) \
253 __lll_robust_unlock(&(futex), private)
2568b674
AJ
254
255
8c276674 256#define lll_islocked(futex) \
2568b674
AJ
257 (futex != 0)
258
259
260/* Our internal lock implementation is identical to the binary-compatible
261 mutex implementation. */
262
2568b674
AJ
263/* Initializers for lock. */
264#define LLL_LOCK_INITIALIZER (0)
265#define LLL_LOCK_INITIALIZER_LOCKED (1)
266
2568b674
AJ
267/* The states of a lock are:
268 0 - untaken
269 1 - taken by one user
270 >1 - taken by more users */
271
2568b674
AJ
272/* The kernel notifies a process which uses CLONE_CLEARTID via futex
273 wakeup when the clone terminates. The memory location contains the
274 thread ID while the clone is running and is reset to zero
275 afterwards. */
276#define lll_wait_tid(tid) \
8c276674
DJ
277 do { \
278 __typeof (tid) __tid; \
279 while ((__tid = (tid)) != 0) \
280 lll_futex_wait (&(tid), __tid, LLL_SHARED); \
2568b674
AJ
281 } while (0)
282
283extern int __lll_timedwait_tid (int *, const struct timespec *)
284 attribute_hidden;
285
286#define lll_timedwait_tid(tid, abstime) \
287 ({ \
288 int __res = 0; \
289 if ((tid) != 0) \
290 __res = __lll_timedwait_tid (&(tid), (abstime)); \
291 __res; \
292 })
293
2568b674 294#endif /* lowlevellock.h */
This page took 0.097521 seconds and 5 git commands to generate.