* tst-robust9.c (do_test): Don't fail if ENABLE_PI and
[glibc.git] / nptl / pthread_mutex_trylock.c
blob9db904c60ba15024d93a23927d57d4f47e7624cb
1 /* Copyright (C) 2002, 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
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. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include "pthreadP.h"
24 #include <lowlevellock.h>
27 int
28 __pthread_mutex_trylock (mutex)
29 pthread_mutex_t *mutex;
31 int oldval;
32 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
34 switch (__builtin_expect (mutex->__data.__kind, PTHREAD_MUTEX_TIMED_NP))
36 /* Recursive mutex. */
37 case PTHREAD_MUTEX_RECURSIVE_NP:
38 /* Check whether we already hold the mutex. */
39 if (mutex->__data.__owner == id)
41 /* Just bump the counter. */
42 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
43 /* Overflow of the counter. */
44 return EAGAIN;
46 ++mutex->__data.__count;
47 return 0;
50 if (lll_mutex_trylock (mutex->__data.__lock) == 0)
52 /* Record the ownership. */
53 mutex->__data.__owner = id;
54 mutex->__data.__count = 1;
55 ++mutex->__data.__nusers;
56 return 0;
58 break;
60 case PTHREAD_MUTEX_ERRORCHECK_NP:
61 case PTHREAD_MUTEX_TIMED_NP:
62 case PTHREAD_MUTEX_ADAPTIVE_NP:
63 /* Normal mutex. */
64 if (lll_mutex_trylock (mutex->__data.__lock) != 0)
65 break;
67 /* Record the ownership. */
68 mutex->__data.__owner = id;
69 ++mutex->__data.__nusers;
71 return 0;
73 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
74 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
75 case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
76 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
77 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
78 &mutex->__data.__list.__next);
80 oldval = mutex->__data.__lock;
83 again:
84 if ((oldval & FUTEX_OWNER_DIED) != 0)
86 /* The previous owner died. Try locking the mutex. */
87 int newval = id | (oldval & FUTEX_WAITERS);
89 newval
90 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
91 newval, oldval);
93 if (newval != oldval)
95 oldval = newval;
96 goto again;
99 /* We got the mutex. */
100 mutex->__data.__count = 1;
101 /* But it is inconsistent unless marked otherwise. */
102 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
104 ENQUEUE_MUTEX (mutex);
105 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
107 /* Note that we deliberately exist here. If we fall
108 through to the end of the function __nusers would be
109 incremented which is not correct because the old
110 owner has to be discounted. */
111 return EOWNERDEAD;
114 /* Check whether we already hold the mutex. */
115 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
117 if (mutex->__data.__kind
118 == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
120 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
121 NULL);
122 return EDEADLK;
125 if (mutex->__data.__kind
126 == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
128 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
129 NULL);
131 /* Just bump the counter. */
132 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
133 /* Overflow of the counter. */
134 return EAGAIN;
136 ++mutex->__data.__count;
138 return 0;
142 oldval = lll_robust_mutex_trylock (mutex->__data.__lock, id);
143 if (oldval != 0 && (oldval & FUTEX_OWNER_DIED) == 0)
145 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
147 return EBUSY;
150 if (__builtin_expect (mutex->__data.__owner
151 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
153 /* This mutex is now not recoverable. */
154 mutex->__data.__count = 0;
155 if (oldval == id)
156 lll_mutex_unlock (mutex->__data.__lock);
157 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
158 return ENOTRECOVERABLE;
161 while ((oldval & FUTEX_OWNER_DIED) != 0);
163 ENQUEUE_MUTEX (mutex);
164 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
166 mutex->__data.__owner = id;
167 ++mutex->__data.__nusers;
168 mutex->__data.__count = 1;
170 return 0;
172 case PTHREAD_MUTEX_PI_RECURSIVE_NP:
173 case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
174 case PTHREAD_MUTEX_PI_NORMAL_NP:
175 case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
176 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
177 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
178 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
179 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
181 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
182 int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
184 if (robust)
185 /* Note: robust PI futexes are signaled by setting bit 0. */
186 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
187 (void *) (((uintptr_t) &mutex->__data.__list.__next)
188 | 1));
190 oldval = mutex->__data.__lock;
192 /* Check whether we already hold the mutex. */
193 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
195 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
197 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
198 return EDEADLK;
201 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
203 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
205 /* Just bump the counter. */
206 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
207 /* Overflow of the counter. */
208 return EAGAIN;
210 ++mutex->__data.__count;
212 return 0;
216 oldval
217 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
218 id, 0);
220 if (oldval != 0)
222 if ((oldval & FUTEX_OWNER_DIED) == 0)
224 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
226 return EBUSY;
229 assert (robust);
231 /* The mutex owner died. The kernel will now take care of
232 everything. */
233 INTERNAL_SYSCALL_DECL (__err);
234 int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
235 FUTEX_TRYLOCK_PI, 0, 0);
237 if (INTERNAL_SYSCALL_ERROR_P (e, __err)
238 && INTERNAL_SYSCALL_ERRNO (e, __err) == EWOULDBLOCK)
240 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
242 return EBUSY;
245 oldval = mutex->__data.__lock;
248 if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
250 atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
252 /* We got the mutex. */
253 mutex->__data.__count = 1;
254 /* But it is inconsistent unless marked otherwise. */
255 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
257 ENQUEUE_MUTEX (mutex);
258 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
260 /* Note that we deliberately exit here. If we fall
261 through to the end of the function __nusers would be
262 incremented which is not correct because the old owner
263 has to be discounted. */
264 return EOWNERDEAD;
267 if (robust
268 && __builtin_expect (mutex->__data.__owner
269 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
271 /* This mutex is now not recoverable. */
272 mutex->__data.__count = 0;
274 INTERNAL_SYSCALL_DECL (__err);
275 INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
276 FUTEX_UNLOCK_PI, 0, 0);
278 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
279 return ENOTRECOVERABLE;
282 if (robust)
284 ENQUEUE_MUTEX_PI (mutex);
285 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
288 mutex->__data.__owner = id;
289 ++mutex->__data.__nusers;
290 mutex->__data.__count = 1;
292 return 0;
295 case PTHREAD_MUTEX_PP_RECURSIVE_NP:
296 case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
297 case PTHREAD_MUTEX_PP_NORMAL_NP:
298 case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
300 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
302 oldval = mutex->__data.__lock;
304 /* Check whether we already hold the mutex. */
305 if (mutex->__data.__owner == id)
307 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
308 return EDEADLK;
310 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
312 /* Just bump the counter. */
313 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
314 /* Overflow of the counter. */
315 return EAGAIN;
317 ++mutex->__data.__count;
319 return 0;
323 int oldprio = -1, ceilval;
326 int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
327 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
329 if (__pthread_current_priority () > ceiling)
331 if (oldprio != -1)
332 __pthread_tpp_change_priority (oldprio, -1);
333 return EINVAL;
336 int retval = __pthread_tpp_change_priority (oldprio, ceiling);
337 if (retval)
338 return retval;
340 ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
341 oldprio = ceiling;
343 oldval
344 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
345 ceilval | 1, ceilval);
347 if (oldval == ceilval)
348 break;
350 while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
352 if (oldval != ceilval)
354 __pthread_tpp_change_priority (oldprio, -1);
355 break;
358 assert (mutex->__data.__owner == 0);
359 /* Record the ownership. */
360 mutex->__data.__owner = id;
361 ++mutex->__data.__nusers;
362 mutex->__data.__count = 1;
364 return 0;
366 break;
368 default:
369 /* Correct code cannot set any other type. */
370 return EINVAL;
373 return EBUSY;
375 strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)