* locales/as_IN: Fix currency_symbol, abday for Sunday, abmon for
[glibc/pb-stable.git] / nptl / pthread_mutex_timedlock.c
blobc8e6b8507a30157ec4bc6d8737a7c4e0dd14156b
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006 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
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <time.h>
23 #include "pthreadP.h"
24 #include <lowlevellock.h>
25 #include <not-cancel.h>
28 int
29 pthread_mutex_timedlock (mutex, abstime)
30 pthread_mutex_t *mutex;
31 const struct timespec *abstime;
33 int oldval;
34 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
35 int result = 0;
37 /* We must not check ABSTIME here. If the thread does not block
38 abstime must not be checked for a valid value. */
40 switch (__builtin_expect (mutex->__data.__kind, PTHREAD_MUTEX_TIMED_NP))
42 /* Recursive mutex. */
43 case PTHREAD_MUTEX_RECURSIVE_NP:
44 /* Check whether we already hold the mutex. */
45 if (mutex->__data.__owner == id)
47 /* Just bump the counter. */
48 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
49 /* Overflow of the counter. */
50 return EAGAIN;
52 ++mutex->__data.__count;
54 goto out;
57 /* We have to get the mutex. */
58 result = lll_mutex_timedlock (mutex->__data.__lock, abstime);
60 if (result != 0)
61 goto out;
63 /* Only locked once so far. */
64 mutex->__data.__count = 1;
65 break;
67 /* Error checking mutex. */
68 case PTHREAD_MUTEX_ERRORCHECK_NP:
69 /* Check whether we already hold the mutex. */
70 if (__builtin_expect (mutex->__data.__owner == id, 0))
71 return EDEADLK;
73 /* FALLTHROUGH */
75 case PTHREAD_MUTEX_TIMED_NP:
76 simple:
77 /* Normal mutex. */
78 result = lll_mutex_timedlock (mutex->__data.__lock, abstime);
79 break;
81 case PTHREAD_MUTEX_ADAPTIVE_NP:
82 if (! __is_smp)
83 goto simple;
85 if (lll_mutex_trylock (mutex->__data.__lock) != 0)
87 int cnt = 0;
88 int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
89 mutex->__data.__spins * 2 + 10);
92 if (cnt++ >= max_cnt)
94 result = lll_mutex_timedlock (mutex->__data.__lock, abstime);
95 break;
98 #ifdef BUSY_WAIT_NOP
99 BUSY_WAIT_NOP;
100 #endif
102 while (lll_mutex_trylock (mutex->__data.__lock) != 0);
104 mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
106 break;
108 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
109 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
110 case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
111 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
112 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
113 &mutex->__data.__list.__next);
115 oldval = mutex->__data.__lock;
118 again:
119 if ((oldval & FUTEX_OWNER_DIED) != 0)
121 /* The previous owner died. Try locking the mutex. */
122 int newval
123 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
124 id, oldval);
125 if (newval != oldval)
127 oldval = newval;
128 goto again;
131 /* We got the mutex. */
132 mutex->__data.__count = 1;
133 /* But it is inconsistent unless marked otherwise. */
134 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
136 ENQUEUE_MUTEX (mutex);
137 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
139 /* Note that we deliberately exit here. If we fall
140 through to the end of the function __nusers would be
141 incremented which is not correct because the old
142 owner has to be discounted. */
143 return EOWNERDEAD;
146 /* Check whether we already hold the mutex. */
147 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
149 if (mutex->__data.__kind
150 == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
152 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
153 NULL);
154 return EDEADLK;
157 if (mutex->__data.__kind
158 == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
160 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
161 NULL);
163 /* Just bump the counter. */
164 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
165 /* Overflow of the counter. */
166 return EAGAIN;
168 ++mutex->__data.__count;
170 return 0;
174 result = lll_robust_mutex_timedlock (mutex->__data.__lock, abstime,
175 id);
177 if (__builtin_expect (mutex->__data.__owner
178 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
180 /* This mutex is now not recoverable. */
181 mutex->__data.__count = 0;
182 lll_mutex_unlock (mutex->__data.__lock);
183 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
184 return ENOTRECOVERABLE;
187 if (result == ETIMEDOUT || result == EINVAL)
188 goto out;
190 oldval = result;
192 while ((oldval & FUTEX_OWNER_DIED) != 0);
194 mutex->__data.__count = 1;
195 ENQUEUE_MUTEX (mutex);
196 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
197 break;
199 case PTHREAD_MUTEX_PI_RECURSIVE_NP:
200 case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
201 case PTHREAD_MUTEX_PI_NORMAL_NP:
202 case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
203 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
204 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
205 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
206 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
208 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
209 int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
211 if (robust)
212 /* Note: robust PI futexes are signaled by setting bit 0. */
213 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
214 (void *) (((uintptr_t) &mutex->__data.__list.__next)
215 | 1));
217 oldval = mutex->__data.__lock;
219 /* Check whether we already hold the mutex. */
220 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
222 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
224 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
225 return EDEADLK;
228 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
230 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
232 /* Just bump the counter. */
233 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
234 /* Overflow of the counter. */
235 return EAGAIN;
237 ++mutex->__data.__count;
239 return 0;
243 oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
244 id, 0);
246 if (oldval != 0)
248 /* The mutex is locked. The kernel will now take care of
249 everything. The timeout value must be a relative value.
250 Convert it. */
251 INTERNAL_SYSCALL_DECL (__err);
253 int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
254 FUTEX_LOCK_PI, 1, abstime);
255 if (INTERNAL_SYSCALL_ERROR_P (e, __err))
257 if (INTERNAL_SYSCALL_ERRNO (e, __err) == ETIMEDOUT)
258 return ETIMEDOUT;
260 if (INTERNAL_SYSCALL_ERRNO (e, __err) == ESRCH
261 || INTERNAL_SYSCALL_ERRNO (e, __err) == EDEADLK)
263 assert (INTERNAL_SYSCALL_ERRNO (e, __err) != EDEADLK
264 || (kind != PTHREAD_MUTEX_ERRORCHECK_NP
265 && kind != PTHREAD_MUTEX_RECURSIVE_NP));
266 /* ESRCH can happen only for non-robust PI mutexes where
267 the owner of the lock died. */
268 assert (INTERNAL_SYSCALL_ERRNO (e, __err) != ESRCH
269 || !robust);
271 /* Delay the thread until the timeout is reached.
272 Then return ETIMEDOUT. */
273 struct timespec reltime;
274 struct timespec now;
276 INTERNAL_SYSCALL (clock_gettime, __err, 2, CLOCK_REALTIME,
277 &now);
278 reltime.tv_sec = abstime->tv_sec - now.tv_sec;
279 reltime.tv_nsec = abstime->tv_nsec - now.tv_nsec;
280 if (reltime.tv_nsec < 0)
282 reltime.tv_nsec += 1000000000;
283 --reltime.tv_sec;
285 if (reltime.tv_sec >= 0)
286 while (nanosleep_not_cancel (&reltime, &reltime) != 0)
287 continue;
289 return ETIMEDOUT;
292 return INTERNAL_SYSCALL_ERRNO (e, __err);
295 oldval = mutex->__data.__lock;
297 assert (robust || (oldval & FUTEX_OWNER_DIED) == 0);
300 if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
302 atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
304 /* We got the mutex. */
305 mutex->__data.__count = 1;
306 /* But it is inconsistent unless marked otherwise. */
307 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
309 ENQUEUE_MUTEX_PI (mutex);
310 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
312 /* Note that we deliberately exit here. If we fall
313 through to the end of the function __nusers would be
314 incremented which is not correct because the old owner
315 has to be discounted. */
316 return EOWNERDEAD;
319 if (robust
320 && __builtin_expect (mutex->__data.__owner
321 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
323 /* This mutex is now not recoverable. */
324 mutex->__data.__count = 0;
326 INTERNAL_SYSCALL_DECL (__err);
327 INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
328 FUTEX_UNLOCK_PI, 0, 0);
330 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
331 return ENOTRECOVERABLE;
334 mutex->__data.__count = 1;
335 if (robust)
337 ENQUEUE_MUTEX_PI (mutex);
338 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
341 break;
343 case PTHREAD_MUTEX_PP_RECURSIVE_NP:
344 case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
345 case PTHREAD_MUTEX_PP_NORMAL_NP:
346 case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
348 int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
350 oldval = mutex->__data.__lock;
352 /* Check whether we already hold the mutex. */
353 if (mutex->__data.__owner == id)
355 if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
356 return EDEADLK;
358 if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
360 /* Just bump the counter. */
361 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
362 /* Overflow of the counter. */
363 return EAGAIN;
365 ++mutex->__data.__count;
367 return 0;
371 int oldprio = -1, ceilval;
374 int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
375 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
377 if (__pthread_current_priority () > ceiling)
379 result = EINVAL;
380 failpp:
381 if (oldprio != -1)
382 __pthread_tpp_change_priority (oldprio, -1);
383 return result;
386 result = __pthread_tpp_change_priority (oldprio, ceiling);
387 if (result)
388 return result;
390 ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
391 oldprio = ceiling;
393 oldval
394 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
395 ceilval | 1, ceilval);
397 if (oldval == ceilval)
398 break;
402 oldval
403 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
404 ceilval | 2,
405 ceilval | 1);
407 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
408 break;
410 if (oldval != ceilval)
412 /* Reject invalid timeouts. */
413 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
415 result = EINVAL;
416 goto failpp;
419 struct timeval tv;
420 struct timespec rt;
422 /* Get the current time. */
423 (void) __gettimeofday (&tv, NULL);
425 /* Compute relative timeout. */
426 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
427 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
428 if (rt.tv_nsec < 0)
430 rt.tv_nsec += 1000000000;
431 --rt.tv_sec;
434 /* Already timed out? */
435 if (rt.tv_sec < 0)
437 result = ETIMEDOUT;
438 goto failpp;
441 lll_futex_timed_wait (&mutex->__data.__lock,
442 ceilval | 2, &rt);
445 while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
446 ceilval | 2, ceilval)
447 != ceilval);
449 while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
451 assert (mutex->__data.__owner == 0);
452 mutex->__data.__count = 1;
454 break;
456 default:
457 /* Correct code cannot set any other type. */
458 return EINVAL;
461 if (result == 0)
463 /* Record the ownership. */
464 mutex->__data.__owner = id;
465 ++mutex->__data.__nusers;
468 out:
469 return result;