Move some chown / lchown / fchown definitions to syscalls.list (bug 14138).
[glibc.git] / nptl / pthread_mutex_trylock.c
blob0867b065331ffaefce8d6def60101de7175a2b51
1 /* Copyright (C) 2002-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include "pthreadP.h"
23 #include <lowlevellock.h>
25 #ifndef lll_trylock_elision
26 #define lll_trylock_elision(a,t) lll_trylock(a)
27 #endif
29 #ifndef DO_ELISION
30 #define DO_ELISION(m) 0
31 #endif
33 /* We don't force elision in trylock, because this can lead to inconsistent
34 lock state if the lock was actually busy. */
36 int
37 __pthread_mutex_trylock (mutex)
38 pthread_mutex_t *mutex;
40 int oldval;
41 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
43 switch (__builtin_expect (PTHREAD_MUTEX_TYPE_ELISION (mutex),
44 PTHREAD_MUTEX_TIMED_NP))
46 /* Recursive mutex. */
47 case PTHREAD_MUTEX_RECURSIVE_NP|PTHREAD_MUTEX_ELISION_NP:
48 case PTHREAD_MUTEX_RECURSIVE_NP:
49 /* Check whether we already hold the mutex. */
50 if (mutex->__data.__owner == id)
52 /* Just bump the counter. */
53 if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
54 /* Overflow of the counter. */
55 return EAGAIN;
57 ++mutex->__data.__count;
58 return 0;
61 if (lll_trylock (mutex->__data.__lock) == 0)
63 /* Record the ownership. */
64 mutex->__data.__owner = id;
65 mutex->__data.__count = 1;
66 ++mutex->__data.__nusers;
67 return 0;
69 break;
71 case PTHREAD_MUTEX_TIMED_ELISION_NP:
72 elision:
73 if (lll_trylock_elision (mutex->__data.__lock,
74 mutex->__data.__elision) != 0)
75 break;
76 /* Don't record the ownership. */
77 return 0;
79 case PTHREAD_MUTEX_TIMED_NP:
80 if (DO_ELISION (mutex))
81 goto elision;
82 /*FALL THROUGH*/
83 case PTHREAD_MUTEX_ADAPTIVE_NP:
84 case PTHREAD_MUTEX_ERRORCHECK_NP:
85 if (lll_trylock (mutex->__data.__lock) != 0)
86 break;
88 /* Record the ownership. */
89 mutex->__data.__owner = id;
90 ++mutex->__data.__nusers;
92 return 0;
94 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
95 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
96 case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
97 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
98 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
99 &mutex->__data.__list.__next);
101 oldval = mutex->__data.__lock;
104 again:
105 if ((oldval & FUTEX_OWNER_DIED) != 0)
107 /* The previous owner died. Try locking the mutex. */
108 int newval = id | (oldval & FUTEX_WAITERS);
110 newval
111 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
112 newval, oldval);
114 if (newval != oldval)
116 oldval = newval;
117 goto again;
120 /* We got the mutex. */
121 mutex->__data.__count = 1;
122 /* But it is inconsistent unless marked otherwise. */
123 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
125 ENQUEUE_MUTEX (mutex);
126 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
128 /* Note that we deliberately exist here. If we fall
129 through to the end of the function __nusers would be
130 incremented which is not correct because the old
131 owner has to be discounted. */
132 return EOWNERDEAD;
135 /* Check whether we already hold the mutex. */
136 if (__glibc_unlikely ((oldval & FUTEX_TID_MASK) == id))
138 int kind = PTHREAD_MUTEX_TYPE (mutex);
139 if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
141 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
142 NULL);
143 return EDEADLK;
146 if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
148 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
149 NULL);
151 /* Just bump the counter. */
152 if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
153 /* Overflow of the counter. */
154 return EAGAIN;
156 ++mutex->__data.__count;
158 return 0;
162 oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
163 id, 0);
164 if (oldval != 0 && (oldval & FUTEX_OWNER_DIED) == 0)
166 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
168 return EBUSY;
171 if (__builtin_expect (mutex->__data.__owner
172 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
174 /* This mutex is now not recoverable. */
175 mutex->__data.__count = 0;
176 if (oldval == id)
177 lll_unlock (mutex->__data.__lock,
178 PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
179 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
180 return ENOTRECOVERABLE;
183 while ((oldval & FUTEX_OWNER_DIED) != 0);
185 ENQUEUE_MUTEX (mutex);
186 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
188 mutex->__data.__owner = id;
189 ++mutex->__data.__nusers;
190 mutex->__data.__count = 1;
192 return 0;
194 case PTHREAD_MUTEX_PI_RECURSIVE_NP:
195 case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
196 case PTHREAD_MUTEX_PI_NORMAL_NP:
197 case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
198 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
199 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
200 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
201 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
203 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
204 int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
206 if (robust)
207 /* Note: robust PI futexes are signaled by setting bit 0. */
208 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
209 (void *) (((uintptr_t) &mutex->__data.__list.__next)
210 | 1));
212 oldval = mutex->__data.__lock;
214 /* Check whether we already hold the mutex. */
215 if (__glibc_unlikely ((oldval & FUTEX_TID_MASK) == id))
217 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
219 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
220 return EDEADLK;
223 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
225 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
227 /* Just bump the counter. */
228 if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
229 /* Overflow of the counter. */
230 return EAGAIN;
232 ++mutex->__data.__count;
234 return 0;
238 oldval
239 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
240 id, 0);
242 if (oldval != 0)
244 if ((oldval & FUTEX_OWNER_DIED) == 0)
246 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
248 return EBUSY;
251 assert (robust);
253 /* The mutex owner died. The kernel will now take care of
254 everything. */
255 int private = (robust
256 ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
257 : PTHREAD_MUTEX_PSHARED (mutex));
258 INTERNAL_SYSCALL_DECL (__err);
259 int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
260 __lll_private_flag (FUTEX_TRYLOCK_PI,
261 private), 0, 0);
263 if (INTERNAL_SYSCALL_ERROR_P (e, __err)
264 && INTERNAL_SYSCALL_ERRNO (e, __err) == EWOULDBLOCK)
266 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
268 return EBUSY;
271 oldval = mutex->__data.__lock;
274 if (__glibc_unlikely (oldval & FUTEX_OWNER_DIED))
276 atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
278 /* We got the mutex. */
279 mutex->__data.__count = 1;
280 /* But it is inconsistent unless marked otherwise. */
281 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
283 ENQUEUE_MUTEX (mutex);
284 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
286 /* Note that we deliberately exit here. If we fall
287 through to the end of the function __nusers would be
288 incremented which is not correct because the old owner
289 has to be discounted. */
290 return EOWNERDEAD;
293 if (robust
294 && __builtin_expect (mutex->__data.__owner
295 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
297 /* This mutex is now not recoverable. */
298 mutex->__data.__count = 0;
300 INTERNAL_SYSCALL_DECL (__err);
301 INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
302 __lll_private_flag (FUTEX_UNLOCK_PI,
303 PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
304 0, 0);
306 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
307 return ENOTRECOVERABLE;
310 if (robust)
312 ENQUEUE_MUTEX_PI (mutex);
313 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
316 mutex->__data.__owner = id;
317 ++mutex->__data.__nusers;
318 mutex->__data.__count = 1;
320 return 0;
323 case PTHREAD_MUTEX_PP_RECURSIVE_NP:
324 case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
325 case PTHREAD_MUTEX_PP_NORMAL_NP:
326 case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
328 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
330 oldval = mutex->__data.__lock;
332 /* Check whether we already hold the mutex. */
333 if (mutex->__data.__owner == id)
335 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
336 return EDEADLK;
338 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
340 /* Just bump the counter. */
341 if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
342 /* Overflow of the counter. */
343 return EAGAIN;
345 ++mutex->__data.__count;
347 return 0;
351 int oldprio = -1, ceilval;
354 int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
355 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
357 if (__pthread_current_priority () > ceiling)
359 if (oldprio != -1)
360 __pthread_tpp_change_priority (oldprio, -1);
361 return EINVAL;
364 int retval = __pthread_tpp_change_priority (oldprio, ceiling);
365 if (retval)
366 return retval;
368 ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
369 oldprio = ceiling;
371 oldval
372 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
373 ceilval | 1, ceilval);
375 if (oldval == ceilval)
376 break;
378 while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
380 if (oldval != ceilval)
382 __pthread_tpp_change_priority (oldprio, -1);
383 break;
386 assert (mutex->__data.__owner == 0);
387 /* Record the ownership. */
388 mutex->__data.__owner = id;
389 ++mutex->__data.__nusers;
390 mutex->__data.__count = 1;
392 return 0;
394 break;
396 default:
397 /* Correct code cannot set any other type. */
398 return EINVAL;
401 return EBUSY;
404 #ifndef __pthread_mutex_trylock
405 #ifndef pthread_mutex_trylock
406 strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
407 #endif
408 #endif