]>
Commit | Line | Data |
---|---|---|
30efab51 | 1 | /* Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. |
2568b674 AJ |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU Lesser General Public | |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Lesser General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
15 | License along with the GNU C Library; if not, write to the Free | |
16 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
17 | 02111-1307 USA. */ | |
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> | |
27 | ||
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 | |
30efab51 | 38 | #define FUTEX_PRIVATE_FLAG 128 |
2568b674 AJ |
39 | |
40 | /* Initializer for compatibility lock. */ | |
41 | #define LLL_MUTEX_LOCK_INITIALIZER (0) | |
42 | ||
43 | #define lll_futex_wait(futexp, val) \ | |
44 | ({ \ | |
45 | INTERNAL_SYSCALL_DECL (__err); \ | |
46 | long int __ret; \ | |
47 | __ret = INTERNAL_SYSCALL (futex, __err, 4, \ | |
6428ce3c | 48 | (long) (futexp), FUTEX_WAIT, (val), 0); \ |
2568b674 AJ |
49 | INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \ |
50 | }) | |
51 | ||
52 | #define lll_futex_timed_wait(futexp, val, timespec) \ | |
53 | ({ \ | |
54 | INTERNAL_SYSCALL_DECL (__err); \ | |
55 | long int __ret; \ | |
56 | __ret = INTERNAL_SYSCALL (futex, __err, 4, \ | |
6428ce3c | 57 | (long) (futexp), FUTEX_WAIT, (val), (timespec));\ |
2568b674 AJ |
58 | INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \ |
59 | }) | |
60 | ||
61 | #define lll_futex_wake(futexp, nr) \ | |
62 | ({ \ | |
63 | INTERNAL_SYSCALL_DECL (__err); \ | |
64 | long int __ret; \ | |
65 | __ret = INTERNAL_SYSCALL (futex, __err, 4, \ | |
6428ce3c | 66 | (long) (futexp), FUTEX_WAKE, (nr), 0); \ |
2568b674 AJ |
67 | INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \ |
68 | }) | |
69 | ||
13d7881a DJ |
70 | #define lll_robust_mutex_dead(futexv) \ |
71 | do \ | |
72 | { \ | |
73 | int *__futexp = &(futexv); \ | |
74 | atomic_or (__futexp, FUTEX_OWNER_DIED); \ | |
75 | lll_futex_wake (__futexp, 1); \ | |
76 | } \ | |
77 | while (0) | |
78 | ||
2568b674 AJ |
79 | /* Returns non-zero if error happened, zero if success. */ |
80 | #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \ | |
81 | ({ \ | |
82 | INTERNAL_SYSCALL_DECL (__err); \ | |
83 | long int __ret; \ | |
84 | __ret = INTERNAL_SYSCALL (futex, __err, 6, \ | |
6428ce3c | 85 | (long) (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \ |
2568b674 AJ |
86 | (nr_move), (mutex), (val)); \ |
87 | INTERNAL_SYSCALL_ERROR_P (__ret, __err); \ | |
88 | }) | |
89 | ||
13d7881a DJ |
90 | /* Returns non-zero if error happened, zero if success. */ |
91 | #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) \ | |
92 | ({ \ | |
93 | INTERNAL_SYSCALL_DECL (__err); \ | |
94 | long int __ret; \ | |
95 | \ | |
96 | __ret = INTERNAL_SYSCALL (futex, __err, 6, \ | |
97 | (futexp), FUTEX_WAKE_OP, (nr_wake), \ | |
98 | (nr_wake2), (futexp2), \ | |
99 | FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \ | |
100 | INTERNAL_SYSCALL_ERROR_P (__ret, __err); \ | |
101 | }) | |
2568b674 AJ |
102 | |
103 | static inline int __attribute__((always_inline)) | |
104 | __lll_mutex_trylock(int *futex) | |
105 | { | |
106 | return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0; | |
107 | } | |
108 | #define lll_mutex_trylock(lock) __lll_mutex_trylock (&(lock)) | |
109 | ||
110 | ||
111 | static inline int __attribute__((always_inline)) | |
112 | __lll_mutex_cond_trylock(int *futex) | |
113 | { | |
114 | return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0; | |
115 | } | |
116 | #define lll_mutex_cond_trylock(lock) __lll_mutex_cond_trylock (&(lock)) | |
117 | ||
118 | ||
13d7881a DJ |
119 | static inline int __attribute__((always_inline)) |
120 | __lll_robust_mutex_trylock(int *futex, int id) | |
121 | { | |
122 | return atomic_compare_and_exchange_val_acq (futex, id, 0) != 0; | |
123 | } | |
124 | #define lll_robust_mutex_trylock(lock, id) \ | |
125 | __lll_robust_mutex_trylock (&(lock), id) | |
126 | ||
2568b674 | 127 | extern void __lll_lock_wait (int *futex) attribute_hidden; |
13d7881a | 128 | extern int __lll_robust_lock_wait (int *futex) attribute_hidden; |
2568b674 AJ |
129 | |
130 | static inline void __attribute__((always_inline)) | |
131 | __lll_mutex_lock(int *futex) | |
132 | { | |
133 | if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0) | |
134 | __lll_lock_wait (futex); | |
135 | } | |
136 | #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex)) | |
137 | ||
138 | ||
13d7881a DJ |
139 | static inline int __attribute__ ((always_inline)) |
140 | __lll_robust_mutex_lock (int *futex, int id) | |
141 | { | |
142 | int result = 0; | |
143 | if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0) | |
144 | result = __lll_robust_lock_wait (futex); | |
145 | return result; | |
146 | } | |
147 | #define lll_robust_mutex_lock(futex, id) \ | |
148 | __lll_robust_mutex_lock (&(futex), id) | |
149 | ||
150 | ||
2568b674 AJ |
151 | static inline void __attribute__ ((always_inline)) |
152 | __lll_mutex_cond_lock (int *futex) | |
153 | { | |
154 | if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0) | |
155 | __lll_lock_wait (futex); | |
156 | } | |
157 | #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex)) | |
158 | ||
159 | ||
13d7881a DJ |
160 | #define lll_robust_mutex_cond_lock(futex, id) \ |
161 | __lll_robust_mutex_lock (&(futex), (id) | FUTEX_WAITERS) | |
162 | ||
163 | ||
2568b674 AJ |
164 | extern int __lll_timedlock_wait (int *futex, const struct timespec *) |
165 | attribute_hidden; | |
13d7881a DJ |
166 | extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *) |
167 | attribute_hidden; | |
2568b674 AJ |
168 | |
169 | static inline int __attribute__ ((always_inline)) | |
170 | __lll_mutex_timedlock (int *futex, const struct timespec *abstime) | |
171 | { | |
172 | int result = 0; | |
173 | if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0) | |
174 | result = __lll_timedlock_wait (futex, abstime); | |
175 | return result; | |
176 | } | |
177 | #define lll_mutex_timedlock(futex, abstime) \ | |
178 | __lll_mutex_timedlock (&(futex), abstime) | |
179 | ||
180 | ||
13d7881a DJ |
181 | static inline int __attribute__ ((always_inline)) |
182 | __lll_robust_mutex_timedlock (int *futex, const struct timespec *abstime, | |
183 | int id) | |
184 | { | |
185 | int result = 0; | |
186 | if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0) | |
187 | result = __lll_robust_timedlock_wait (futex, abstime); | |
188 | return result; | |
189 | } | |
190 | #define lll_robust_mutex_timedlock(futex, abstime, id) \ | |
191 | __lll_robust_mutex_timedlock (&(futex), abstime, id) | |
192 | ||
193 | ||
2568b674 AJ |
194 | static inline void __attribute__ ((always_inline)) |
195 | __lll_mutex_unlock (int *futex) | |
196 | { | |
197 | int val = atomic_exchange_rel (futex, 0); | |
198 | if (__builtin_expect (val > 1, 0)) | |
199 | lll_futex_wake (futex, 1); | |
200 | } | |
201 | #define lll_mutex_unlock(futex) __lll_mutex_unlock(&(futex)) | |
202 | ||
203 | ||
13d7881a DJ |
204 | static inline void __attribute__ ((always_inline)) |
205 | __lll_robust_mutex_unlock (int *futex, int mask) | |
206 | { | |
207 | int val = atomic_exchange_rel (futex, 0); | |
208 | if (__builtin_expect (val & mask, 0)) | |
209 | lll_futex_wake (futex, 1); | |
210 | } | |
211 | #define lll_robust_mutex_unlock(futex) \ | |
212 | __lll_robust_mutex_unlock(&(futex), FUTEX_WAITERS) | |
213 | ||
214 | ||
2568b674 AJ |
215 | static inline void __attribute__ ((always_inline)) |
216 | __lll_mutex_unlock_force (int *futex) | |
217 | { | |
218 | (void) atomic_exchange_rel (futex, 0); | |
219 | lll_futex_wake (futex, 1); | |
220 | } | |
221 | #define lll_mutex_unlock_force(futex) __lll_mutex_unlock_force(&(futex)) | |
222 | ||
223 | ||
224 | #define lll_mutex_islocked(futex) \ | |
225 | (futex != 0) | |
226 | ||
227 | ||
228 | /* Our internal lock implementation is identical to the binary-compatible | |
229 | mutex implementation. */ | |
230 | ||
231 | /* Type for lock object. */ | |
232 | typedef int lll_lock_t; | |
233 | ||
234 | /* Initializers for lock. */ | |
235 | #define LLL_LOCK_INITIALIZER (0) | |
236 | #define LLL_LOCK_INITIALIZER_LOCKED (1) | |
237 | ||
2568b674 AJ |
238 | /* The states of a lock are: |
239 | 0 - untaken | |
240 | 1 - taken by one user | |
241 | >1 - taken by more users */ | |
242 | ||
243 | #define lll_trylock(lock) lll_mutex_trylock (lock) | |
244 | #define lll_lock(lock) lll_mutex_lock (lock) | |
245 | #define lll_unlock(lock) lll_mutex_unlock (lock) | |
246 | #define lll_islocked(lock) lll_mutex_islocked (lock) | |
247 | ||
248 | /* The kernel notifies a process which uses CLONE_CLEARTID via futex | |
249 | wakeup when the clone terminates. The memory location contains the | |
250 | thread ID while the clone is running and is reset to zero | |
251 | afterwards. */ | |
252 | #define lll_wait_tid(tid) \ | |
253 | do { \ | |
254 | __typeof (tid) __tid; \ | |
255 | while ((__tid = (tid)) != 0) \ | |
256 | lll_futex_wait (&(tid), __tid); \ | |
257 | } while (0) | |
258 | ||
259 | extern int __lll_timedwait_tid (int *, const struct timespec *) | |
260 | attribute_hidden; | |
261 | ||
262 | #define lll_timedwait_tid(tid, abstime) \ | |
263 | ({ \ | |
264 | int __res = 0; \ | |
265 | if ((tid) != 0) \ | |
266 | __res = __lll_timedwait_tid (&(tid), (abstime)); \ | |
267 | __res; \ | |
268 | }) | |
269 | ||
270 | ||
271 | /* Conditional variable handling. */ | |
272 | ||
273 | extern void __lll_cond_wait (pthread_cond_t *cond) | |
274 | attribute_hidden; | |
275 | extern int __lll_cond_timedwait (pthread_cond_t *cond, | |
276 | const struct timespec *abstime) | |
277 | attribute_hidden; | |
278 | extern void __lll_cond_wake (pthread_cond_t *cond) | |
279 | attribute_hidden; | |
280 | extern void __lll_cond_broadcast (pthread_cond_t *cond) | |
281 | attribute_hidden; | |
282 | ||
283 | #define lll_cond_wait(cond) \ | |
284 | __lll_cond_wait (cond) | |
285 | #define lll_cond_timedwait(cond, abstime) \ | |
286 | __lll_cond_timedwait (cond, abstime) | |
287 | #define lll_cond_wake(cond) \ | |
288 | __lll_cond_wake (cond) | |
289 | #define lll_cond_broadcast(cond) \ | |
290 | __lll_cond_broadcast (cond) | |
291 | ||
292 | #endif /* lowlevellock.h */ |