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