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/>. */
23 #include <sys/param.h>
24 #include <not-cancel.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; })
35 #ifndef lll_trylock_elision
36 #define lll_trylock_elision(a,t) lll_trylock(a)
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))
56 #define FORCE_ELISION(m, s)
59 static int __pthread_mutex_lock_full (pthread_mutex_t
*mutex
)
60 __attribute_noinline__
;
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
);
80 LLL_MUTEX_LOCK (mutex
);
81 assert (mutex
->__data
.__owner
== 0);
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
92 return LLL_MUTEX_LOCK_ELISION (mutex
);
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. */
109 ++mutex
->__data
.__count
;
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))
126 if (LLL_MUTEX_TRYLOCK (mutex
) != 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
);
140 while (LLL_MUTEX_TRYLOCK (mutex
) != 0);
142 mutex
->__data
.__spins
+= (cnt
- mutex
->__data
.__spins
) / 8;
144 assert (mutex
->__data
.__owner
== 0);
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
))
156 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
158 /* Record the ownership. */
159 mutex
->__data
.__owner
= id
;
161 ++mutex
->__data
.__nusers
;
164 LIBC_PROBE (mutex_acquired
, 1, mutex
);
170 __pthread_mutex_lock_full (pthread_mutex_t
*mutex
)
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
;
188 if ((oldval
& FUTEX_OWNER_DIED
) != 0)
190 /* The previous owner died. Try locking the mutex. */
193 newval
|= FUTEX_WAITERS
;
195 newval
|= (oldval
& FUTEX_WAITERS
);
199 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
202 if (newval
!= oldval
)
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
223 --mutex
->__data
.__nusers
;
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
,
240 if (kind
== PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
)
242 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
245 /* Just bump the counter. */
246 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
247 /* Overflow of the counter. */
250 ++mutex
->__data
.__count
;
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
);
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. */
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
;
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
)
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
);
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. */
318 ++mutex
->__data
.__count
;
326 newval
|= FUTEX_WAITERS
;
328 oldval
= atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
333 /* The mutex is locked. The kernel will now take care of
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
,
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. */
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. */
382 --mutex
->__data
.__nusers
;
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
)),
401 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
402 return ENOTRECOVERABLE
;
405 mutex
->__data
.__count
= 1;
408 ENQUEUE_MUTEX_PI (mutex
);
409 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
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
)
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. */
437 ++mutex
->__data
.__count
;
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
)
452 __pthread_tpp_change_priority (oldprio
, -1);
456 int retval
= __pthread_tpp_change_priority (oldprio
, ceiling
);
460 ceilval
= ceiling
<< PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
464 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
472 if (oldval
== ceilval
)
478 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
482 if ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
)
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
)
493 while ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
);
495 assert (mutex
->__data
.__owner
== 0);
496 mutex
->__data
.__count
= 1;
501 /* Correct code cannot set any other type. */
505 /* Record the ownership. */
506 mutex
->__data
.__owner
= id
;
508 ++mutex
->__data
.__nusers
;
511 LIBC_PROBE (mutex_acquired
, 1, mutex
);
515 #ifndef __pthread_mutex_lock
516 strong_alias (__pthread_mutex_lock
, pthread_mutex_lock
)
517 hidden_def (__pthread_mutex_lock
)
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
;