1 /* Copyright (C) 2002-2007, 2008 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include <lowlevellock.h>
25 #include <not-cancel.h>
27 /* We need to build this function with optimization to avoid
28 * lll_timedlock erroring out with
29 * error: can't find a register in class ‘GENERAL_REGS’ while reloading ‘asm’
32 attribute_optimize("Os")
33 pthread_mutex_timedlock (
34 pthread_mutex_t *mutex,
35 const struct timespec *abstime)
38 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
41 /* We must not check ABSTIME here. If the thread does not block
42 abstime must not be checked for a valid value. */
44 switch (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex),
45 PTHREAD_MUTEX_TIMED_NP))
47 /* Recursive mutex. */
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 (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
54 /* Overflow of the counter. */
57 ++mutex->__data.__count;
62 /* We have to get the mutex. */
63 result = lll_timedlock (mutex->__data.__lock, abstime,
64 PTHREAD_MUTEX_PSHARED (mutex));
69 /* Only locked once so far. */
70 mutex->__data.__count = 1;
73 /* Error checking mutex. */
74 case PTHREAD_MUTEX_ERRORCHECK_NP:
75 /* Check whether we already hold the mutex. */
76 if (__builtin_expect (mutex->__data.__owner == id, 0))
81 case PTHREAD_MUTEX_TIMED_NP:
84 result = lll_timedlock (mutex->__data.__lock, abstime,
85 PTHREAD_MUTEX_PSHARED (mutex));
88 case PTHREAD_MUTEX_ADAPTIVE_NP:
92 if (lll_trylock (mutex->__data.__lock) != 0)
95 int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
96 mutex->__data.__spins * 2 + 10);
101 result = lll_timedlock (mutex->__data.__lock, abstime,
102 PTHREAD_MUTEX_PSHARED (mutex));
110 while (lll_trylock (mutex->__data.__lock) != 0);
112 mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
116 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
117 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
118 case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
119 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
120 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
121 &mutex->__data.__list.__next);
123 oldval = mutex->__data.__lock;
127 if ((oldval & FUTEX_OWNER_DIED) != 0)
129 /* The previous owner died. Try locking the mutex. */
130 int newval = id | (oldval & FUTEX_WAITERS);
133 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
135 if (newval != oldval)
141 /* We got the mutex. */
142 mutex->__data.__count = 1;
143 /* But it is inconsistent unless marked otherwise. */
144 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
146 ENQUEUE_MUTEX (mutex);
147 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
149 /* Note that we deliberately exit here. If we fall
150 through to the end of the function __nusers would be
151 incremented which is not correct because the old
152 owner has to be discounted. */
156 /* Check whether we already hold the mutex. */
157 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
159 int kind = PTHREAD_MUTEX_TYPE (mutex);
160 if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
162 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
167 if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
169 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
172 /* Just bump the counter. */
173 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
174 /* Overflow of the counter. */
177 ++mutex->__data.__count;
183 result = lll_robust_timedlock (mutex->__data.__lock, abstime, id,
184 PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
186 if (__builtin_expect (mutex->__data.__owner
187 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
189 /* This mutex is now not recoverable. */
190 mutex->__data.__count = 0;
191 lll_unlock (mutex->__data.__lock,
192 PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
193 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
194 return ENOTRECOVERABLE;
197 if (result == ETIMEDOUT || result == EINVAL)
202 while ((oldval & FUTEX_OWNER_DIED) != 0);
204 mutex->__data.__count = 1;
205 ENQUEUE_MUTEX (mutex);
206 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
209 case PTHREAD_MUTEX_PI_RECURSIVE_NP:
210 case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
211 case PTHREAD_MUTEX_PI_NORMAL_NP:
212 case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
213 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
214 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
215 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
216 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
218 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
219 int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
222 /* Note: robust PI futexes are signaled by setting bit 0. */
223 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
224 (void *) (((uintptr_t) &mutex->__data.__list.__next)
227 oldval = mutex->__data.__lock;
229 /* Check whether we already hold the mutex. */
230 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
232 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
234 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
238 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
240 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
242 /* Just bump the counter. */
243 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
244 /* Overflow of the counter. */
247 ++mutex->__data.__count;
253 oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
258 /* The mutex is locked. The kernel will now take care of
259 everything. The timeout value must be a relative value.
261 int private = (robust
262 ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
263 : PTHREAD_MUTEX_PSHARED (mutex));
264 INTERNAL_SYSCALL_DECL (__err);
266 int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
267 __lll_private_flag (FUTEX_LOCK_PI,
270 if (INTERNAL_SYSCALL_ERROR_P (e, __err))
272 if (INTERNAL_SYSCALL_ERRNO (e, __err) == ETIMEDOUT)
275 if (INTERNAL_SYSCALL_ERRNO (e, __err) == ESRCH
276 || INTERNAL_SYSCALL_ERRNO (e, __err) == EDEADLK)
278 assert (INTERNAL_SYSCALL_ERRNO (e, __err) != EDEADLK
279 || (kind != PTHREAD_MUTEX_ERRORCHECK_NP
280 && kind != PTHREAD_MUTEX_RECURSIVE_NP));
281 /* ESRCH can happen only for non-robust PI mutexes where
282 the owner of the lock died. */
283 assert (INTERNAL_SYSCALL_ERRNO (e, __err) != ESRCH
286 /* Delay the thread until the timeout is reached.
287 Then return ETIMEDOUT. */
288 struct timespec reltime;
291 INTERNAL_SYSCALL (clock_gettime, __err, 2, CLOCK_REALTIME,
293 reltime.tv_sec = abstime->tv_sec - now.tv_sec;
294 reltime.tv_nsec = abstime->tv_nsec - now.tv_nsec;
295 if (reltime.tv_nsec < 0)
297 reltime.tv_nsec += 1000000000;
300 if (reltime.tv_sec >= 0)
301 while (nanosleep_not_cancel (&reltime, &reltime) != 0)
307 return INTERNAL_SYSCALL_ERRNO (e, __err);
310 oldval = mutex->__data.__lock;
312 assert (robust || (oldval & FUTEX_OWNER_DIED) == 0);
315 if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
317 atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
319 /* We got the mutex. */
320 mutex->__data.__count = 1;
321 /* But it is inconsistent unless marked otherwise. */
322 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
324 ENQUEUE_MUTEX_PI (mutex);
325 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
327 /* Note that we deliberately exit here. If we fall
328 through to the end of the function __nusers would be
329 incremented which is not correct because the old owner
330 has to be discounted. */
335 && __builtin_expect (mutex->__data.__owner
336 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
338 /* This mutex is now not recoverable. */
339 mutex->__data.__count = 0;
341 INTERNAL_SYSCALL_DECL (__err);
342 INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
343 __lll_private_flag (FUTEX_UNLOCK_PI,
344 PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
347 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
348 return ENOTRECOVERABLE;
351 mutex->__data.__count = 1;
354 ENQUEUE_MUTEX_PI (mutex);
355 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
360 case PTHREAD_MUTEX_PP_RECURSIVE_NP:
361 case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
362 case PTHREAD_MUTEX_PP_NORMAL_NP:
363 case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
365 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
367 oldval = mutex->__data.__lock;
369 /* Check whether we already hold the mutex. */
370 if (mutex->__data.__owner == id)
372 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
375 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
377 /* Just bump the counter. */
378 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
379 /* Overflow of the counter. */
382 ++mutex->__data.__count;
388 int oldprio = -1, ceilval;
391 int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
392 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
394 if (__pthread_current_priority () > ceiling)
399 __pthread_tpp_change_priority (oldprio, -1);
403 result = __pthread_tpp_change_priority (oldprio, ceiling);
407 ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
411 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
412 ceilval | 1, ceilval);
414 if (oldval == ceilval)
420 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
424 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
427 if (oldval != ceilval)
429 /* Reject invalid timeouts. */
430 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
439 /* Get the current time. */
440 (void) gettimeofday (&tv, NULL);
442 /* Compute relative timeout. */
443 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
444 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
447 rt.tv_nsec += 1000000000;
451 /* Already timed out? */
458 lll_futex_timed_wait (&mutex->__data.__lock,
460 PTHREAD_MUTEX_PSHARED (mutex));
463 while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
464 ceilval | 2, ceilval)
467 while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
469 assert (mutex->__data.__owner == 0);
470 mutex->__data.__count = 1;
475 /* Correct code cannot set any other type. */
481 /* Record the ownership. */
482 mutex->__data.__owner = id;
483 ++mutex->__data.__nusers;