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/>. */
22 #include <sys/param.h>
26 #include <lowlevellock.h>
27 #include <not-cancel.h>
29 #include <stap-probe.h>
31 #ifndef lll_timedlock_elision
32 #define lll_timedlock_elision(a,dummy,b,c) lll_timedlock(a, b, c)
35 #ifndef lll_trylock_elision
36 #define lll_trylock_elision(a,t) lll_trylock(a)
40 #define FORCE_ELISION(m, s)
44 pthread_mutex_timedlock (pthread_mutex_t
*mutex
,
45 const struct timespec
*abstime
)
48 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
51 LIBC_PROBE (mutex_timedlock_entry
, 2, mutex
, abstime
);
53 /* We must not check ABSTIME here. If the thread does not block
54 abstime must not be checked for a valid value. */
56 switch (__builtin_expect (PTHREAD_MUTEX_TYPE_ELISION (mutex
),
57 PTHREAD_MUTEX_TIMED_NP
))
59 /* Recursive mutex. */
60 case PTHREAD_MUTEX_RECURSIVE_NP
|PTHREAD_MUTEX_ELISION_NP
:
61 case PTHREAD_MUTEX_RECURSIVE_NP
:
62 /* Check whether we already hold the mutex. */
63 if (mutex
->__data
.__owner
== id
)
65 /* Just bump the counter. */
66 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
67 /* Overflow of the counter. */
70 ++mutex
->__data
.__count
;
75 /* We have to get the mutex. */
76 result
= lll_timedlock (mutex
->__data
.__lock
, abstime
,
77 PTHREAD_MUTEX_PSHARED (mutex
));
82 /* Only locked once so far. */
83 mutex
->__data
.__count
= 1;
86 /* Error checking mutex. */
87 case PTHREAD_MUTEX_ERRORCHECK_NP
:
88 /* Check whether we already hold the mutex. */
89 if (__glibc_unlikely (mutex
->__data
.__owner
== id
))
92 /* Don't do lock elision on an error checking mutex. */
95 case PTHREAD_MUTEX_TIMED_NP
:
96 FORCE_ELISION (mutex
, goto elision
);
99 result
= lll_timedlock (mutex
->__data
.__lock
, abstime
,
100 PTHREAD_MUTEX_PSHARED (mutex
));
103 case PTHREAD_MUTEX_TIMED_ELISION_NP
:
104 elision
: __attribute__((unused
))
105 /* Don't record ownership */
106 return lll_timedlock_elision (mutex
->__data
.__lock
,
107 mutex
->__data
.__spins
,
109 PTHREAD_MUTEX_PSHARED (mutex
));
112 case PTHREAD_MUTEX_ADAPTIVE_NP
:
116 if (lll_trylock (mutex
->__data
.__lock
) != 0)
119 int max_cnt
= MIN (MAX_ADAPTIVE_COUNT
,
120 mutex
->__data
.__spins
* 2 + 10);
123 if (cnt
++ >= max_cnt
)
125 result
= lll_timedlock (mutex
->__data
.__lock
, abstime
,
126 PTHREAD_MUTEX_PSHARED (mutex
));
131 while (lll_trylock (mutex
->__data
.__lock
) != 0);
133 mutex
->__data
.__spins
+= (cnt
- mutex
->__data
.__spins
) / 8;
137 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
:
138 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
:
139 case PTHREAD_MUTEX_ROBUST_NORMAL_NP
:
140 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP
:
141 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
142 &mutex
->__data
.__list
.__next
);
144 oldval
= mutex
->__data
.__lock
;
148 if ((oldval
& FUTEX_OWNER_DIED
) != 0)
150 /* The previous owner died. Try locking the mutex. */
151 int newval
= id
| (oldval
& FUTEX_WAITERS
);
154 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
156 if (newval
!= oldval
)
162 /* We got the mutex. */
163 mutex
->__data
.__count
= 1;
164 /* But it is inconsistent unless marked otherwise. */
165 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
167 ENQUEUE_MUTEX (mutex
);
168 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
170 /* Note that we deliberately exit here. If we fall
171 through to the end of the function __nusers would be
172 incremented which is not correct because the old
173 owner has to be discounted. */
177 /* Check whether we already hold the mutex. */
178 if (__glibc_unlikely ((oldval
& FUTEX_TID_MASK
) == id
))
180 int kind
= PTHREAD_MUTEX_TYPE (mutex
);
181 if (kind
== PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
)
183 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
188 if (kind
== PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
)
190 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
193 /* Just bump the counter. */
194 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
195 /* Overflow of the counter. */
198 ++mutex
->__data
.__count
;
200 LIBC_PROBE (mutex_timedlock_acquired
, 1, mutex
);
206 result
= lll_robust_timedlock (mutex
->__data
.__lock
, abstime
, id
,
207 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
));
209 if (__builtin_expect (mutex
->__data
.__owner
210 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
212 /* This mutex is now not recoverable. */
213 mutex
->__data
.__count
= 0;
214 lll_unlock (mutex
->__data
.__lock
,
215 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
));
216 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
217 return ENOTRECOVERABLE
;
220 if (result
== ETIMEDOUT
|| result
== EINVAL
)
225 while ((oldval
& FUTEX_OWNER_DIED
) != 0);
227 mutex
->__data
.__count
= 1;
228 ENQUEUE_MUTEX (mutex
);
229 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
232 /* The PI support requires the Linux futex system call. If that's not
233 available, pthread_mutex_init should never have allowed the type to
234 be set. So it will get the default case for an invalid type. */
236 case PTHREAD_MUTEX_PI_RECURSIVE_NP
:
237 case PTHREAD_MUTEX_PI_ERRORCHECK_NP
:
238 case PTHREAD_MUTEX_PI_NORMAL_NP
:
239 case PTHREAD_MUTEX_PI_ADAPTIVE_NP
:
240 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP
:
241 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP
:
242 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP
:
243 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP
:
245 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
246 int robust
= mutex
->__data
.__kind
& PTHREAD_MUTEX_ROBUST_NORMAL_NP
;
249 /* Note: robust PI futexes are signaled by setting bit 0. */
250 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
251 (void *) (((uintptr_t) &mutex
->__data
.__list
.__next
)
254 oldval
= mutex
->__data
.__lock
;
256 /* Check whether we already hold the mutex. */
257 if (__glibc_unlikely ((oldval
& FUTEX_TID_MASK
) == id
))
259 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
261 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
265 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
267 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
269 /* Just bump the counter. */
270 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
271 /* Overflow of the counter. */
274 ++mutex
->__data
.__count
;
276 LIBC_PROBE (mutex_timedlock_acquired
, 1, mutex
);
282 oldval
= atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
287 /* The mutex is locked. The kernel will now take care of
288 everything. The timeout value must be a relative value.
290 int private = (robust
291 ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex
)
292 : PTHREAD_MUTEX_PSHARED (mutex
));
293 INTERNAL_SYSCALL_DECL (__err
);
295 int e
= INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
296 __lll_private_flag (FUTEX_LOCK_PI
,
299 if (INTERNAL_SYSCALL_ERROR_P (e
, __err
))
301 if (INTERNAL_SYSCALL_ERRNO (e
, __err
) == ETIMEDOUT
)
304 if (INTERNAL_SYSCALL_ERRNO (e
, __err
) == ESRCH
305 || INTERNAL_SYSCALL_ERRNO (e
, __err
) == EDEADLK
)
307 assert (INTERNAL_SYSCALL_ERRNO (e
, __err
) != EDEADLK
308 || (kind
!= PTHREAD_MUTEX_ERRORCHECK_NP
309 && kind
!= PTHREAD_MUTEX_RECURSIVE_NP
));
310 /* ESRCH can happen only for non-robust PI mutexes where
311 the owner of the lock died. */
312 assert (INTERNAL_SYSCALL_ERRNO (e
, __err
) != ESRCH
315 /* Delay the thread until the timeout is reached.
316 Then return ETIMEDOUT. */
317 struct timespec reltime
;
320 INTERNAL_SYSCALL (clock_gettime
, __err
, 2, CLOCK_REALTIME
,
322 reltime
.tv_sec
= abstime
->tv_sec
- now
.tv_sec
;
323 reltime
.tv_nsec
= abstime
->tv_nsec
- now
.tv_nsec
;
324 if (reltime
.tv_nsec
< 0)
326 reltime
.tv_nsec
+= 1000000000;
329 if (reltime
.tv_sec
>= 0)
330 while (nanosleep_not_cancel (&reltime
, &reltime
) != 0)
336 return INTERNAL_SYSCALL_ERRNO (e
, __err
);
339 oldval
= mutex
->__data
.__lock
;
341 assert (robust
|| (oldval
& FUTEX_OWNER_DIED
) == 0);
344 if (__glibc_unlikely (oldval
& FUTEX_OWNER_DIED
))
346 atomic_and (&mutex
->__data
.__lock
, ~FUTEX_OWNER_DIED
);
348 /* We got the mutex. */
349 mutex
->__data
.__count
= 1;
350 /* But it is inconsistent unless marked otherwise. */
351 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
353 ENQUEUE_MUTEX_PI (mutex
);
354 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
356 /* Note that we deliberately exit here. If we fall
357 through to the end of the function __nusers would be
358 incremented which is not correct because the old owner
359 has to be discounted. */
364 && __builtin_expect (mutex
->__data
.__owner
365 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
367 /* This mutex is now not recoverable. */
368 mutex
->__data
.__count
= 0;
370 INTERNAL_SYSCALL_DECL (__err
);
371 INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
372 __lll_private_flag (FUTEX_UNLOCK_PI
,
373 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
)),
376 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
377 return ENOTRECOVERABLE
;
380 mutex
->__data
.__count
= 1;
383 ENQUEUE_MUTEX_PI (mutex
);
384 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
388 #endif /* __NR_futex. */
390 case PTHREAD_MUTEX_PP_RECURSIVE_NP
:
391 case PTHREAD_MUTEX_PP_ERRORCHECK_NP
:
392 case PTHREAD_MUTEX_PP_NORMAL_NP
:
393 case PTHREAD_MUTEX_PP_ADAPTIVE_NP
:
395 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
397 oldval
= mutex
->__data
.__lock
;
399 /* Check whether we already hold the mutex. */
400 if (mutex
->__data
.__owner
== id
)
402 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
405 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
407 /* Just bump the counter. */
408 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
409 /* Overflow of the counter. */
412 ++mutex
->__data
.__count
;
414 LIBC_PROBE (mutex_timedlock_acquired
, 1, mutex
);
420 int oldprio
= -1, ceilval
;
423 int ceiling
= (oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
)
424 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
426 if (__pthread_current_priority () > ceiling
)
431 __pthread_tpp_change_priority (oldprio
, -1);
435 result
= __pthread_tpp_change_priority (oldprio
, ceiling
);
439 ceilval
= ceiling
<< PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
443 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
444 ceilval
| 1, ceilval
);
446 if (oldval
== ceilval
)
452 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
456 if ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
)
459 if (oldval
!= ceilval
)
461 /* Reject invalid timeouts. */
462 if (abstime
->tv_nsec
< 0 || abstime
->tv_nsec
>= 1000000000)
471 /* Get the current time. */
472 (void) __gettimeofday (&tv
, NULL
);
474 /* Compute relative timeout. */
475 rt
.tv_sec
= abstime
->tv_sec
- tv
.tv_sec
;
476 rt
.tv_nsec
= abstime
->tv_nsec
- tv
.tv_usec
* 1000;
479 rt
.tv_nsec
+= 1000000000;
483 /* Already timed out? */
490 lll_futex_timed_wait (&mutex
->__data
.__lock
,
492 PTHREAD_MUTEX_PSHARED (mutex
));
495 while (atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
496 ceilval
| 2, ceilval
)
499 while ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
);
501 assert (mutex
->__data
.__owner
== 0);
502 mutex
->__data
.__count
= 1;
507 /* Correct code cannot set any other type. */
513 /* Record the ownership. */
514 mutex
->__data
.__owner
= id
;
515 ++mutex
->__data
.__nusers
;
517 LIBC_PROBE (mutex_timedlock_acquired
, 1, mutex
);