]>
Commit | Line | Data |
---|---|---|
1bb5f5a1 | 1 | /* Copyright (C) 2002, 2003, 2005, 2006, 2007 Free Software Foundation, Inc. |
76a50749 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
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 Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
19 | ||
1bcfb5a5 | 20 | #include <assert.h> |
76a50749 | 21 | #include <errno.h> |
1bcfb5a5 | 22 | #include <stdlib.h> |
76a50749 UD |
23 | #include "pthreadP.h" |
24 | #include <lowlevellock.h> | |
25 | ||
26 | ||
27 | int | |
28 | __pthread_mutex_trylock (mutex) | |
29 | pthread_mutex_t *mutex; | |
30 | { | |
683040c3 | 31 | int oldval; |
1bcfb5a5 | 32 | pid_t id = THREAD_GETMEM (THREAD_SELF, tid); |
76a50749 | 33 | |
ae1ad3ae UD |
34 | switch (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex), |
35 | PTHREAD_MUTEX_TIMED_NP)) | |
76a50749 UD |
36 | { |
37 | /* Recursive mutex. */ | |
38 | case PTHREAD_MUTEX_RECURSIVE_NP: | |
39 | /* Check whether we already hold the mutex. */ | |
9a7178d6 | 40 | if (mutex->__data.__owner == id) |
76a50749 UD |
41 | { |
42 | /* Just bump the counter. */ | |
0a37669a UD |
43 | if (__builtin_expect (mutex->__data.__count + 1 == 0, 0)) |
44 | /* Overflow of the counter. */ | |
45 | return EAGAIN; | |
46 | ||
76a50749 UD |
47 | ++mutex->__data.__count; |
48 | return 0; | |
49 | } | |
50 | ||
e51deae7 | 51 | if (lll_trylock (mutex->__data.__lock) == 0) |
76a50749 UD |
52 | { |
53 | /* Record the ownership. */ | |
9a7178d6 | 54 | mutex->__data.__owner = id; |
76a50749 | 55 | mutex->__data.__count = 1; |
61623643 | 56 | ++mutex->__data.__nusers; |
76a50749 UD |
57 | return 0; |
58 | } | |
59 | break; | |
60 | ||
61 | case PTHREAD_MUTEX_ERRORCHECK_NP: | |
76a50749 UD |
62 | case PTHREAD_MUTEX_TIMED_NP: |
63 | case PTHREAD_MUTEX_ADAPTIVE_NP: | |
64 | /* Normal mutex. */ | |
e51deae7 | 65 | if (lll_trylock (mutex->__data.__lock) != 0) |
1bcfb5a5 UD |
66 | break; |
67 | ||
68 | /* Record the ownership. */ | |
69 | mutex->__data.__owner = id; | |
70 | ++mutex->__data.__nusers; | |
71 | ||
72 | return 0; | |
73 | ||
0f6699ea UD |
74 | case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP: |
75 | case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP: | |
76 | case PTHREAD_MUTEX_ROBUST_NORMAL_NP: | |
77 | case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP: | |
78 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, | |
79 | &mutex->__data.__list.__next); | |
80 | ||
683040c3 UD |
81 | oldval = mutex->__data.__lock; |
82 | do | |
1bcfb5a5 | 83 | { |
0f6699ea | 84 | again: |
683040c3 UD |
85 | if ((oldval & FUTEX_OWNER_DIED) != 0) |
86 | { | |
87 | /* The previous owner died. Try locking the mutex. */ | |
113ad5fc UD |
88 | int newval = id | (oldval & FUTEX_WAITERS); |
89 | ||
90 | newval | |
0f6699ea | 91 | = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock, |
113ad5fc | 92 | newval, oldval); |
0f6699ea UD |
93 | |
94 | if (newval != oldval) | |
683040c3 | 95 | { |
683040c3 | 96 | oldval = newval; |
0f6699ea | 97 | goto again; |
683040c3 UD |
98 | } |
99 | ||
100 | /* We got the mutex. */ | |
101 | mutex->__data.__count = 1; | |
102 | /* But it is inconsistent unless marked otherwise. */ | |
103 | mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT; | |
104 | ||
105 | ENQUEUE_MUTEX (mutex); | |
0f6699ea | 106 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); |
683040c3 UD |
107 | |
108 | /* Note that we deliberately exist here. If we fall | |
109 | through to the end of the function __nusers would be | |
110 | incremented which is not correct because the old | |
111 | owner has to be discounted. */ | |
112 | return EOWNERDEAD; | |
113 | } | |
114 | ||
683040c3 | 115 | /* Check whether we already hold the mutex. */ |
0f6699ea | 116 | if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0)) |
683040c3 | 117 | { |
5bd8a249 UD |
118 | int kind = PTHREAD_MUTEX_TYPE (mutex); |
119 | if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP) | |
0f6699ea UD |
120 | { |
121 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, | |
122 | NULL); | |
123 | return EDEADLK; | |
124 | } | |
683040c3 | 125 | |
5bd8a249 | 126 | if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP) |
683040c3 | 127 | { |
0f6699ea UD |
128 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, |
129 | NULL); | |
130 | ||
683040c3 UD |
131 | /* Just bump the counter. */ |
132 | if (__builtin_expect (mutex->__data.__count + 1 == 0, 0)) | |
133 | /* Overflow of the counter. */ | |
134 | return EAGAIN; | |
135 | ||
136 | ++mutex->__data.__count; | |
137 | ||
138 | return 0; | |
139 | } | |
140 | } | |
141 | ||
e51deae7 | 142 | oldval = lll_robust_trylock (mutex->__data.__lock, id); |
683040c3 | 143 | if (oldval != 0 && (oldval & FUTEX_OWNER_DIED) == 0) |
0f6699ea UD |
144 | { |
145 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); | |
146 | ||
147 | return EBUSY; | |
148 | } | |
683040c3 | 149 | |
683040c3 UD |
150 | if (__builtin_expect (mutex->__data.__owner |
151 | == PTHREAD_MUTEX_NOTRECOVERABLE, 0)) | |
152 | { | |
153 | /* This mutex is now not recoverable. */ | |
154 | mutex->__data.__count = 0; | |
155 | if (oldval == id) | |
5bd8a249 UD |
156 | lll_unlock (mutex->__data.__lock, |
157 | PTHREAD_ROBUST_MUTEX_PSHARED (mutex)); | |
0f6699ea | 158 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); |
683040c3 UD |
159 | return ENOTRECOVERABLE; |
160 | } | |
1bcfb5a5 | 161 | } |
683040c3 | 162 | while ((oldval & FUTEX_OWNER_DIED) != 0); |
1bcfb5a5 UD |
163 | |
164 | ENQUEUE_MUTEX (mutex); | |
0f6699ea | 165 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); |
1bcfb5a5 UD |
166 | |
167 | mutex->__data.__owner = id; | |
168 | ++mutex->__data.__nusers; | |
683040c3 UD |
169 | mutex->__data.__count = 1; |
170 | ||
171 | return 0; | |
1bcfb5a5 | 172 | |
df47504c UD |
173 | case PTHREAD_MUTEX_PI_RECURSIVE_NP: |
174 | case PTHREAD_MUTEX_PI_ERRORCHECK_NP: | |
175 | case PTHREAD_MUTEX_PI_NORMAL_NP: | |
176 | case PTHREAD_MUTEX_PI_ADAPTIVE_NP: | |
177 | case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP: | |
178 | case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP: | |
179 | case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP: | |
180 | case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP: | |
181 | { | |
182 | int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP; | |
183 | int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP; | |
184 | ||
185 | if (robust) | |
186 | /* Note: robust PI futexes are signaled by setting bit 0. */ | |
187 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, | |
188 | (void *) (((uintptr_t) &mutex->__data.__list.__next) | |
189 | | 1)); | |
190 | ||
191 | oldval = mutex->__data.__lock; | |
192 | ||
193 | /* Check whether we already hold the mutex. */ | |
194 | if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0)) | |
195 | { | |
196 | if (kind == PTHREAD_MUTEX_ERRORCHECK_NP) | |
197 | { | |
198 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); | |
199 | return EDEADLK; | |
200 | } | |
201 | ||
202 | if (kind == PTHREAD_MUTEX_RECURSIVE_NP) | |
203 | { | |
204 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); | |
205 | ||
206 | /* Just bump the counter. */ | |
207 | if (__builtin_expect (mutex->__data.__count + 1 == 0, 0)) | |
208 | /* Overflow of the counter. */ | |
209 | return EAGAIN; | |
210 | ||
211 | ++mutex->__data.__count; | |
212 | ||
213 | return 0; | |
214 | } | |
215 | } | |
216 | ||
217 | oldval | |
218 | = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock, | |
219 | id, 0); | |
220 | ||
221 | if (oldval != 0) | |
222 | { | |
223 | if ((oldval & FUTEX_OWNER_DIED) == 0) | |
224 | { | |
225 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); | |
226 | ||
227 | return EBUSY; | |
228 | } | |
229 | ||
230 | assert (robust); | |
231 | ||
232 | /* The mutex owner died. The kernel will now take care of | |
233 | everything. */ | |
234 | INTERNAL_SYSCALL_DECL (__err); | |
235 | int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock, | |
236 | FUTEX_TRYLOCK_PI, 0, 0); | |
237 | ||
238 | if (INTERNAL_SYSCALL_ERROR_P (e, __err) | |
239 | && INTERNAL_SYSCALL_ERRNO (e, __err) == EWOULDBLOCK) | |
240 | { | |
241 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); | |
242 | ||
243 | return EBUSY; | |
244 | } | |
245 | ||
246 | oldval = mutex->__data.__lock; | |
247 | } | |
248 | ||
249 | if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0)) | |
250 | { | |
251 | atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED); | |
252 | ||
253 | /* We got the mutex. */ | |
254 | mutex->__data.__count = 1; | |
255 | /* But it is inconsistent unless marked otherwise. */ | |
256 | mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT; | |
257 | ||
258 | ENQUEUE_MUTEX (mutex); | |
259 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); | |
260 | ||
261 | /* Note that we deliberately exit here. If we fall | |
262 | through to the end of the function __nusers would be | |
263 | incremented which is not correct because the old owner | |
264 | has to be discounted. */ | |
265 | return EOWNERDEAD; | |
266 | } | |
267 | ||
268 | if (robust | |
269 | && __builtin_expect (mutex->__data.__owner | |
270 | == PTHREAD_MUTEX_NOTRECOVERABLE, 0)) | |
271 | { | |
272 | /* This mutex is now not recoverable. */ | |
273 | mutex->__data.__count = 0; | |
274 | ||
275 | INTERNAL_SYSCALL_DECL (__err); | |
276 | INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock, | |
277 | FUTEX_UNLOCK_PI, 0, 0); | |
278 | ||
279 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); | |
280 | return ENOTRECOVERABLE; | |
281 | } | |
282 | ||
283 | if (robust) | |
284 | { | |
285 | ENQUEUE_MUTEX_PI (mutex); | |
286 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); | |
287 | } | |
288 | ||
289 | mutex->__data.__owner = id; | |
290 | ++mutex->__data.__nusers; | |
291 | mutex->__data.__count = 1; | |
292 | ||
293 | return 0; | |
294 | } | |
295 | ||
f17efcb4 UD |
296 | case PTHREAD_MUTEX_PP_RECURSIVE_NP: |
297 | case PTHREAD_MUTEX_PP_ERRORCHECK_NP: | |
298 | case PTHREAD_MUTEX_PP_NORMAL_NP: | |
299 | case PTHREAD_MUTEX_PP_ADAPTIVE_NP: | |
300 | { | |
301 | int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP; | |
302 | ||
303 | oldval = mutex->__data.__lock; | |
304 | ||
305 | /* Check whether we already hold the mutex. */ | |
306 | if (mutex->__data.__owner == id) | |
307 | { | |
308 | if (kind == PTHREAD_MUTEX_ERRORCHECK_NP) | |
309 | return EDEADLK; | |
310 | ||
311 | if (kind == PTHREAD_MUTEX_RECURSIVE_NP) | |
312 | { | |
313 | /* Just bump the counter. */ | |
314 | if (__builtin_expect (mutex->__data.__count + 1 == 0, 0)) | |
315 | /* Overflow of the counter. */ | |
316 | return EAGAIN; | |
317 | ||
318 | ++mutex->__data.__count; | |
319 | ||
320 | return 0; | |
321 | } | |
322 | } | |
323 | ||
324 | int oldprio = -1, ceilval; | |
325 | do | |
326 | { | |
327 | int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) | |
328 | >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT; | |
329 | ||
330 | if (__pthread_current_priority () > ceiling) | |
331 | { | |
332 | if (oldprio != -1) | |
333 | __pthread_tpp_change_priority (oldprio, -1); | |
334 | return EINVAL; | |
335 | } | |
336 | ||
337 | int retval = __pthread_tpp_change_priority (oldprio, ceiling); | |
338 | if (retval) | |
339 | return retval; | |
340 | ||
341 | ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT; | |
342 | oldprio = ceiling; | |
343 | ||
344 | oldval | |
345 | = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock, | |
346 | ceilval | 1, ceilval); | |
347 | ||
348 | if (oldval == ceilval) | |
349 | break; | |
350 | } | |
351 | while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval); | |
352 | ||
353 | if (oldval != ceilval) | |
354 | { | |
355 | __pthread_tpp_change_priority (oldprio, -1); | |
356 | break; | |
357 | } | |
358 | ||
359 | assert (mutex->__data.__owner == 0); | |
360 | /* Record the ownership. */ | |
361 | mutex->__data.__owner = id; | |
362 | ++mutex->__data.__nusers; | |
363 | mutex->__data.__count = 1; | |
364 | ||
365 | return 0; | |
366 | } | |
367 | break; | |
368 | ||
dcc73a8d UD |
369 | default: |
370 | /* Correct code cannot set any other type. */ | |
371 | return EINVAL; | |
76a50749 UD |
372 | } |
373 | ||
374 | return EBUSY; | |
375 | } | |
376 | strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock) |