1 /* Copyright (C) 2002-2013 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, see
17 <http://www.gnu.org/licenses/>. */
23 #include <lowlevellock.h>
27 __pthread_mutex_trylock (pthread_mutex_t
*mutex
)
30 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
32 switch (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex
),
33 PTHREAD_MUTEX_TIMED_NP
))
35 /* Recursive mutex. */
36 case PTHREAD_MUTEX_RECURSIVE_NP
:
37 /* Check whether we already hold the mutex. */
38 if (mutex
->__data
.__owner
== id
)
40 /* Just bump the counter. */
41 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
42 /* Overflow of the counter. */
45 ++mutex
->__data
.__count
;
49 if (lll_trylock (mutex
->__data
.__lock
) == 0)
51 /* Record the ownership. */
52 mutex
->__data
.__owner
= id
;
53 mutex
->__data
.__count
= 1;
54 ++mutex
->__data
.__nusers
;
59 case PTHREAD_MUTEX_ERRORCHECK_NP
:
60 case PTHREAD_MUTEX_TIMED_NP
:
61 case PTHREAD_MUTEX_ADAPTIVE_NP
:
63 if (lll_trylock (mutex
->__data
.__lock
) != 0)
66 /* Record the ownership. */
67 mutex
->__data
.__owner
= id
;
68 ++mutex
->__data
.__nusers
;
72 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
:
73 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
:
74 case PTHREAD_MUTEX_ROBUST_NORMAL_NP
:
75 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP
:
76 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
77 &mutex
->__data
.__list
.__next
);
79 oldval
= mutex
->__data
.__lock
;
83 if ((oldval
& FUTEX_OWNER_DIED
) != 0)
85 /* The previous owner died. Try locking the mutex. */
86 int newval
= id
| (oldval
& FUTEX_WAITERS
);
89 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
98 /* We got the mutex. */
99 mutex
->__data
.__count
= 1;
100 /* But it is inconsistent unless marked otherwise. */
101 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
103 ENQUEUE_MUTEX (mutex
);
104 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
106 /* Note that we deliberately exist here. If we fall
107 through to the end of the function __nusers would be
108 incremented which is not correct because the old
109 owner has to be discounted. */
113 /* Check whether we already hold the mutex. */
114 if (__glibc_unlikely ((oldval
& FUTEX_TID_MASK
) == id
))
116 int kind
= PTHREAD_MUTEX_TYPE (mutex
);
117 if (kind
== PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
)
119 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
124 if (kind
== PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
)
126 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
129 /* Just bump the counter. */
130 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
131 /* Overflow of the counter. */
134 ++mutex
->__data
.__count
;
140 oldval
= lll_robust_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_unlock (mutex
->__data
.__lock
,
155 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
));
156 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
157 return ENOTRECOVERABLE
;
160 while ((oldval
& FUTEX_OWNER_DIED
) != 0);
162 ENQUEUE_MUTEX (mutex
);
163 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
165 mutex
->__data
.__owner
= id
;
166 ++mutex
->__data
.__nusers
;
167 mutex
->__data
.__count
= 1;
171 case PTHREAD_MUTEX_PI_RECURSIVE_NP
:
172 case PTHREAD_MUTEX_PI_ERRORCHECK_NP
:
173 case PTHREAD_MUTEX_PI_NORMAL_NP
:
174 case PTHREAD_MUTEX_PI_ADAPTIVE_NP
:
175 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP
:
176 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP
:
177 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP
:
178 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP
:
180 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
181 int robust
= mutex
->__data
.__kind
& PTHREAD_MUTEX_ROBUST_NORMAL_NP
;
184 /* Note: robust PI futexes are signaled by setting bit 0. */
185 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
186 (void *) (((uintptr_t) &mutex
->__data
.__list
.__next
)
189 oldval
= mutex
->__data
.__lock
;
191 /* Check whether we already hold the mutex. */
192 if (__glibc_unlikely ((oldval
& FUTEX_TID_MASK
) == id
))
194 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
196 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
200 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
202 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
204 /* Just bump the counter. */
205 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
206 /* Overflow of the counter. */
209 ++mutex
->__data
.__count
;
216 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
221 if ((oldval
& FUTEX_OWNER_DIED
) == 0)
223 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
230 /* The mutex owner died. The kernel will now take care of
232 int private = (robust
233 ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex
)
234 : PTHREAD_MUTEX_PSHARED (mutex
));
235 INTERNAL_SYSCALL_DECL (__err
);
236 int e
= INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
237 __lll_private_flag (FUTEX_TRYLOCK_PI
,
240 if (INTERNAL_SYSCALL_ERROR_P (e
, __err
)
241 && INTERNAL_SYSCALL_ERRNO (e
, __err
) == EWOULDBLOCK
)
243 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
248 oldval
= mutex
->__data
.__lock
;
251 if (__glibc_unlikely (oldval
& FUTEX_OWNER_DIED
))
253 atomic_and (&mutex
->__data
.__lock
, ~FUTEX_OWNER_DIED
);
255 /* We got the mutex. */
256 mutex
->__data
.__count
= 1;
257 /* But it is inconsistent unless marked otherwise. */
258 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
260 ENQUEUE_MUTEX (mutex
);
261 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
263 /* Note that we deliberately exit here. If we fall
264 through to the end of the function __nusers would be
265 incremented which is not correct because the old owner
266 has to be discounted. */
271 && __builtin_expect (mutex
->__data
.__owner
272 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
274 /* This mutex is now not recoverable. */
275 mutex
->__data
.__count
= 0;
277 INTERNAL_SYSCALL_DECL (__err
);
278 INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
279 __lll_private_flag (FUTEX_UNLOCK_PI
,
280 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
)),
283 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
284 return ENOTRECOVERABLE
;
289 ENQUEUE_MUTEX_PI (mutex
);
290 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
293 mutex
->__data
.__owner
= id
;
294 ++mutex
->__data
.__nusers
;
295 mutex
->__data
.__count
= 1;
300 case PTHREAD_MUTEX_PP_RECURSIVE_NP
:
301 case PTHREAD_MUTEX_PP_ERRORCHECK_NP
:
302 case PTHREAD_MUTEX_PP_NORMAL_NP
:
303 case PTHREAD_MUTEX_PP_ADAPTIVE_NP
:
305 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
307 oldval
= mutex
->__data
.__lock
;
309 /* Check whether we already hold the mutex. */
310 if (mutex
->__data
.__owner
== id
)
312 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
315 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
317 /* Just bump the counter. */
318 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
319 /* Overflow of the counter. */
322 ++mutex
->__data
.__count
;
328 int oldprio
= -1, ceilval
;
331 int ceiling
= (oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
)
332 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
334 if (__pthread_current_priority () > ceiling
)
337 __pthread_tpp_change_priority (oldprio
, -1);
341 int retval
= __pthread_tpp_change_priority (oldprio
, ceiling
);
345 ceilval
= ceiling
<< PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
349 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
350 ceilval
| 1, ceilval
);
352 if (oldval
== ceilval
)
355 while ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
);
357 if (oldval
!= ceilval
)
359 __pthread_tpp_change_priority (oldprio
, -1);
363 assert (mutex
->__data
.__owner
== 0);
364 /* Record the ownership. */
365 mutex
->__data
.__owner
= id
;
366 ++mutex
->__data
.__nusers
;
367 mutex
->__data
.__count
= 1;
374 /* Correct code cannot set any other type. */
380 strong_alias (__pthread_mutex_trylock
, pthread_mutex_trylock
)