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. */
88 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
97 /* We got the mutex. */
98 mutex
->__data
.__count
= 1;
99 /* But it is inconsistent unless marked otherwise. */
100 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
102 ENQUEUE_MUTEX (mutex
);
103 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
105 /* Note that we deliberately exist here. If we fall
106 through to the end of the function __nusers would be
107 incremented which is not correct because the old
108 owner has to be discounted. */
112 /* Check whether we already hold the mutex. */
113 if (__builtin_expect ((oldval
& FUTEX_TID_MASK
) == id
, 0))
115 if (mutex
->__data
.__kind
116 == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
)
118 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
123 if (mutex
->__data
.__kind
124 == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
)
126 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
129 /* Just bump the counter. */
130 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
131 /* Overflow of the counter. */
134 ++mutex
->__data
.__count
;
140 oldval
= lll_robust_mutex_trylock (mutex
->__data
.__lock
, id
);
141 if (oldval
!= 0 && (oldval
& FUTEX_OWNER_DIED
) == 0)
143 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
148 if (__builtin_expect (mutex
->__data
.__owner
149 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
151 /* This mutex is now not recoverable. */
152 mutex
->__data
.__count
= 0;
154 lll_mutex_unlock (mutex
->__data
.__lock
);
155 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
156 return ENOTRECOVERABLE
;
159 while ((oldval
& FUTEX_OWNER_DIED
) != 0);
161 ENQUEUE_MUTEX (mutex
);
162 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
164 mutex
->__data
.__owner
= id
;
165 ++mutex
->__data
.__nusers
;
166 mutex
->__data
.__count
= 1;
170 case PTHREAD_MUTEX_PI_RECURSIVE_NP
:
171 case PTHREAD_MUTEX_PI_ERRORCHECK_NP
:
172 case PTHREAD_MUTEX_PI_NORMAL_NP
:
173 case PTHREAD_MUTEX_PI_ADAPTIVE_NP
:
174 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP
:
175 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP
:
176 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP
:
177 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP
:
179 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
180 int robust
= mutex
->__data
.__kind
& PTHREAD_MUTEX_ROBUST_NORMAL_NP
;
183 /* Note: robust PI futexes are signaled by setting bit 0. */
184 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
185 (void *) (((uintptr_t) &mutex
->__data
.__list
.__next
)
188 oldval
= mutex
->__data
.__lock
;
190 /* Check whether we already hold the mutex. */
191 if (__builtin_expect ((oldval
& FUTEX_TID_MASK
) == id
, 0))
193 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
195 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
199 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
201 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
203 /* Just bump the counter. */
204 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
205 /* Overflow of the counter. */
208 ++mutex
->__data
.__count
;
215 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
220 if ((oldval
& FUTEX_OWNER_DIED
) == 0)
222 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
229 /* The mutex owner died. The kernel will now take care of
231 INTERNAL_SYSCALL_DECL (__err
);
232 int e
= INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
233 FUTEX_TRYLOCK_PI
, 0, 0);
235 if (INTERNAL_SYSCALL_ERROR_P (e
, __err
)
236 && INTERNAL_SYSCALL_ERRNO (e
, __err
) == EWOULDBLOCK
)
238 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
243 oldval
= mutex
->__data
.__lock
;
246 if (__builtin_expect (oldval
& FUTEX_OWNER_DIED
, 0))
248 atomic_and (&mutex
->__data
.__lock
, ~FUTEX_OWNER_DIED
);
250 /* We got the mutex. */
251 mutex
->__data
.__count
= 1;
252 /* But it is inconsistent unless marked otherwise. */
253 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
255 ENQUEUE_MUTEX (mutex
);
256 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
258 /* Note that we deliberately exit here. If we fall
259 through to the end of the function __nusers would be
260 incremented which is not correct because the old owner
261 has to be discounted. */
266 && __builtin_expect (mutex
->__data
.__owner
267 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
269 /* This mutex is now not recoverable. */
270 mutex
->__data
.__count
= 0;
272 INTERNAL_SYSCALL_DECL (__err
);
273 INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
274 FUTEX_UNLOCK_PI
, 0, 0);
276 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
277 return ENOTRECOVERABLE
;
282 ENQUEUE_MUTEX_PI (mutex
);
283 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
286 mutex
->__data
.__owner
= id
;
287 ++mutex
->__data
.__nusers
;
288 mutex
->__data
.__count
= 1;
293 case PTHREAD_MUTEX_PP_RECURSIVE_NP
:
294 case PTHREAD_MUTEX_PP_ERRORCHECK_NP
:
295 case PTHREAD_MUTEX_PP_NORMAL_NP
:
296 case PTHREAD_MUTEX_PP_ADAPTIVE_NP
:
298 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
300 oldval
= mutex
->__data
.__lock
;
302 /* Check whether we already hold the mutex. */
303 if (mutex
->__data
.__owner
== id
)
305 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
308 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
310 /* Just bump the counter. */
311 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
312 /* Overflow of the counter. */
315 ++mutex
->__data
.__count
;
321 int oldprio
= -1, ceilval
;
324 int ceiling
= (oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
)
325 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
327 if (__pthread_current_priority () > ceiling
)
330 __pthread_tpp_change_priority (oldprio
, -1);
334 int retval
= __pthread_tpp_change_priority (oldprio
, ceiling
);
338 ceilval
= ceiling
<< PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
342 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
343 ceilval
| 1, ceilval
);
345 if (oldval
== ceilval
)
348 while ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
);
350 if (oldval
!= ceilval
)
352 __pthread_tpp_change_priority (oldprio
, -1);
356 assert (mutex
->__data
.__owner
== 0);
357 /* Record the ownership. */
358 mutex
->__data
.__owner
= id
;
359 ++mutex
->__data
.__nusers
;
360 mutex
->__data
.__count
= 1;
367 /* Correct code cannot set any other type. */
373 strong_alias (__pthread_mutex_trylock
, pthread_mutex_trylock
)