Add support for using time64 on big-endian machines.
[uclibc-ng.git] / libpthread / nptl / pthread_mutex_timedlock.c
blobd54983315ed24a87e3a1925c1956f1aa45914b1f
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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <time.h>
22 #include "pthreadP.h"
23 #include <lowlevellock.h>
24 #include <not-cancel.h>
26 #if defined(__UCLIBC_USE_TIME64__)
27 #include "internal/time64_helpers.h"
28 #endif
30 /* We need to build this function with optimization to avoid
31 * lll_timedlock erroring out with
32 * error: can't find a register in class ‘GENERAL_REGS’ while reloading ‘asm’
34 int
35 #ifndef __OPTIMIZE__
36 attribute_optimize("Os")
37 #endif
38 pthread_mutex_timedlock (
39 pthread_mutex_t *mutex,
40 const struct timespec *abstime)
42 int oldval;
43 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
44 int result = 0;
46 /* We must not check ABSTIME here. If the thread does not block
47 abstime must not be checked for a valid value. */
49 switch (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex),
50 PTHREAD_MUTEX_TIMED_NP))
52 /* Recursive mutex. */
53 case PTHREAD_MUTEX_RECURSIVE_NP:
54 /* Check whether we already hold the mutex. */
55 if (mutex->__data.__owner == id)
57 /* Just bump the counter. */
58 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
59 /* Overflow of the counter. */
60 return EAGAIN;
62 ++mutex->__data.__count;
64 goto out;
67 /* We have to get the mutex. */
68 result = lll_timedlock (mutex->__data.__lock, abstime,
69 PTHREAD_MUTEX_PSHARED (mutex));
71 if (result != 0)
72 goto out;
74 /* Only locked once so far. */
75 mutex->__data.__count = 1;
76 break;
78 /* Error checking mutex. */
79 case PTHREAD_MUTEX_ERRORCHECK_NP:
80 /* Check whether we already hold the mutex. */
81 if (__builtin_expect (mutex->__data.__owner == id, 0))
82 return EDEADLK;
84 /* FALLTHROUGH */
86 case PTHREAD_MUTEX_TIMED_NP:
87 simple:
88 /* Normal mutex. */
89 result = lll_timedlock (mutex->__data.__lock, abstime,
90 PTHREAD_MUTEX_PSHARED (mutex));
91 break;
93 case PTHREAD_MUTEX_ADAPTIVE_NP:
94 if (! __is_smp)
95 goto simple;
97 if (lll_trylock (mutex->__data.__lock) != 0)
99 int cnt = 0;
100 int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
101 mutex->__data.__spins * 2 + 10);
104 if (cnt++ >= max_cnt)
106 result = lll_timedlock (mutex->__data.__lock, abstime,
107 PTHREAD_MUTEX_PSHARED (mutex));
108 break;
111 #ifdef BUSY_WAIT_NOP
112 BUSY_WAIT_NOP;
113 #endif
115 while (lll_trylock (mutex->__data.__lock) != 0);
117 mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
119 break;
121 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
122 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
123 case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
124 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
125 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
126 &mutex->__data.__list.__next);
128 oldval = mutex->__data.__lock;
131 again:
132 if ((oldval & FUTEX_OWNER_DIED) != 0)
134 /* The previous owner died. Try locking the mutex. */
135 int newval = id | (oldval & FUTEX_WAITERS);
137 newval
138 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
139 newval, oldval);
140 if (newval != oldval)
142 oldval = newval;
143 goto again;
146 /* We got the mutex. */
147 mutex->__data.__count = 1;
148 /* But it is inconsistent unless marked otherwise. */
149 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
151 ENQUEUE_MUTEX (mutex);
152 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
154 /* Note that we deliberately exit here. If we fall
155 through to the end of the function __nusers would be
156 incremented which is not correct because the old
157 owner has to be discounted. */
158 return EOWNERDEAD;
161 /* Check whether we already hold the mutex. */
162 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
164 int kind = PTHREAD_MUTEX_TYPE (mutex);
165 if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
167 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
168 NULL);
169 return EDEADLK;
172 if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
174 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
175 NULL);
177 /* Just bump the counter. */
178 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
179 /* Overflow of the counter. */
180 return EAGAIN;
182 ++mutex->__data.__count;
184 return 0;
188 result = lll_robust_timedlock (mutex->__data.__lock, abstime, id,
189 PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
191 if (__builtin_expect (mutex->__data.__owner
192 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
194 /* This mutex is now not recoverable. */
195 mutex->__data.__count = 0;
196 lll_unlock (mutex->__data.__lock,
197 PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
198 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
199 return ENOTRECOVERABLE;
202 if (result == ETIMEDOUT || result == EINVAL)
203 goto out;
205 oldval = result;
207 while ((oldval & FUTEX_OWNER_DIED) != 0);
209 mutex->__data.__count = 1;
210 ENQUEUE_MUTEX (mutex);
211 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
212 break;
214 case PTHREAD_MUTEX_PI_RECURSIVE_NP:
215 case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
216 case PTHREAD_MUTEX_PI_NORMAL_NP:
217 case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
218 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
219 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
220 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
221 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
223 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
224 int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
226 if (robust)
227 /* Note: robust PI futexes are signaled by setting bit 0. */
228 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
229 (void *) (((uintptr_t) &mutex->__data.__list.__next)
230 | 1));
232 oldval = mutex->__data.__lock;
234 /* Check whether we already hold the mutex. */
235 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
237 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
239 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
240 return EDEADLK;
243 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
245 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
247 /* Just bump the counter. */
248 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
249 /* Overflow of the counter. */
250 return EAGAIN;
252 ++mutex->__data.__count;
254 return 0;
258 oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
259 id, 0);
261 if (oldval != 0)
263 /* The mutex is locked. The kernel will now take care of
264 everything. The timeout value must be a relative value.
265 Convert it. */
266 int private = (robust
267 ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
268 : PTHREAD_MUTEX_PSHARED (mutex));
269 INTERNAL_SYSCALL_DECL (__err);
271 #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
272 int e = INTERNAL_SYSCALL (futex_time64, __err, 4, &mutex->__data.__lock,
273 __lll_private_flag (FUTEX_LOCK_PI,
274 private), 1,
275 TO_TS64_P(abstime));
276 #else
277 int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
278 __lll_private_flag (FUTEX_LOCK_PI,
279 private), 1,
280 abstime);
281 #endif
282 if (INTERNAL_SYSCALL_ERROR_P (e, __err))
284 if (INTERNAL_SYSCALL_ERRNO (e, __err) == ETIMEDOUT)
285 return ETIMEDOUT;
287 if (INTERNAL_SYSCALL_ERRNO (e, __err) == ESRCH
288 || INTERNAL_SYSCALL_ERRNO (e, __err) == EDEADLK)
290 assert (INTERNAL_SYSCALL_ERRNO (e, __err) != EDEADLK
291 || (kind != PTHREAD_MUTEX_ERRORCHECK_NP
292 && kind != PTHREAD_MUTEX_RECURSIVE_NP));
293 /* ESRCH can happen only for non-robust PI mutexes where
294 the owner of the lock died. */
295 assert (INTERNAL_SYSCALL_ERRNO (e, __err) != ESRCH
296 || !robust);
298 /* Delay the thread until the timeout is reached.
299 Then return ETIMEDOUT. */
300 struct timespec reltime;
301 struct timespec now;
303 #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_clock_gettime64)
304 INTERNAL_SYSCALL (clock_gettime64, __err, 2, CLOCK_REALTIME,
305 &now);
306 #else
307 INTERNAL_SYSCALL (clock_gettime, __err, 2, CLOCK_REALTIME,
308 &now);
309 #endif
310 reltime.tv_sec = abstime->tv_sec - now.tv_sec;
311 reltime.tv_nsec = abstime->tv_nsec - now.tv_nsec;
312 if (reltime.tv_nsec < 0)
314 reltime.tv_nsec += 1000000000;
315 --reltime.tv_sec;
317 if (reltime.tv_sec >= 0)
318 while (nanosleep_not_cancel (&reltime, &reltime) != 0)
319 continue;
321 return ETIMEDOUT;
324 return INTERNAL_SYSCALL_ERRNO (e, __err);
327 oldval = mutex->__data.__lock;
329 assert (robust || (oldval & FUTEX_OWNER_DIED) == 0);
332 if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
334 atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
336 /* We got the mutex. */
337 mutex->__data.__count = 1;
338 /* But it is inconsistent unless marked otherwise. */
339 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
341 ENQUEUE_MUTEX_PI (mutex);
342 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
344 /* Note that we deliberately exit here. If we fall
345 through to the end of the function __nusers would be
346 incremented which is not correct because the old owner
347 has to be discounted. */
348 return EOWNERDEAD;
351 if (robust
352 && __builtin_expect (mutex->__data.__owner
353 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
355 /* This mutex is now not recoverable. */
356 mutex->__data.__count = 0;
358 INTERNAL_SYSCALL_DECL (__err);
359 #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
360 INTERNAL_SYSCALL (futex_time64, __err, 4, &mutex->__data.__lock,
361 __lll_private_flag (FUTEX_UNLOCK_PI,
362 PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
363 0, 0);
364 #else
365 INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
366 __lll_private_flag (FUTEX_UNLOCK_PI,
367 PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
368 0, 0);
369 #endif
371 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
372 return ENOTRECOVERABLE;
375 mutex->__data.__count = 1;
376 if (robust)
378 ENQUEUE_MUTEX_PI (mutex);
379 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
382 break;
384 case PTHREAD_MUTEX_PP_RECURSIVE_NP:
385 case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
386 case PTHREAD_MUTEX_PP_NORMAL_NP:
387 case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
389 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
391 oldval = mutex->__data.__lock;
393 /* Check whether we already hold the mutex. */
394 if (mutex->__data.__owner == id)
396 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
397 return EDEADLK;
399 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
401 /* Just bump the counter. */
402 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
403 /* Overflow of the counter. */
404 return EAGAIN;
406 ++mutex->__data.__count;
408 return 0;
412 int oldprio = -1, ceilval;
415 int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
416 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
418 if (__pthread_current_priority () > ceiling)
420 result = EINVAL;
421 failpp:
422 if (oldprio != -1)
423 __pthread_tpp_change_priority (oldprio, -1);
424 return result;
427 result = __pthread_tpp_change_priority (oldprio, ceiling);
428 if (result)
429 return result;
431 ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
432 oldprio = ceiling;
434 oldval
435 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
436 ceilval | 1, ceilval);
438 if (oldval == ceilval)
439 break;
443 oldval
444 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
445 ceilval | 2,
446 ceilval | 1);
448 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
449 break;
451 if (oldval != ceilval)
453 /* Reject invalid timeouts. */
454 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
456 result = EINVAL;
457 goto failpp;
460 struct timeval tv;
461 struct timespec rt;
463 /* Get the current time. */
464 (void) gettimeofday (&tv, NULL);
466 /* Compute relative timeout. */
467 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
468 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
469 if (rt.tv_nsec < 0)
471 rt.tv_nsec += 1000000000;
472 --rt.tv_sec;
475 /* Already timed out? */
476 if (rt.tv_sec < 0)
478 result = ETIMEDOUT;
479 goto failpp;
482 lll_futex_timed_wait (&mutex->__data.__lock,
483 ceilval | 2, &rt,
484 PTHREAD_MUTEX_PSHARED (mutex));
487 while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
488 ceilval | 2, ceilval)
489 != ceilval);
491 while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
493 assert (mutex->__data.__owner == 0);
494 mutex->__data.__count = 1;
496 break;
498 default:
499 /* Correct code cannot set any other type. */
500 return EINVAL;
503 if (result == 0)
505 /* Record the ownership. */
506 mutex->__data.__owner = id;
507 ++mutex->__data.__nusers;
510 out:
511 return result;