Fixup usage of MIN_EXP in libm-test.inc
[glibc.git] / nptl / pthread_mutex_lock.c
blobbdfa529f639bda7eb59b620c042545f268f5a056
1 /* Copyright (C) 2002-2016 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 <unistd.h>
23 #include <sys/param.h>
24 #include <not-cancel.h>
25 #include "pthreadP.h"
26 #include <atomic.h>
27 #include <lowlevellock.h>
28 #include <stap-probe.h>
30 #ifndef lll_lock_elision
31 #define lll_lock_elision(lock, try_lock, private) ({ \
32 lll_lock (lock, private); 0; })
33 #endif
35 #ifndef lll_trylock_elision
36 #define lll_trylock_elision(a,t) lll_trylock(a)
37 #endif
39 #ifndef LLL_MUTEX_LOCK
40 # define LLL_MUTEX_LOCK(mutex) \
41 lll_lock ((mutex)->__data.__lock, PTHREAD_MUTEX_PSHARED (mutex))
42 # define LLL_MUTEX_TRYLOCK(mutex) \
43 lll_trylock ((mutex)->__data.__lock)
44 # define LLL_ROBUST_MUTEX_LOCK(mutex, id) \
45 lll_robust_lock ((mutex)->__data.__lock, id, \
46 PTHREAD_ROBUST_MUTEX_PSHARED (mutex))
47 # define LLL_MUTEX_LOCK_ELISION(mutex) \
48 lll_lock_elision ((mutex)->__data.__lock, (mutex)->__data.__elision, \
49 PTHREAD_MUTEX_PSHARED (mutex))
50 # define LLL_MUTEX_TRYLOCK_ELISION(mutex) \
51 lll_trylock_elision((mutex)->__data.__lock, (mutex)->__data.__elision, \
52 PTHREAD_MUTEX_PSHARED (mutex))
53 #endif
55 #ifndef FORCE_ELISION
56 #define FORCE_ELISION(m, s)
57 #endif
59 static int __pthread_mutex_lock_full (pthread_mutex_t *mutex)
60 __attribute_noinline__;
62 int
63 __pthread_mutex_lock (pthread_mutex_t *mutex)
65 assert (sizeof (mutex->__size) >= sizeof (mutex->__data));
67 unsigned int type = PTHREAD_MUTEX_TYPE_ELISION (mutex);
69 LIBC_PROBE (mutex_entry, 1, mutex);
71 if (__builtin_expect (type & ~(PTHREAD_MUTEX_KIND_MASK_NP
72 | PTHREAD_MUTEX_ELISION_FLAGS_NP), 0))
73 return __pthread_mutex_lock_full (mutex);
75 if (__glibc_likely (type == PTHREAD_MUTEX_TIMED_NP))
77 FORCE_ELISION (mutex, goto elision);
78 simple:
79 /* Normal mutex. */
80 LLL_MUTEX_LOCK (mutex);
81 assert (mutex->__data.__owner == 0);
83 #ifdef HAVE_ELISION
84 else if (__glibc_likely (type == PTHREAD_MUTEX_TIMED_ELISION_NP))
86 elision: __attribute__((unused))
87 /* This case can never happen on a system without elision,
88 as the mutex type initialization functions will not
89 allow to set the elision flags. */
90 /* Don't record owner or users for elision case. This is a
91 tail call. */
92 return LLL_MUTEX_LOCK_ELISION (mutex);
94 #endif
95 else if (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex)
96 == PTHREAD_MUTEX_RECURSIVE_NP, 1))
98 /* Recursive mutex. */
99 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
101 /* Check whether we already hold the mutex. */
102 if (mutex->__data.__owner == id)
104 /* Just bump the counter. */
105 if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
106 /* Overflow of the counter. */
107 return EAGAIN;
109 ++mutex->__data.__count;
111 return 0;
114 /* We have to get the mutex. */
115 LLL_MUTEX_LOCK (mutex);
117 assert (mutex->__data.__owner == 0);
118 mutex->__data.__count = 1;
120 else if (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex)
121 == PTHREAD_MUTEX_ADAPTIVE_NP, 1))
123 if (! __is_smp)
124 goto simple;
126 if (LLL_MUTEX_TRYLOCK (mutex) != 0)
128 int cnt = 0;
129 int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
130 mutex->__data.__spins * 2 + 10);
133 if (cnt++ >= max_cnt)
135 LLL_MUTEX_LOCK (mutex);
136 break;
138 atomic_spin_nop ();
140 while (LLL_MUTEX_TRYLOCK (mutex) != 0);
142 mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
144 assert (mutex->__data.__owner == 0);
146 else
148 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
149 assert (PTHREAD_MUTEX_TYPE (mutex) == PTHREAD_MUTEX_ERRORCHECK_NP);
150 /* Check whether we already hold the mutex. */
151 if (__glibc_unlikely (mutex->__data.__owner == id))
152 return EDEADLK;
153 goto simple;
156 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
158 /* Record the ownership. */
159 mutex->__data.__owner = id;
160 #ifndef NO_INCR
161 ++mutex->__data.__nusers;
162 #endif
164 LIBC_PROBE (mutex_acquired, 1, mutex);
166 return 0;
169 static int
170 __pthread_mutex_lock_full (pthread_mutex_t *mutex)
172 int oldval;
173 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
175 switch (PTHREAD_MUTEX_TYPE (mutex))
177 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
178 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
179 case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
180 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
181 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
182 &mutex->__data.__list.__next);
184 oldval = mutex->__data.__lock;
187 again:
188 if ((oldval & FUTEX_OWNER_DIED) != 0)
190 /* The previous owner died. Try locking the mutex. */
191 int newval = id;
192 #ifdef NO_INCR
193 newval |= FUTEX_WAITERS;
194 #else
195 newval |= (oldval & FUTEX_WAITERS);
196 #endif
198 newval
199 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
200 newval, oldval);
202 if (newval != oldval)
204 oldval = newval;
205 goto again;
208 /* We got the mutex. */
209 mutex->__data.__count = 1;
210 /* But it is inconsistent unless marked otherwise. */
211 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
213 ENQUEUE_MUTEX (mutex);
214 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
216 /* Note that we deliberately exit here. If we fall
217 through to the end of the function __nusers would be
218 incremented which is not correct because the old
219 owner has to be discounted. If we are not supposed
220 to increment __nusers we actually have to decrement
221 it here. */
222 #ifdef NO_INCR
223 --mutex->__data.__nusers;
224 #endif
226 return EOWNERDEAD;
229 /* Check whether we already hold the mutex. */
230 if (__glibc_unlikely ((oldval & FUTEX_TID_MASK) == id))
232 int kind = PTHREAD_MUTEX_TYPE (mutex);
233 if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
235 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
236 NULL);
237 return EDEADLK;
240 if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
242 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
243 NULL);
245 /* Just bump the counter. */
246 if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
247 /* Overflow of the counter. */
248 return EAGAIN;
250 ++mutex->__data.__count;
252 return 0;
256 oldval = LLL_ROBUST_MUTEX_LOCK (mutex, id);
258 if (__builtin_expect (mutex->__data.__owner
259 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
261 /* This mutex is now not recoverable. */
262 mutex->__data.__count = 0;
263 lll_unlock (mutex->__data.__lock,
264 PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
265 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
266 return ENOTRECOVERABLE;
269 while ((oldval & FUTEX_OWNER_DIED) != 0);
271 mutex->__data.__count = 1;
272 ENQUEUE_MUTEX (mutex);
273 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
274 break;
276 /* The PI support requires the Linux futex system call. If that's not
277 available, pthread_mutex_init should never have allowed the type to
278 be set. So it will get the default case for an invalid type. */
279 #ifdef __NR_futex
280 case PTHREAD_MUTEX_PI_RECURSIVE_NP:
281 case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
282 case PTHREAD_MUTEX_PI_NORMAL_NP:
283 case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
284 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
285 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
286 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
287 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
289 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
290 int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
292 if (robust)
293 /* Note: robust PI futexes are signaled by setting bit 0. */
294 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
295 (void *) (((uintptr_t) &mutex->__data.__list.__next)
296 | 1));
298 oldval = mutex->__data.__lock;
300 /* Check whether we already hold the mutex. */
301 if (__glibc_unlikely ((oldval & FUTEX_TID_MASK) == id))
303 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
305 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
306 return EDEADLK;
309 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
311 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
313 /* Just bump the counter. */
314 if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
315 /* Overflow of the counter. */
316 return EAGAIN;
318 ++mutex->__data.__count;
320 return 0;
324 int newval = id;
325 # ifdef NO_INCR
326 newval |= FUTEX_WAITERS;
327 # endif
328 oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
329 newval, 0);
331 if (oldval != 0)
333 /* The mutex is locked. The kernel will now take care of
334 everything. */
335 int private = (robust
336 ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
337 : PTHREAD_MUTEX_PSHARED (mutex));
338 INTERNAL_SYSCALL_DECL (__err);
339 int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
340 __lll_private_flag (FUTEX_LOCK_PI,
341 private), 1, 0);
343 if (INTERNAL_SYSCALL_ERROR_P (e, __err)
344 && (INTERNAL_SYSCALL_ERRNO (e, __err) == ESRCH
345 || INTERNAL_SYSCALL_ERRNO (e, __err) == EDEADLK))
347 assert (INTERNAL_SYSCALL_ERRNO (e, __err) != EDEADLK
348 || (kind != PTHREAD_MUTEX_ERRORCHECK_NP
349 && kind != PTHREAD_MUTEX_RECURSIVE_NP));
350 /* ESRCH can happen only for non-robust PI mutexes where
351 the owner of the lock died. */
352 assert (INTERNAL_SYSCALL_ERRNO (e, __err) != ESRCH || !robust);
354 /* Delay the thread indefinitely. */
355 while (1)
356 pause_not_cancel ();
359 oldval = mutex->__data.__lock;
361 assert (robust || (oldval & FUTEX_OWNER_DIED) == 0);
364 if (__glibc_unlikely (oldval & FUTEX_OWNER_DIED))
366 atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
368 /* We got the mutex. */
369 mutex->__data.__count = 1;
370 /* But it is inconsistent unless marked otherwise. */
371 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
373 ENQUEUE_MUTEX_PI (mutex);
374 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
376 /* Note that we deliberately exit here. If we fall
377 through to the end of the function __nusers would be
378 incremented which is not correct because the old owner
379 has to be discounted. If we are not supposed to
380 increment __nusers we actually have to decrement it here. */
381 # ifdef NO_INCR
382 --mutex->__data.__nusers;
383 # endif
385 return EOWNERDEAD;
388 if (robust
389 && __builtin_expect (mutex->__data.__owner
390 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
392 /* This mutex is now not recoverable. */
393 mutex->__data.__count = 0;
395 INTERNAL_SYSCALL_DECL (__err);
396 INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
397 __lll_private_flag (FUTEX_UNLOCK_PI,
398 PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
399 0, 0);
401 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
402 return ENOTRECOVERABLE;
405 mutex->__data.__count = 1;
406 if (robust)
408 ENQUEUE_MUTEX_PI (mutex);
409 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
412 break;
413 #endif /* __NR_futex. */
415 case PTHREAD_MUTEX_PP_RECURSIVE_NP:
416 case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
417 case PTHREAD_MUTEX_PP_NORMAL_NP:
418 case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
420 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
422 oldval = mutex->__data.__lock;
424 /* Check whether we already hold the mutex. */
425 if (mutex->__data.__owner == id)
427 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
428 return EDEADLK;
430 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
432 /* Just bump the counter. */
433 if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
434 /* Overflow of the counter. */
435 return EAGAIN;
437 ++mutex->__data.__count;
439 return 0;
443 int oldprio = -1, ceilval;
446 int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
447 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
449 if (__pthread_current_priority () > ceiling)
451 if (oldprio != -1)
452 __pthread_tpp_change_priority (oldprio, -1);
453 return EINVAL;
456 int retval = __pthread_tpp_change_priority (oldprio, ceiling);
457 if (retval)
458 return retval;
460 ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
461 oldprio = ceiling;
463 oldval
464 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
465 #ifdef NO_INCR
466 ceilval | 2,
467 #else
468 ceilval | 1,
469 #endif
470 ceilval);
472 if (oldval == ceilval)
473 break;
477 oldval
478 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
479 ceilval | 2,
480 ceilval | 1);
482 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
483 break;
485 if (oldval != ceilval)
486 lll_futex_wait (&mutex->__data.__lock, ceilval | 2,
487 PTHREAD_MUTEX_PSHARED (mutex));
489 while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
490 ceilval | 2, ceilval)
491 != ceilval);
493 while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
495 assert (mutex->__data.__owner == 0);
496 mutex->__data.__count = 1;
498 break;
500 default:
501 /* Correct code cannot set any other type. */
502 return EINVAL;
505 /* Record the ownership. */
506 mutex->__data.__owner = id;
507 #ifndef NO_INCR
508 ++mutex->__data.__nusers;
509 #endif
511 LIBC_PROBE (mutex_acquired, 1, mutex);
513 return 0;
515 #ifndef __pthread_mutex_lock
516 strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
517 hidden_def (__pthread_mutex_lock)
518 #endif
521 #ifdef NO_INCR
522 void
523 internal_function
524 __pthread_mutex_cond_lock_adjust (pthread_mutex_t *mutex)
526 assert ((mutex->__data.__kind & PTHREAD_MUTEX_PRIO_INHERIT_NP) != 0);
527 assert ((mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP) == 0);
528 assert ((mutex->__data.__kind & PTHREAD_MUTEX_PSHARED_BIT) == 0);
530 /* Record the ownership. */
531 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
532 mutex->__data.__owner = id;
534 if (mutex->__data.__kind == PTHREAD_MUTEX_PI_RECURSIVE_NP)
535 ++mutex->__data.__count;
537 #endif