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
23 #include <lowlevellock.h>
27 pthread_mutex_timedlock (mutex
, abstime
)
28 pthread_mutex_t
*mutex
;
29 const struct timespec
*abstime
;
32 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
35 /* We must not check ABSTIME here. If the thread does not block
36 abstime must not be checked for a valid value. */
38 switch (mutex
->__data
.__kind
)
40 /* Recursive mutex. */
41 case PTHREAD_MUTEX_RECURSIVE_NP
:
42 /* Check whether we already hold the mutex. */
43 if (mutex
->__data
.__owner
== id
)
45 /* Just bump the counter. */
46 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
47 /* Overflow of the counter. */
50 ++mutex
->__data
.__count
;
55 /* We have to get the mutex. */
56 result
= lll_mutex_timedlock (mutex
->__data
.__lock
, abstime
);
61 /* Only locked once so far. */
62 mutex
->__data
.__count
= 1;
65 /* Error checking mutex. */
66 case PTHREAD_MUTEX_ERRORCHECK_NP
:
67 /* Check whether we already hold the mutex. */
68 if (mutex
->__data
.__owner
== id
)
73 case PTHREAD_MUTEX_TIMED_NP
:
76 result
= lll_mutex_timedlock (mutex
->__data
.__lock
, abstime
);
79 case PTHREAD_MUTEX_ADAPTIVE_NP
:
83 if (lll_mutex_trylock (mutex
->__data
.__lock
) != 0)
86 int max_cnt
= MIN (MAX_ADAPTIVE_COUNT
,
87 mutex
->__data
.__spins
* 2 + 10);
92 result
= lll_mutex_timedlock (mutex
->__data
.__lock
, abstime
);
100 while (lll_mutex_trylock (mutex
->__data
.__lock
) != 0);
102 mutex
->__data
.__spins
+= (cnt
- mutex
->__data
.__spins
) / 8;
106 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
:
107 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
:
108 case PTHREAD_MUTEX_ROBUST_NORMAL_NP
:
109 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP
:
110 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
111 &mutex
->__data
.__list
.__next
);
113 oldval
= mutex
->__data
.__lock
;
117 if ((oldval
& FUTEX_OWNER_DIED
) != 0)
119 /* The previous owner died. Try locking the mutex. */
121 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
123 if (newval
!= oldval
)
129 /* We got the mutex. */
130 mutex
->__data
.__count
= 1;
131 /* But it is inconsistent unless marked otherwise. */
132 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
134 ENQUEUE_MUTEX (mutex
);
135 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
137 /* Note that we deliberately exist here. If we fall
138 through to the end of the function __nusers would be
139 incremented which is not correct because the old
140 owner has to be discounted. */
144 /* Check whether we already hold the mutex. */
145 if (__builtin_expect ((oldval
& FUTEX_TID_MASK
) == id
, 0))
147 if (mutex
->__data
.__kind
148 == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
)
150 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
155 if (mutex
->__data
.__kind
156 == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
)
158 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
161 /* Just bump the counter. */
162 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
163 /* Overflow of the counter. */
166 ++mutex
->__data
.__count
;
172 result
= lll_robust_mutex_timedlock (mutex
->__data
.__lock
, abstime
,
175 if (__builtin_expect (mutex
->__data
.__owner
176 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
178 /* This mutex is now not recoverable. */
179 mutex
->__data
.__count
= 0;
180 lll_mutex_unlock (mutex
->__data
.__lock
);
181 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
182 return ENOTRECOVERABLE
;
185 if (result
== ETIMEDOUT
|| result
== EINVAL
)
190 while ((oldval
& FUTEX_OWNER_DIED
) != 0);
192 mutex
->__data
.__count
= 1;
193 ENQUEUE_MUTEX (mutex
);
194 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
198 /* Correct code cannot set any other type. */
204 /* Record the ownership. */
205 mutex
->__data
.__owner
= id
;
206 ++mutex
->__data
.__nusers
;