1 /* Copyright (C) 2002, 2003, 2005, 2006, 2007 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>
28 __pthread_mutex_trylock (mutex
)
29 pthread_mutex_t
*mutex
;
32 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
34 switch (__builtin_expect (mutex
->__data
.__kind
, PTHREAD_MUTEX_TIMED_NP
))
36 /* Recursive mutex. */
37 case PTHREAD_MUTEX_RECURSIVE_NP
:
38 /* Check whether we already hold the mutex. */
39 if (mutex
->__data
.__owner
== id
)
41 /* Just bump the counter. */
42 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
43 /* Overflow of the counter. */
46 ++mutex
->__data
.__count
;
50 if (lll_mutex_trylock (mutex
->__data
.__lock
) == 0)
52 /* Record the ownership. */
53 mutex
->__data
.__owner
= id
;
54 mutex
->__data
.__count
= 1;
55 ++mutex
->__data
.__nusers
;
60 case PTHREAD_MUTEX_ERRORCHECK_NP
:
61 case PTHREAD_MUTEX_TIMED_NP
:
62 case PTHREAD_MUTEX_ADAPTIVE_NP
:
64 if (lll_mutex_trylock (mutex
->__data
.__lock
) != 0)
67 /* Record the ownership. */
68 mutex
->__data
.__owner
= id
;
69 ++mutex
->__data
.__nusers
;
73 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
:
74 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
:
75 case PTHREAD_MUTEX_ROBUST_NORMAL_NP
:
76 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP
:
77 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
78 &mutex
->__data
.__list
.__next
);
80 oldval
= mutex
->__data
.__lock
;
84 if ((oldval
& FUTEX_OWNER_DIED
) != 0)
86 /* The previous owner died. Try locking the mutex. */
87 int newval
= id
| (oldval
& FUTEX_WAITERS
);
90 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
99 /* We got the mutex. */
100 mutex
->__data
.__count
= 1;
101 /* But it is inconsistent unless marked otherwise. */
102 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
104 ENQUEUE_MUTEX (mutex
);
105 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
107 /* Note that we deliberately exist here. If we fall
108 through to the end of the function __nusers would be
109 incremented which is not correct because the old
110 owner has to be discounted. */
114 /* Check whether we already hold the mutex. */
115 if (__builtin_expect ((oldval
& FUTEX_TID_MASK
) == id
, 0))
117 if (mutex
->__data
.__kind
118 == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
)
120 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
125 if (mutex
->__data
.__kind
126 == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
)
128 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
131 /* Just bump the counter. */
132 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
133 /* Overflow of the counter. */
136 ++mutex
->__data
.__count
;
142 oldval
= lll_robust_mutex_trylock (mutex
->__data
.__lock
, id
);
143 if (oldval
!= 0 && (oldval
& FUTEX_OWNER_DIED
) == 0)
145 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
150 if (__builtin_expect (mutex
->__data
.__owner
151 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
153 /* This mutex is now not recoverable. */
154 mutex
->__data
.__count
= 0;
156 lll_mutex_unlock (mutex
->__data
.__lock
);
157 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
158 return ENOTRECOVERABLE
;
161 while ((oldval
& FUTEX_OWNER_DIED
) != 0);
163 ENQUEUE_MUTEX (mutex
);
164 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
166 mutex
->__data
.__owner
= id
;
167 ++mutex
->__data
.__nusers
;
168 mutex
->__data
.__count
= 1;
172 case PTHREAD_MUTEX_PI_RECURSIVE_NP
:
173 case PTHREAD_MUTEX_PI_ERRORCHECK_NP
:
174 case PTHREAD_MUTEX_PI_NORMAL_NP
:
175 case PTHREAD_MUTEX_PI_ADAPTIVE_NP
:
176 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP
:
177 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP
:
178 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP
:
179 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP
:
181 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
182 int robust
= mutex
->__data
.__kind
& PTHREAD_MUTEX_ROBUST_NORMAL_NP
;
185 /* Note: robust PI futexes are signaled by setting bit 0. */
186 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
187 (void *) (((uintptr_t) &mutex
->__data
.__list
.__next
)
190 oldval
= mutex
->__data
.__lock
;
192 /* Check whether we already hold the mutex. */
193 if (__builtin_expect ((oldval
& FUTEX_TID_MASK
) == id
, 0))
195 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
197 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
201 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
203 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
205 /* Just bump the counter. */
206 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
207 /* Overflow of the counter. */
210 ++mutex
->__data
.__count
;
217 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
222 if ((oldval
& FUTEX_OWNER_DIED
) == 0)
224 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
231 /* The mutex owner died. The kernel will now take care of
233 INTERNAL_SYSCALL_DECL (__err
);
234 int e
= INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
235 FUTEX_TRYLOCK_PI
, 0, 0);
237 if (INTERNAL_SYSCALL_ERROR_P (e
, __err
)
238 && INTERNAL_SYSCALL_ERRNO (e
, __err
) == EWOULDBLOCK
)
240 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
245 oldval
= mutex
->__data
.__lock
;
248 if (__builtin_expect (oldval
& FUTEX_OWNER_DIED
, 0))
250 atomic_and (&mutex
->__data
.__lock
, ~FUTEX_OWNER_DIED
);
252 /* We got the mutex. */
253 mutex
->__data
.__count
= 1;
254 /* But it is inconsistent unless marked otherwise. */
255 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
257 ENQUEUE_MUTEX (mutex
);
258 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
260 /* Note that we deliberately exit here. If we fall
261 through to the end of the function __nusers would be
262 incremented which is not correct because the old owner
263 has to be discounted. */
268 && __builtin_expect (mutex
->__data
.__owner
269 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
271 /* This mutex is now not recoverable. */
272 mutex
->__data
.__count
= 0;
274 INTERNAL_SYSCALL_DECL (__err
);
275 INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
276 FUTEX_UNLOCK_PI
, 0, 0);
278 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
279 return ENOTRECOVERABLE
;
284 ENQUEUE_MUTEX_PI (mutex
);
285 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
288 mutex
->__data
.__owner
= id
;
289 ++mutex
->__data
.__nusers
;
290 mutex
->__data
.__count
= 1;
295 case PTHREAD_MUTEX_PP_RECURSIVE_NP
:
296 case PTHREAD_MUTEX_PP_ERRORCHECK_NP
:
297 case PTHREAD_MUTEX_PP_NORMAL_NP
:
298 case PTHREAD_MUTEX_PP_ADAPTIVE_NP
:
300 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
302 oldval
= mutex
->__data
.__lock
;
304 /* Check whether we already hold the mutex. */
305 if (mutex
->__data
.__owner
== id
)
307 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
310 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
312 /* Just bump the counter. */
313 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
314 /* Overflow of the counter. */
317 ++mutex
->__data
.__count
;
323 int oldprio
= -1, ceilval
;
326 int ceiling
= (oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
)
327 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
329 if (__pthread_current_priority () > ceiling
)
332 __pthread_tpp_change_priority (oldprio
, -1);
336 int retval
= __pthread_tpp_change_priority (oldprio
, ceiling
);
340 ceilval
= ceiling
<< PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
344 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
345 ceilval
| 1, ceilval
);
347 if (oldval
== ceilval
)
350 while ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
);
352 if (oldval
!= ceilval
)
354 __pthread_tpp_change_priority (oldprio
, -1);
358 assert (mutex
->__data
.__owner
== 0);
359 /* Record the ownership. */
360 mutex
->__data
.__owner
= id
;
361 ++mutex
->__data
.__nusers
;
362 mutex
->__data
.__count
= 1;
369 /* Correct code cannot set any other type. */
375 strong_alias (__pthread_mutex_trylock
, pthread_mutex_trylock
)