* locales/as_IN: Fix currency_symbol, abday for Sunday, abmon for
[glibc/pb-stable.git] / nptl / pthread_mutex_trylock.c
blob4990ecd7114def59abb6f7cef4209bf2c408771b
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
88 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
89 id, oldval);
91 if (newval != oldval)
93 oldval = newval;
94 goto again;
97 /* We got the mutex. */
98 mutex->__data.__count = 1;
99 /* But it is inconsistent unless marked otherwise. */
100 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
102 ENQUEUE_MUTEX (mutex);
103 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
105 /* Note that we deliberately exist here. If we fall
106 through to the end of the function __nusers would be
107 incremented which is not correct because the old
108 owner has to be discounted. */
109 return EOWNERDEAD;
112 /* Check whether we already hold the mutex. */
113 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
115 if (mutex->__data.__kind
116 == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
118 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
119 NULL);
120 return EDEADLK;
123 if (mutex->__data.__kind
124 == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
126 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
127 NULL);
129 /* Just bump the counter. */
130 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
131 /* Overflow of the counter. */
132 return EAGAIN;
134 ++mutex->__data.__count;
136 return 0;
140 oldval = lll_robust_mutex_trylock (mutex->__data.__lock, id);
141 if (oldval != 0 && (oldval & FUTEX_OWNER_DIED) == 0)
143 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
145 return EBUSY;
148 if (__builtin_expect (mutex->__data.__owner
149 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
151 /* This mutex is now not recoverable. */
152 mutex->__data.__count = 0;
153 if (oldval == id)
154 lll_mutex_unlock (mutex->__data.__lock);
155 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
156 return ENOTRECOVERABLE;
159 while ((oldval & FUTEX_OWNER_DIED) != 0);
161 ENQUEUE_MUTEX (mutex);
162 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
164 mutex->__data.__owner = id;
165 ++mutex->__data.__nusers;
166 mutex->__data.__count = 1;
168 return 0;
170 case PTHREAD_MUTEX_PI_RECURSIVE_NP:
171 case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
172 case PTHREAD_MUTEX_PI_NORMAL_NP:
173 case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
174 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
175 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
176 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
177 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
179 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
180 int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
182 if (robust)
183 /* Note: robust PI futexes are signaled by setting bit 0. */
184 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
185 (void *) (((uintptr_t) &mutex->__data.__list.__next)
186 | 1));
188 oldval = mutex->__data.__lock;
190 /* Check whether we already hold the mutex. */
191 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
193 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
195 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
196 return EDEADLK;
199 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
201 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
203 /* Just bump the counter. */
204 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
205 /* Overflow of the counter. */
206 return EAGAIN;
208 ++mutex->__data.__count;
210 return 0;
214 oldval
215 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
216 id, 0);
218 if (oldval != 0)
220 if ((oldval & FUTEX_OWNER_DIED) == 0)
222 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
224 return EBUSY;
227 assert (robust);
229 /* The mutex owner died. The kernel will now take care of
230 everything. */
231 INTERNAL_SYSCALL_DECL (__err);
232 int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
233 FUTEX_TRYLOCK_PI, 0, 0);
235 if (INTERNAL_SYSCALL_ERROR_P (e, __err)
236 && INTERNAL_SYSCALL_ERRNO (e, __err) == EWOULDBLOCK)
238 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
240 return EBUSY;
243 oldval = mutex->__data.__lock;
246 if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
248 atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
250 /* We got the mutex. */
251 mutex->__data.__count = 1;
252 /* But it is inconsistent unless marked otherwise. */
253 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
255 ENQUEUE_MUTEX (mutex);
256 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
258 /* Note that we deliberately exit here. If we fall
259 through to the end of the function __nusers would be
260 incremented which is not correct because the old owner
261 has to be discounted. */
262 return EOWNERDEAD;
265 if (robust
266 && __builtin_expect (mutex->__data.__owner
267 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
269 /* This mutex is now not recoverable. */
270 mutex->__data.__count = 0;
272 INTERNAL_SYSCALL_DECL (__err);
273 INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
274 FUTEX_UNLOCK_PI, 0, 0);
276 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
277 return ENOTRECOVERABLE;
280 if (robust)
282 ENQUEUE_MUTEX_PI (mutex);
283 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
286 mutex->__data.__owner = id;
287 ++mutex->__data.__nusers;
288 mutex->__data.__count = 1;
290 return 0;
293 case PTHREAD_MUTEX_PP_RECURSIVE_NP:
294 case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
295 case PTHREAD_MUTEX_PP_NORMAL_NP:
296 case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
298 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
300 oldval = mutex->__data.__lock;
302 /* Check whether we already hold the mutex. */
303 if (mutex->__data.__owner == id)
305 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
306 return EDEADLK;
308 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
310 /* Just bump the counter. */
311 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
312 /* Overflow of the counter. */
313 return EAGAIN;
315 ++mutex->__data.__count;
317 return 0;
321 int oldprio = -1, ceilval;
324 int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
325 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
327 if (__pthread_current_priority () > ceiling)
329 if (oldprio != -1)
330 __pthread_tpp_change_priority (oldprio, -1);
331 return EINVAL;
334 int retval = __pthread_tpp_change_priority (oldprio, ceiling);
335 if (retval)
336 return retval;
338 ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
339 oldprio = ceiling;
341 oldval
342 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
343 ceilval | 1, ceilval);
345 if (oldval == ceilval)
346 break;
348 while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
350 if (oldval != ceilval)
352 __pthread_tpp_change_priority (oldprio, -1);
353 break;
356 assert (mutex->__data.__owner == 0);
357 /* Record the ownership. */
358 mutex->__data.__owner = id;
359 ++mutex->__data.__nusers;
360 mutex->__data.__count = 1;
362 return 0;
364 break;
366 default:
367 /* Correct code cannot set any other type. */
368 return EINVAL;
371 return EBUSY;
373 strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)