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>
29 pthread_mutex_timedlock (mutex
, abstime
)
30 pthread_mutex_t
*mutex
;
31 const struct timespec
*abstime
;
34 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
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 (PTHREAD_MUTEX_TYPE (mutex
),
41 PTHREAD_MUTEX_TIMED_NP
))
43 /* Recursive mutex. */
44 case PTHREAD_MUTEX_RECURSIVE_NP
:
45 /* Check whether we already hold the mutex. */
46 if (mutex
->__data
.__owner
== id
)
48 /* Just bump the counter. */
49 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
50 /* Overflow of the counter. */
53 ++mutex
->__data
.__count
;
58 /* We have to get the mutex. */
59 result
= lll_timedlock (mutex
->__data
.__lock
, abstime
,
60 PTHREAD_MUTEX_PSHARED (mutex
));
65 /* Only locked once so far. */
66 mutex
->__data
.__count
= 1;
69 /* Error checking mutex. */
70 case PTHREAD_MUTEX_ERRORCHECK_NP
:
71 /* Check whether we already hold the mutex. */
72 if (__builtin_expect (mutex
->__data
.__owner
== id
, 0))
77 case PTHREAD_MUTEX_TIMED_NP
:
80 result
= lll_timedlock (mutex
->__data
.__lock
, abstime
,
81 PTHREAD_MUTEX_PSHARED (mutex
));
84 case PTHREAD_MUTEX_ADAPTIVE_NP
:
88 if (lll_trylock (mutex
->__data
.__lock
) != 0)
91 int max_cnt
= MIN (MAX_ADAPTIVE_COUNT
,
92 mutex
->__data
.__spins
* 2 + 10);
97 result
= lll_timedlock (mutex
->__data
.__lock
, abstime
,
98 PTHREAD_MUTEX_PSHARED (mutex
));
106 while (lll_trylock (mutex
->__data
.__lock
) != 0);
108 mutex
->__data
.__spins
+= (cnt
- mutex
->__data
.__spins
) / 8;
112 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
:
113 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
:
114 case PTHREAD_MUTEX_ROBUST_NORMAL_NP
:
115 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP
:
116 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
117 &mutex
->__data
.__list
.__next
);
119 oldval
= mutex
->__data
.__lock
;
123 if ((oldval
& FUTEX_OWNER_DIED
) != 0)
125 /* The previous owner died. Try locking the mutex. */
126 int newval
= id
| (oldval
& FUTEX_WAITERS
);
129 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
131 if (newval
!= oldval
)
137 /* We got the mutex. */
138 mutex
->__data
.__count
= 1;
139 /* But it is inconsistent unless marked otherwise. */
140 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
142 ENQUEUE_MUTEX (mutex
);
143 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
145 /* Note that we deliberately exit here. If we fall
146 through to the end of the function __nusers would be
147 incremented which is not correct because the old
148 owner has to be discounted. */
152 /* Check whether we already hold the mutex. */
153 if (__builtin_expect ((oldval
& FUTEX_TID_MASK
) == id
, 0))
155 int kind
= PTHREAD_MUTEX_TYPE (mutex
);
156 if (kind
== PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
)
158 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
163 if (kind
== PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
)
165 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
168 /* Just bump the counter. */
169 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
170 /* Overflow of the counter. */
173 ++mutex
->__data
.__count
;
179 result
= lll_robust_timedlock (mutex
->__data
.__lock
, abstime
, id
,
180 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
));
182 if (__builtin_expect (mutex
->__data
.__owner
183 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
185 /* This mutex is now not recoverable. */
186 mutex
->__data
.__count
= 0;
187 lll_unlock (mutex
->__data
.__lock
,
188 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
));
189 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
190 return ENOTRECOVERABLE
;
193 if (result
== ETIMEDOUT
|| result
== EINVAL
)
198 while ((oldval
& FUTEX_OWNER_DIED
) != 0);
200 mutex
->__data
.__count
= 1;
201 ENQUEUE_MUTEX (mutex
);
202 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
205 case PTHREAD_MUTEX_PI_RECURSIVE_NP
:
206 case PTHREAD_MUTEX_PI_ERRORCHECK_NP
:
207 case PTHREAD_MUTEX_PI_NORMAL_NP
:
208 case PTHREAD_MUTEX_PI_ADAPTIVE_NP
:
209 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP
:
210 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP
:
211 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP
:
212 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP
:
214 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
215 int robust
= mutex
->__data
.__kind
& PTHREAD_MUTEX_ROBUST_NORMAL_NP
;
218 /* Note: robust PI futexes are signaled by setting bit 0. */
219 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
220 (void *) (((uintptr_t) &mutex
->__data
.__list
.__next
)
223 oldval
= mutex
->__data
.__lock
;
225 /* Check whether we already hold the mutex. */
226 if (__builtin_expect ((oldval
& FUTEX_TID_MASK
) == id
, 0))
228 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
230 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
234 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
236 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
238 /* Just bump the counter. */
239 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
240 /* Overflow of the counter. */
243 ++mutex
->__data
.__count
;
249 oldval
= atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
254 /* The mutex is locked. The kernel will now take care of
255 everything. The timeout value must be a relative value.
257 int private = (robust
258 ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex
)
259 : PTHREAD_MUTEX_PSHARED (mutex
));
260 INTERNAL_SYSCALL_DECL (__err
);
262 int e
= INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
263 __lll_private_flag (FUTEX_LOCK_PI
,
266 if (INTERNAL_SYSCALL_ERROR_P (e
, __err
))
268 if (INTERNAL_SYSCALL_ERRNO (e
, __err
) == ETIMEDOUT
)
271 if (INTERNAL_SYSCALL_ERRNO (e
, __err
) == ESRCH
272 || INTERNAL_SYSCALL_ERRNO (e
, __err
) == EDEADLK
)
274 assert (INTERNAL_SYSCALL_ERRNO (e
, __err
) != EDEADLK
275 || (kind
!= PTHREAD_MUTEX_ERRORCHECK_NP
276 && kind
!= PTHREAD_MUTEX_RECURSIVE_NP
));
277 /* ESRCH can happen only for non-robust PI mutexes where
278 the owner of the lock died. */
279 assert (INTERNAL_SYSCALL_ERRNO (e
, __err
) != ESRCH
282 /* Delay the thread until the timeout is reached.
283 Then return ETIMEDOUT. */
284 struct timespec reltime
;
287 INTERNAL_SYSCALL (clock_gettime
, __err
, 2, CLOCK_REALTIME
,
289 reltime
.tv_sec
= abstime
->tv_sec
- now
.tv_sec
;
290 reltime
.tv_nsec
= abstime
->tv_nsec
- now
.tv_nsec
;
291 if (reltime
.tv_nsec
< 0)
293 reltime
.tv_nsec
+= 1000000000;
296 if (reltime
.tv_sec
>= 0)
297 while (nanosleep_not_cancel (&reltime
, &reltime
) != 0)
303 return INTERNAL_SYSCALL_ERRNO (e
, __err
);
306 oldval
= mutex
->__data
.__lock
;
308 assert (robust
|| (oldval
& FUTEX_OWNER_DIED
) == 0);
311 if (__builtin_expect (oldval
& FUTEX_OWNER_DIED
, 0))
313 atomic_and (&mutex
->__data
.__lock
, ~FUTEX_OWNER_DIED
);
315 /* We got the mutex. */
316 mutex
->__data
.__count
= 1;
317 /* But it is inconsistent unless marked otherwise. */
318 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
320 ENQUEUE_MUTEX_PI (mutex
);
321 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
323 /* Note that we deliberately exit here. If we fall
324 through to the end of the function __nusers would be
325 incremented which is not correct because the old owner
326 has to be discounted. */
331 && __builtin_expect (mutex
->__data
.__owner
332 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
334 /* This mutex is now not recoverable. */
335 mutex
->__data
.__count
= 0;
337 INTERNAL_SYSCALL_DECL (__err
);
338 INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
339 __lll_private_flag (FUTEX_UNLOCK_PI
,
340 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
)),
343 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
344 return ENOTRECOVERABLE
;
347 mutex
->__data
.__count
= 1;
350 ENQUEUE_MUTEX_PI (mutex
);
351 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
356 case PTHREAD_MUTEX_PP_RECURSIVE_NP
:
357 case PTHREAD_MUTEX_PP_ERRORCHECK_NP
:
358 case PTHREAD_MUTEX_PP_NORMAL_NP
:
359 case PTHREAD_MUTEX_PP_ADAPTIVE_NP
:
361 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
363 oldval
= mutex
->__data
.__lock
;
365 /* Check whether we already hold the mutex. */
366 if (mutex
->__data
.__owner
== id
)
368 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
371 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
373 /* Just bump the counter. */
374 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
375 /* Overflow of the counter. */
378 ++mutex
->__data
.__count
;
384 int oldprio
= -1, ceilval
;
387 int ceiling
= (oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
)
388 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
390 if (__pthread_current_priority () > ceiling
)
395 __pthread_tpp_change_priority (oldprio
, -1);
399 result
= __pthread_tpp_change_priority (oldprio
, ceiling
);
403 ceilval
= ceiling
<< PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
407 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
408 ceilval
| 1, ceilval
);
410 if (oldval
== ceilval
)
416 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
420 if ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
)
423 if (oldval
!= ceilval
)
425 /* Reject invalid timeouts. */
426 if (abstime
->tv_nsec
< 0 || abstime
->tv_nsec
>= 1000000000)
435 /* Get the current time. */
436 (void) __gettimeofday (&tv
, NULL
);
438 /* Compute relative timeout. */
439 rt
.tv_sec
= abstime
->tv_sec
- tv
.tv_sec
;
440 rt
.tv_nsec
= abstime
->tv_nsec
- tv
.tv_usec
* 1000;
443 rt
.tv_nsec
+= 1000000000;
447 /* Already timed out? */
454 lll_futex_timed_wait (&mutex
->__data
.__lock
,
456 PTHREAD_MUTEX_PSHARED (mutex
));
459 while (atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
460 ceilval
| 2, ceilval
)
463 while ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
);
465 assert (mutex
->__data
.__owner
== 0);
466 mutex
->__data
.__count
= 1;
471 /* Correct code cannot set any other type. */
477 /* Record the ownership. */
478 mutex
->__data
.__owner
= id
;
479 ++mutex
->__data
.__nusers
;