[powerpc] No need to enter "Ignore Exceptions Mode"
[glibc.git] / nptl / pthread_mutex_trylock.c
blob87e87c013a6d8151d8f8a9508174ffcbdffe81ae
1 /* Copyright (C) 2002-2019 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 <https://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 FORCE_ELISION
30 #define FORCE_ELISION(m, s)
31 #endif
33 int
34 __pthread_mutex_trylock (pthread_mutex_t *mutex)
36 int oldval;
37 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
39 /* See concurrency notes regarding mutex type which is loaded from __kind
40 in struct __pthread_mutex_s in sysdeps/nptl/bits/thread-shared-types.h. */
41 switch (__builtin_expect (PTHREAD_MUTEX_TYPE_ELISION (mutex),
42 PTHREAD_MUTEX_TIMED_NP))
44 /* Recursive mutex. */
45 case PTHREAD_MUTEX_RECURSIVE_NP|PTHREAD_MUTEX_ELISION_NP:
46 case PTHREAD_MUTEX_RECURSIVE_NP:
47 /* Check whether we already hold the mutex. */
48 if (mutex->__data.__owner == id)
50 /* Just bump the counter. */
51 if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
52 /* Overflow of the counter. */
53 return EAGAIN;
55 ++mutex->__data.__count;
56 return 0;
59 if (lll_trylock (mutex->__data.__lock) == 0)
61 /* Record the ownership. */
62 mutex->__data.__owner = id;
63 mutex->__data.__count = 1;
64 ++mutex->__data.__nusers;
65 return 0;
67 break;
69 case PTHREAD_MUTEX_TIMED_ELISION_NP:
70 elision: __attribute__((unused))
71 if (lll_trylock_elision (mutex->__data.__lock,
72 mutex->__data.__elision) != 0)
73 break;
74 /* Don't record the ownership. */
75 return 0;
77 case PTHREAD_MUTEX_TIMED_NP:
78 FORCE_ELISION (mutex, goto elision);
79 /*FALL THROUGH*/
80 case PTHREAD_MUTEX_ADAPTIVE_NP:
81 case PTHREAD_MUTEX_ERRORCHECK_NP:
82 if (lll_trylock (mutex->__data.__lock) != 0)
83 break;
85 /* Record the ownership. */
86 mutex->__data.__owner = id;
87 ++mutex->__data.__nusers;
89 return 0;
91 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
92 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
93 case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
94 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
95 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
96 &mutex->__data.__list.__next);
97 /* We need to set op_pending before starting the operation. Also
98 see comments at ENQUEUE_MUTEX. */
99 __asm ("" ::: "memory");
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 /* We must not enqueue the mutex before we have acquired it.
126 Also see comments at ENQUEUE_MUTEX. */
127 __asm ("" ::: "memory");
128 ENQUEUE_MUTEX (mutex);
129 /* We need to clear op_pending after we enqueue the mutex. */
130 __asm ("" ::: "memory");
131 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
133 /* Note that we deliberately exit here. If we fall
134 through to the end of the function __nusers would be
135 incremented which is not correct because the old
136 owner has to be discounted. */
137 return EOWNERDEAD;
140 /* Check whether we already hold the mutex. */
141 if (__glibc_unlikely ((oldval & FUTEX_TID_MASK) == id))
143 int kind = PTHREAD_MUTEX_TYPE (mutex);
144 if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
146 /* We do not need to ensure ordering wrt another memory
147 access. Also see comments at ENQUEUE_MUTEX. */
148 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
149 NULL);
150 return EDEADLK;
153 if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
155 /* We do not need to ensure ordering wrt another memory
156 access. */
157 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
158 NULL);
160 /* Just bump the counter. */
161 if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
162 /* Overflow of the counter. */
163 return EAGAIN;
165 ++mutex->__data.__count;
167 return 0;
171 oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
172 id, 0);
173 if (oldval != 0 && (oldval & FUTEX_OWNER_DIED) == 0)
175 /* We haven't acquired the lock as it is already acquired by
176 another owner. We do not need to ensure ordering wrt another
177 memory access. */
178 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
180 return EBUSY;
183 if (__builtin_expect (mutex->__data.__owner
184 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
186 /* This mutex is now not recoverable. */
187 mutex->__data.__count = 0;
188 if (oldval == id)
189 lll_unlock (mutex->__data.__lock,
190 PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
191 /* FIXME This violates the mutex destruction requirements. See
192 __pthread_mutex_unlock_full. */
193 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
194 return ENOTRECOVERABLE;
197 while ((oldval & FUTEX_OWNER_DIED) != 0);
199 /* We must not enqueue the mutex before we have acquired it.
200 Also see comments at ENQUEUE_MUTEX. */
201 __asm ("" ::: "memory");
202 ENQUEUE_MUTEX (mutex);
203 /* We need to clear op_pending after we enqueue the mutex. */
204 __asm ("" ::: "memory");
205 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
207 mutex->__data.__owner = id;
208 ++mutex->__data.__nusers;
209 mutex->__data.__count = 1;
211 return 0;
213 /* The PI support requires the Linux futex system call. If that's not
214 available, pthread_mutex_init should never have allowed the type to
215 be set. So it will get the default case for an invalid type. */
216 #ifdef __NR_futex
217 case PTHREAD_MUTEX_PI_RECURSIVE_NP:
218 case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
219 case PTHREAD_MUTEX_PI_NORMAL_NP:
220 case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
221 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
222 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
223 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
224 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
226 int kind, robust;
228 /* See concurrency notes regarding __kind in struct __pthread_mutex_s
229 in sysdeps/nptl/bits/thread-shared-types.h. */
230 int mutex_kind = atomic_load_relaxed (&(mutex->__data.__kind));
231 kind = mutex_kind & PTHREAD_MUTEX_KIND_MASK_NP;
232 robust = mutex_kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
235 if (robust)
237 /* Note: robust PI futexes are signaled by setting bit 0. */
238 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
239 (void *) (((uintptr_t) &mutex->__data.__list.__next)
240 | 1));
241 /* We need to set op_pending before starting the operation. Also
242 see comments at ENQUEUE_MUTEX. */
243 __asm ("" ::: "memory");
246 oldval = mutex->__data.__lock;
248 /* Check whether we already hold the mutex. */
249 if (__glibc_unlikely ((oldval & FUTEX_TID_MASK) == id))
251 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
253 /* We do not need to ensure ordering wrt another memory
254 access. */
255 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
256 return EDEADLK;
259 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
261 /* We do not need to ensure ordering wrt another memory
262 access. */
263 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
265 /* Just bump the counter. */
266 if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
267 /* Overflow of the counter. */
268 return EAGAIN;
270 ++mutex->__data.__count;
272 return 0;
276 oldval
277 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
278 id, 0);
280 if (oldval != 0)
282 if ((oldval & FUTEX_OWNER_DIED) == 0)
284 /* We haven't acquired the lock as it is already acquired by
285 another owner. We do not need to ensure ordering wrt another
286 memory access. */
287 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
289 return EBUSY;
292 assert (robust);
294 /* The mutex owner died. The kernel will now take care of
295 everything. */
296 int private = (robust
297 ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
298 : PTHREAD_MUTEX_PSHARED (mutex));
299 INTERNAL_SYSCALL_DECL (__err);
300 int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
301 __lll_private_flag (FUTEX_TRYLOCK_PI,
302 private), 0, 0);
304 if (INTERNAL_SYSCALL_ERROR_P (e, __err)
305 && INTERNAL_SYSCALL_ERRNO (e, __err) == EWOULDBLOCK)
307 /* The kernel has not yet finished the mutex owner death.
308 We do not need to ensure ordering wrt another memory
309 access. */
310 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
312 return EBUSY;
315 oldval = mutex->__data.__lock;
318 if (__glibc_unlikely (oldval & FUTEX_OWNER_DIED))
320 atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
322 /* We got the mutex. */
323 mutex->__data.__count = 1;
324 /* But it is inconsistent unless marked otherwise. */
325 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
327 /* We must not enqueue the mutex before we have acquired it.
328 Also see comments at ENQUEUE_MUTEX. */
329 __asm ("" ::: "memory");
330 ENQUEUE_MUTEX (mutex);
331 /* We need to clear op_pending after we enqueue the mutex. */
332 __asm ("" ::: "memory");
333 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
335 /* Note that we deliberately exit here. If we fall
336 through to the end of the function __nusers would be
337 incremented which is not correct because the old owner
338 has to be discounted. */
339 return EOWNERDEAD;
342 if (robust
343 && __builtin_expect (mutex->__data.__owner
344 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
346 /* This mutex is now not recoverable. */
347 mutex->__data.__count = 0;
349 INTERNAL_SYSCALL_DECL (__err);
350 INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
351 __lll_private_flag (FUTEX_UNLOCK_PI,
352 PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
353 0, 0);
355 /* To the kernel, this will be visible after the kernel has
356 acquired the mutex in the syscall. */
357 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
358 return ENOTRECOVERABLE;
361 if (robust)
363 /* We must not enqueue the mutex before we have acquired it.
364 Also see comments at ENQUEUE_MUTEX. */
365 __asm ("" ::: "memory");
366 ENQUEUE_MUTEX_PI (mutex);
367 /* We need to clear op_pending after we enqueue the mutex. */
368 __asm ("" ::: "memory");
369 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
372 mutex->__data.__owner = id;
373 ++mutex->__data.__nusers;
374 mutex->__data.__count = 1;
376 return 0;
378 #endif /* __NR_futex. */
380 case PTHREAD_MUTEX_PP_RECURSIVE_NP:
381 case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
382 case PTHREAD_MUTEX_PP_NORMAL_NP:
383 case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
385 /* See concurrency notes regarding __kind in struct __pthread_mutex_s
386 in sysdeps/nptl/bits/thread-shared-types.h. */
387 int kind = atomic_load_relaxed (&(mutex->__data.__kind))
388 & PTHREAD_MUTEX_KIND_MASK_NP;
390 oldval = mutex->__data.__lock;
392 /* Check whether we already hold the mutex. */
393 if (mutex->__data.__owner == id)
395 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
396 return EDEADLK;
398 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
400 /* Just bump the counter. */
401 if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
402 /* Overflow of the counter. */
403 return EAGAIN;
405 ++mutex->__data.__count;
407 return 0;
411 int oldprio = -1, ceilval;
414 int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
415 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
417 if (__pthread_current_priority () > ceiling)
419 if (oldprio != -1)
420 __pthread_tpp_change_priority (oldprio, -1);
421 return EINVAL;
424 int retval = __pthread_tpp_change_priority (oldprio, ceiling);
425 if (retval)
426 return retval;
428 ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
429 oldprio = ceiling;
431 oldval
432 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
433 ceilval | 1, ceilval);
435 if (oldval == ceilval)
436 break;
438 while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
440 if (oldval != ceilval)
442 __pthread_tpp_change_priority (oldprio, -1);
443 break;
446 assert (mutex->__data.__owner == 0);
447 /* Record the ownership. */
448 mutex->__data.__owner = id;
449 ++mutex->__data.__nusers;
450 mutex->__data.__count = 1;
452 return 0;
454 break;
456 default:
457 /* Correct code cannot set any other type. */
458 return EINVAL;
461 return EBUSY;
464 #ifndef __pthread_mutex_trylock
465 #ifndef pthread_mutex_trylock
466 weak_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
467 hidden_def (__pthread_mutex_trylock)
468 #endif
469 #endif