libcrypt: set_errno to something valid
[uclibc-ng.git] / libpthread / nptl / pthread_mutex_timedlock.c
blobf56f6c55ec9a377fcf2939f2ed431ecdbdd5139c
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 /* We need to build this function with optimization to avoid
27 * lll_timedlock erroring out with
28 * error: can't find a register in class ‘GENERAL_REGS’ while reloading ‘asm’
30 int
31 #ifndef __OPTIMIZE__
32 attribute_optimize("Os")
33 #endif
34 pthread_mutex_timedlock (
35 pthread_mutex_t *mutex,
36 const struct timespec *abstime)
38 int oldval;
39 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
40 int result = 0;
42 /* We must not check ABSTIME here. If the thread does not block
43 abstime must not be checked for a valid value. */
45 switch (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex),
46 PTHREAD_MUTEX_TIMED_NP))
48 /* Recursive mutex. */
49 case PTHREAD_MUTEX_RECURSIVE_NP:
50 /* Check whether we already hold the mutex. */
51 if (mutex->__data.__owner == id)
53 /* Just bump the counter. */
54 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
55 /* Overflow of the counter. */
56 return EAGAIN;
58 ++mutex->__data.__count;
60 goto out;
63 /* We have to get the mutex. */
64 result = lll_timedlock (mutex->__data.__lock, abstime,
65 PTHREAD_MUTEX_PSHARED (mutex));
67 if (result != 0)
68 goto out;
70 /* Only locked once so far. */
71 mutex->__data.__count = 1;
72 break;
74 /* Error checking mutex. */
75 case PTHREAD_MUTEX_ERRORCHECK_NP:
76 /* Check whether we already hold the mutex. */
77 if (__builtin_expect (mutex->__data.__owner == id, 0))
78 return EDEADLK;
80 /* FALLTHROUGH */
82 case PTHREAD_MUTEX_TIMED_NP:
83 simple:
84 /* Normal mutex. */
85 result = lll_timedlock (mutex->__data.__lock, abstime,
86 PTHREAD_MUTEX_PSHARED (mutex));
87 break;
89 case PTHREAD_MUTEX_ADAPTIVE_NP:
90 if (! __is_smp)
91 goto simple;
93 if (lll_trylock (mutex->__data.__lock) != 0)
95 int cnt = 0;
96 int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
97 mutex->__data.__spins * 2 + 10);
100 if (cnt++ >= max_cnt)
102 result = lll_timedlock (mutex->__data.__lock, abstime,
103 PTHREAD_MUTEX_PSHARED (mutex));
104 break;
107 #ifdef BUSY_WAIT_NOP
108 BUSY_WAIT_NOP;
109 #endif
111 while (lll_trylock (mutex->__data.__lock) != 0);
113 mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
115 break;
117 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
118 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
119 case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
120 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
121 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
122 &mutex->__data.__list.__next);
124 oldval = mutex->__data.__lock;
127 again:
128 if ((oldval & FUTEX_OWNER_DIED) != 0)
130 /* The previous owner died. Try locking the mutex. */
131 int newval = id | (oldval & FUTEX_WAITERS);
133 newval
134 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
135 newval, oldval);
136 if (newval != oldval)
138 oldval = newval;
139 goto again;
142 /* We got the mutex. */
143 mutex->__data.__count = 1;
144 /* But it is inconsistent unless marked otherwise. */
145 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
147 ENQUEUE_MUTEX (mutex);
148 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
150 /* Note that we deliberately exit here. If we fall
151 through to the end of the function __nusers would be
152 incremented which is not correct because the old
153 owner has to be discounted. */
154 return EOWNERDEAD;
157 /* Check whether we already hold the mutex. */
158 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
160 int kind = PTHREAD_MUTEX_TYPE (mutex);
161 if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
163 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
164 NULL);
165 return EDEADLK;
168 if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
170 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
171 NULL);
173 /* Just bump the counter. */
174 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
175 /* Overflow of the counter. */
176 return EAGAIN;
178 ++mutex->__data.__count;
180 return 0;
184 result = lll_robust_timedlock (mutex->__data.__lock, abstime, id,
185 PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
187 if (__builtin_expect (mutex->__data.__owner
188 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
190 /* This mutex is now not recoverable. */
191 mutex->__data.__count = 0;
192 lll_unlock (mutex->__data.__lock,
193 PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
194 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
195 return ENOTRECOVERABLE;
198 if (result == ETIMEDOUT || result == EINVAL)
199 goto out;
201 oldval = result;
203 while ((oldval & FUTEX_OWNER_DIED) != 0);
205 mutex->__data.__count = 1;
206 ENQUEUE_MUTEX (mutex);
207 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
208 break;
210 case PTHREAD_MUTEX_PI_RECURSIVE_NP:
211 case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
212 case PTHREAD_MUTEX_PI_NORMAL_NP:
213 case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
214 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
215 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
216 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
217 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
219 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
220 int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
222 if (robust)
223 /* Note: robust PI futexes are signaled by setting bit 0. */
224 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
225 (void *) (((uintptr_t) &mutex->__data.__list.__next)
226 | 1));
228 oldval = mutex->__data.__lock;
230 /* Check whether we already hold the mutex. */
231 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
233 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
235 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
236 return EDEADLK;
239 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
241 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
243 /* Just bump the counter. */
244 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
245 /* Overflow of the counter. */
246 return EAGAIN;
248 ++mutex->__data.__count;
250 return 0;
254 oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
255 id, 0);
257 if (oldval != 0)
259 /* The mutex is locked. The kernel will now take care of
260 everything. The timeout value must be a relative value.
261 Convert it. */
262 int private = (robust
263 ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
264 : PTHREAD_MUTEX_PSHARED (mutex));
265 INTERNAL_SYSCALL_DECL (__err);
267 int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
268 __lll_private_flag (FUTEX_LOCK_PI,
269 private), 1,
270 abstime);
271 if (INTERNAL_SYSCALL_ERROR_P (e, __err))
273 if (INTERNAL_SYSCALL_ERRNO (e, __err) == ETIMEDOUT)
274 return ETIMEDOUT;
276 if (INTERNAL_SYSCALL_ERRNO (e, __err) == ESRCH
277 || INTERNAL_SYSCALL_ERRNO (e, __err) == EDEADLK)
279 assert (INTERNAL_SYSCALL_ERRNO (e, __err) != EDEADLK
280 || (kind != PTHREAD_MUTEX_ERRORCHECK_NP
281 && kind != PTHREAD_MUTEX_RECURSIVE_NP));
282 /* ESRCH can happen only for non-robust PI mutexes where
283 the owner of the lock died. */
284 assert (INTERNAL_SYSCALL_ERRNO (e, __err) != ESRCH
285 || !robust);
287 /* Delay the thread until the timeout is reached.
288 Then return ETIMEDOUT. */
289 struct timespec reltime;
290 struct timespec now;
292 INTERNAL_SYSCALL (clock_gettime, __err, 2, CLOCK_REALTIME,
293 &now);
294 reltime.tv_sec = abstime->tv_sec - now.tv_sec;
295 reltime.tv_nsec = abstime->tv_nsec - now.tv_nsec;
296 if (reltime.tv_nsec < 0)
298 reltime.tv_nsec += 1000000000;
299 --reltime.tv_sec;
301 if (reltime.tv_sec >= 0)
302 while (nanosleep_not_cancel (&reltime, &reltime) != 0)
303 continue;
305 return ETIMEDOUT;
308 return INTERNAL_SYSCALL_ERRNO (e, __err);
311 oldval = mutex->__data.__lock;
313 assert (robust || (oldval & FUTEX_OWNER_DIED) == 0);
316 if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
318 atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
320 /* We got the mutex. */
321 mutex->__data.__count = 1;
322 /* But it is inconsistent unless marked otherwise. */
323 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
325 ENQUEUE_MUTEX_PI (mutex);
326 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
328 /* Note that we deliberately exit here. If we fall
329 through to the end of the function __nusers would be
330 incremented which is not correct because the old owner
331 has to be discounted. */
332 return EOWNERDEAD;
335 if (robust
336 && __builtin_expect (mutex->__data.__owner
337 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
339 /* This mutex is now not recoverable. */
340 mutex->__data.__count = 0;
342 INTERNAL_SYSCALL_DECL (__err);
343 INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
344 __lll_private_flag (FUTEX_UNLOCK_PI,
345 PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
346 0, 0);
348 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
349 return ENOTRECOVERABLE;
352 mutex->__data.__count = 1;
353 if (robust)
355 ENQUEUE_MUTEX_PI (mutex);
356 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
359 break;
361 case PTHREAD_MUTEX_PP_RECURSIVE_NP:
362 case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
363 case PTHREAD_MUTEX_PP_NORMAL_NP:
364 case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
366 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
368 oldval = mutex->__data.__lock;
370 /* Check whether we already hold the mutex. */
371 if (mutex->__data.__owner == id)
373 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
374 return EDEADLK;
376 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
378 /* Just bump the counter. */
379 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
380 /* Overflow of the counter. */
381 return EAGAIN;
383 ++mutex->__data.__count;
385 return 0;
389 int oldprio = -1, ceilval;
392 int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
393 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
395 if (__pthread_current_priority () > ceiling)
397 result = EINVAL;
398 failpp:
399 if (oldprio != -1)
400 __pthread_tpp_change_priority (oldprio, -1);
401 return result;
404 result = __pthread_tpp_change_priority (oldprio, ceiling);
405 if (result)
406 return result;
408 ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
409 oldprio = ceiling;
411 oldval
412 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
413 ceilval | 1, ceilval);
415 if (oldval == ceilval)
416 break;
420 oldval
421 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
422 ceilval | 2,
423 ceilval | 1);
425 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
426 break;
428 if (oldval != ceilval)
430 /* Reject invalid timeouts. */
431 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
433 result = EINVAL;
434 goto failpp;
437 struct timeval tv;
438 struct timespec rt;
440 /* Get the current time. */
441 (void) gettimeofday (&tv, NULL);
443 /* Compute relative timeout. */
444 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
445 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
446 if (rt.tv_nsec < 0)
448 rt.tv_nsec += 1000000000;
449 --rt.tv_sec;
452 /* Already timed out? */
453 if (rt.tv_sec < 0)
455 result = ETIMEDOUT;
456 goto failpp;
459 lll_futex_timed_wait (&mutex->__data.__lock,
460 ceilval | 2, &rt,
461 PTHREAD_MUTEX_PSHARED (mutex));
464 while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
465 ceilval | 2, ceilval)
466 != ceilval);
468 while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
470 assert (mutex->__data.__owner == 0);
471 mutex->__data.__count = 1;
473 break;
475 default:
476 /* Correct code cannot set any other type. */
477 return EINVAL;
480 if (result == 0)
482 /* Record the ownership. */
483 mutex->__data.__owner = id;
484 ++mutex->__data.__nusers;
487 out:
488 return result;