]>
Commit | Line | Data |
---|---|---|
b03b0c29 | 1 | /* Copyright (C) 2003, 2004, 2006, 2007 Free Software Foundation, Inc. |
cd2fbe58 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2003. | |
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 | |
16 | License along with the GNU C Libr \ary; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
19 | ||
20 | #ifndef _LOWLEVELLOCK_H | |
21 | #define _LOWLEVELLOCK_H 1 | |
22 | ||
23 | #include <time.h> | |
24 | #include <sys/param.h> | |
25 | #include <bits/pthreadtypes.h> | |
26 | #include <atomic.h> | |
27 | ||
28 | ||
29 | #define FUTEX_WAIT 0 | |
30 | #define FUTEX_WAKE 1 | |
31 | #define FUTEX_REQUEUE 3 | |
75fccede | 32 | #define FUTEX_CMP_REQUEUE 4 |
b9b8cf03 UD |
33 | #define FUTEX_WAKE_OP 5 |
34 | #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1) | |
d5ba53f9 UD |
35 | #define FUTEX_LOCK_PI 6 |
36 | #define FUTEX_UNLOCK_PI 7 | |
37 | #define FUTEX_TRYLOCK_PI 8 | |
cd2fbe58 UD |
38 | |
39 | /* Initializer for compatibility lock. */ | |
40 | #define LLL_MUTEX_LOCK_INITIALIZER (0) | |
41 | ||
42 | #define lll_futex_wait(futexp, val) \ | |
43 | ({ \ | |
44 | INTERNAL_SYSCALL_DECL (__err); \ | |
45 | long int __ret; \ | |
46 | \ | |
47 | __ret = INTERNAL_SYSCALL (futex, __err, 4, \ | |
48 | (futexp), FUTEX_WAIT, (val), 0); \ | |
49 | __ret; \ | |
50 | }) | |
51 | ||
52 | #define lll_futex_timed_wait(futexp, val, timespec) \ | |
53 | ({ \ | |
54 | INTERNAL_SYSCALL_DECL (__err); \ | |
55 | long int __ret; \ | |
56 | \ | |
57 | __ret = INTERNAL_SYSCALL (futex, __err, 4, \ | |
58 | (futexp), FUTEX_WAIT, (val), (timespec)); \ | |
59 | __ret; \ | |
60 | }) | |
61 | ||
62 | #define lll_futex_wake(futexp, nr) \ | |
63 | ({ \ | |
64 | INTERNAL_SYSCALL_DECL (__err); \ | |
65 | long int __ret; \ | |
66 | \ | |
67 | __ret = INTERNAL_SYSCALL (futex, __err, 4, \ | |
68 | (futexp), FUTEX_WAKE, (nr), 0); \ | |
69 | __ret; \ | |
70 | }) | |
71 | ||
75fccede UD |
72 | /* Returns non-zero if error happened, zero if success. */ |
73 | #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \ | |
cd2fbe58 UD |
74 | ({ \ |
75 | INTERNAL_SYSCALL_DECL (__err); \ | |
76 | long int __ret; \ | |
77 | \ | |
75fccede UD |
78 | __ret = INTERNAL_SYSCALL (futex, __err, 6, \ |
79 | (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \ | |
80 | (nr_move), (mutex), (val)); \ | |
81 | INTERNAL_SYSCALL_ERROR_P (__ret, __err); \ | |
cd2fbe58 UD |
82 | }) |
83 | ||
c4a4875d RM |
84 | #define lll_robust_mutex_dead(futexv) \ |
85 | do \ | |
86 | { \ | |
87 | int *__futexp = &(futexv); \ | |
88 | atomic_or (__futexp, FUTEX_OWNER_DIED); \ | |
89 | lll_futex_wake (__futexp, 1); \ | |
90 | } \ | |
91 | while (0) | |
92 | ||
b9b8cf03 | 93 | /* Returns non-zero if error happened, zero if success. */ |
b01fe5f7 UD |
94 | #ifdef __sparc32_atomic_do_lock |
95 | /* Avoid FUTEX_WAKE_OP if supporting pre-v9 CPUs. */ | |
96 | # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) 1 | |
97 | #else | |
98 | # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) \ | |
b9b8cf03 UD |
99 | ({ \ |
100 | INTERNAL_SYSCALL_DECL (__err); \ | |
101 | long int __ret; \ | |
102 | \ | |
103 | __ret = INTERNAL_SYSCALL (futex, __err, 6, \ | |
104 | (futexp), FUTEX_WAKE_OP, (nr_wake), \ | |
105 | (nr_wake2), (futexp2), \ | |
106 | FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \ | |
107 | INTERNAL_SYSCALL_ERROR_P (__ret, __err); \ | |
108 | }) | |
cd2fbe58 UD |
109 | #endif |
110 | ||
111 | static inline int | |
112 | __attribute__ ((always_inline)) | |
113 | __lll_mutex_trylock (int *futex) | |
114 | { | |
b01fe5f7 | 115 | return atomic_compare_and_exchange_val_24_acq (futex, 1, 0) != 0; |
cd2fbe58 UD |
116 | } |
117 | #define lll_mutex_trylock(futex) __lll_mutex_trylock (&(futex)) | |
118 | ||
2c0b891a UD |
119 | static inline int |
120 | __attribute__ ((always_inline)) | |
121 | __lll_mutex_cond_trylock (int *futex) | |
122 | { | |
b01fe5f7 | 123 | return atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0; |
2c0b891a UD |
124 | } |
125 | #define lll_mutex_cond_trylock(futex) __lll_mutex_cond_trylock (&(futex)) | |
126 | ||
c4a4875d RM |
127 | static inline int |
128 | __attribute__ ((always_inline)) | |
129 | __lll_robust_mutex_trylock (int *futex, int id) | |
130 | { | |
131 | return atomic_compare_and_exchange_val_acq (futex, id, 0) != 0; | |
132 | } | |
133 | #define lll_robust_mutex_trylock(futex, id) \ | |
134 | __lll_robust_mutex_trylock (&(futex), id) | |
cd2fbe58 | 135 | |
a334319f | 136 | |
c4a4875d RM |
137 | extern void __lll_lock_wait (int *futex) attribute_hidden; |
138 | extern int __lll_robust_lock_wait (int *futex) attribute_hidden; | |
cd2fbe58 UD |
139 | |
140 | static inline void | |
141 | __attribute__ ((always_inline)) | |
142 | __lll_mutex_lock (int *futex) | |
143 | { | |
b01fe5f7 | 144 | int val = atomic_compare_and_exchange_val_24_acq (futex, 1, 0); |
cd2fbe58 UD |
145 | |
146 | if (__builtin_expect (val != 0, 0)) | |
39358e8b | 147 | __lll_lock_wait (futex); |
cd2fbe58 UD |
148 | } |
149 | #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex)) | |
150 | ||
c4a4875d RM |
151 | static inline int |
152 | __attribute__ ((always_inline)) | |
153 | __lll_robust_mutex_lock (int *futex, int id) | |
154 | { | |
155 | int result = 0; | |
156 | if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0) | |
157 | result = __lll_robust_lock_wait (futex); | |
158 | return result; | |
159 | } | |
160 | #define lll_robust_mutex_lock(futex, id) \ | |
161 | __lll_robust_mutex_lock (&(futex), id) | |
cd2fbe58 UD |
162 | |
163 | static inline void | |
164 | __attribute__ ((always_inline)) | |
165 | __lll_mutex_cond_lock (int *futex) | |
166 | { | |
b01fe5f7 | 167 | int val = atomic_compare_and_exchange_val_24_acq (futex, 2, 0); |
cd2fbe58 UD |
168 | |
169 | if (__builtin_expect (val != 0, 0)) | |
39358e8b | 170 | __lll_lock_wait (futex); |
cd2fbe58 UD |
171 | } |
172 | #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex)) | |
173 | ||
c4a4875d RM |
174 | #define lll_robust_mutex_cond_lock(futex, id) \ |
175 | __lll_robust_mutex_lock (&(futex), (id) | FUTEX_WAITERS) | |
176 | ||
cd2fbe58 | 177 | |
39358e8b | 178 | extern int __lll_timedlock_wait (int *futex, const struct timespec *) |
cd2fbe58 | 179 | attribute_hidden; |
c4a4875d RM |
180 | extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *) |
181 | attribute_hidden; | |
cd2fbe58 UD |
182 | |
183 | static inline int | |
184 | __attribute__ ((always_inline)) | |
185 | __lll_mutex_timedlock (int *futex, const struct timespec *abstime) | |
186 | { | |
b01fe5f7 | 187 | int val = atomic_compare_and_exchange_val_24_acq (futex, 1, 0); |
cd2fbe58 | 188 | int result = 0; |
2c0b891a | 189 | |
cd2fbe58 | 190 | if (__builtin_expect (val != 0, 0)) |
39358e8b | 191 | result = __lll_timedlock_wait (futex, abstime); |
cd2fbe58 UD |
192 | return result; |
193 | } | |
194 | #define lll_mutex_timedlock(futex, abstime) \ | |
195 | __lll_mutex_timedlock (&(futex), abstime) | |
196 | ||
c4a4875d RM |
197 | static inline int |
198 | __attribute__ ((always_inline)) | |
199 | __lll_robust_mutex_timedlock (int *futex, const struct timespec *abstime, | |
200 | int id) | |
201 | { | |
202 | int result = 0; | |
203 | if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0) | |
204 | result = __lll_robust_timedlock_wait (futex, abstime); | |
205 | return result; | |
206 | } | |
207 | #define lll_robust_mutex_timedlock(futex, abstime, id) \ | |
208 | __lll_robust_mutex_timedlock (&(futex), abstime, id) | |
209 | ||
cd2fbe58 | 210 | #define lll_mutex_unlock(lock) \ |
0ecb606c JJ |
211 | ((void) ({ \ |
212 | int *__futex = &(lock); \ | |
b01fe5f7 | 213 | int __val = atomic_exchange_24_rel (__futex, 0); \ |
a334319f | 214 | if (__builtin_expect (__val > 1, 0)) \ |
0ecb606c JJ |
215 | lll_futex_wake (__futex, 1); \ |
216 | })) | |
217 | ||
c4a4875d RM |
218 | #define lll_robust_mutex_unlock(lock) \ |
219 | ((void) ({ \ | |
220 | int *__futex = &(lock); \ | |
221 | int __val = atomic_exchange_rel (__futex, 0); \ | |
222 | if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \ | |
223 | lll_futex_wake (__futex, 1); \ | |
224 | })) | |
225 | ||
cd2fbe58 UD |
226 | #define lll_mutex_unlock_force(lock) \ |
227 | ((void) ({ \ | |
228 | int *__futex = &(lock); \ | |
b01fe5f7 | 229 | (void) atomic_exchange_24_rel (__futex, 0); \ |
cd2fbe58 UD |
230 | lll_futex_wake (__futex, 1); \ |
231 | })) | |
232 | ||
233 | #define lll_mutex_islocked(futex) \ | |
234 | (futex != 0) | |
235 | ||
236 | ||
237 | /* We have a separate internal lock implementation which is not tied | |
238 | to binary compatibility. We can use the lll_mutex_*. */ | |
239 | ||
240 | /* Type for lock object. */ | |
241 | typedef int lll_lock_t; | |
242 | ||
cd2fbe58 UD |
243 | /* Initializers for lock. */ |
244 | #define LLL_LOCK_INITIALIZER (0) | |
245 | #define LLL_LOCK_INITIALIZER_LOCKED (1) | |
246 | ||
247 | #define lll_trylock(futex) lll_mutex_trylock (futex) | |
248 | #define lll_lock(futex) lll_mutex_lock (futex) | |
249 | #define lll_unlock(futex) lll_mutex_unlock (futex) | |
250 | #define lll_islocked(futex) lll_mutex_islocked (futex) | |
251 | ||
252 | ||
253 | /* The kernel notifies a process with uses CLONE_CLEARTID via futex | |
254 | wakeup when the clone terminates. The memory location contains the | |
255 | thread ID while the clone is running and is reset to zero | |
256 | afterwards. */ | |
257 | #define lll_wait_tid(tid) \ | |
258 | do \ | |
259 | { \ | |
260 | __typeof (tid) __tid; \ | |
261 | while ((__tid = (tid)) != 0) \ | |
262 | lll_futex_wait (&(tid), __tid); \ | |
263 | } \ | |
264 | while (0) | |
265 | ||
266 | extern int __lll_timedwait_tid (int *, const struct timespec *) | |
267 | attribute_hidden; | |
268 | ||
269 | #define lll_timedwait_tid(tid, abstime) \ | |
270 | ({ \ | |
271 | int __res = 0; \ | |
272 | if ((tid) != 0) \ | |
273 | __res = __lll_timedwait_tid (&(tid), (abstime)); \ | |
274 | __res; \ | |
275 | }) | |
276 | ||
277 | ||
278 | /* Conditional variable handling. */ | |
279 | ||
280 | extern void __lll_cond_wait (pthread_cond_t *cond) | |
281 | attribute_hidden; | |
282 | extern int __lll_cond_timedwait (pthread_cond_t *cond, | |
283 | const struct timespec *abstime) | |
284 | attribute_hidden; | |
285 | extern void __lll_cond_wake (pthread_cond_t *cond) | |
286 | attribute_hidden; | |
287 | extern void __lll_cond_broadcast (pthread_cond_t *cond) | |
288 | attribute_hidden; | |
289 | ||
290 | #define lll_cond_wait(cond) \ | |
291 | __lll_cond_wait (cond) | |
292 | #define lll_cond_timedwait(cond, abstime) \ | |
293 | __lll_cond_timedwait (cond, abstime) | |
294 | #define lll_cond_wake(cond) \ | |
295 | __lll_cond_wake (cond) | |
296 | #define lll_cond_broadcast(cond) \ | |
297 | __lll_cond_broadcast (cond) | |
298 | ||
299 | #endif /* lowlevellock.h */ |