1 /* Copyright (C) 2002-2014 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>
25 #ifndef lll_trylock_elision
26 #define lll_trylock_elision(a,t) lll_trylock(a)
30 #define DO_ELISION(m) 0
33 /* We don't force elision in trylock, because this can lead to inconsistent
34 lock state if the lock was actually busy. */
37 __pthread_mutex_trylock (mutex
)
38 pthread_mutex_t
*mutex
;
41 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
43 switch (__builtin_expect (PTHREAD_MUTEX_TYPE_ELISION (mutex
),
44 PTHREAD_MUTEX_TIMED_NP
))
46 /* Recursive mutex. */
47 case PTHREAD_MUTEX_RECURSIVE_NP
|PTHREAD_MUTEX_ELISION_NP
:
48 case PTHREAD_MUTEX_RECURSIVE_NP
:
49 /* Check whether we already hold the mutex. */
50 if (mutex
->__data
.__owner
== id
)
52 /* Just bump the counter. */
53 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
54 /* Overflow of the counter. */
57 ++mutex
->__data
.__count
;
61 if (lll_trylock (mutex
->__data
.__lock
) == 0)
63 /* Record the ownership. */
64 mutex
->__data
.__owner
= id
;
65 mutex
->__data
.__count
= 1;
66 ++mutex
->__data
.__nusers
;
71 case PTHREAD_MUTEX_TIMED_ELISION_NP
:
73 if (lll_trylock_elision (mutex
->__data
.__lock
,
74 mutex
->__data
.__elision
) != 0)
76 /* Don't record the ownership. */
79 case PTHREAD_MUTEX_TIMED_NP
:
80 if (DO_ELISION (mutex
))
83 case PTHREAD_MUTEX_ADAPTIVE_NP
:
84 case PTHREAD_MUTEX_ERRORCHECK_NP
:
85 if (lll_trylock (mutex
->__data
.__lock
) != 0)
88 /* Record the ownership. */
89 mutex
->__data
.__owner
= id
;
90 ++mutex
->__data
.__nusers
;
94 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
:
95 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
:
96 case PTHREAD_MUTEX_ROBUST_NORMAL_NP
:
97 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP
:
98 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
99 &mutex
->__data
.__list
.__next
);
101 oldval
= mutex
->__data
.__lock
;
105 if ((oldval
& FUTEX_OWNER_DIED
) != 0)
107 /* The previous owner died. Try locking the mutex. */
108 int newval
= id
| (oldval
& FUTEX_WAITERS
);
111 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
114 if (newval
!= oldval
)
120 /* We got the mutex. */
121 mutex
->__data
.__count
= 1;
122 /* But it is inconsistent unless marked otherwise. */
123 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
125 ENQUEUE_MUTEX (mutex
);
126 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
128 /* Note that we deliberately exist here. If we fall
129 through to the end of the function __nusers would be
130 incremented which is not correct because the old
131 owner has to be discounted. */
135 /* Check whether we already hold the mutex. */
136 if (__glibc_unlikely ((oldval
& FUTEX_TID_MASK
) == id
))
138 int kind
= PTHREAD_MUTEX_TYPE (mutex
);
139 if (kind
== PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
)
141 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
146 if (kind
== PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
)
148 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
151 /* Just bump the counter. */
152 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
153 /* Overflow of the counter. */
156 ++mutex
->__data
.__count
;
162 oldval
= atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
164 if (oldval
!= 0 && (oldval
& FUTEX_OWNER_DIED
) == 0)
166 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
171 if (__builtin_expect (mutex
->__data
.__owner
172 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
174 /* This mutex is now not recoverable. */
175 mutex
->__data
.__count
= 0;
177 lll_unlock (mutex
->__data
.__lock
,
178 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
));
179 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
180 return ENOTRECOVERABLE
;
183 while ((oldval
& FUTEX_OWNER_DIED
) != 0);
185 ENQUEUE_MUTEX (mutex
);
186 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
188 mutex
->__data
.__owner
= id
;
189 ++mutex
->__data
.__nusers
;
190 mutex
->__data
.__count
= 1;
194 case PTHREAD_MUTEX_PI_RECURSIVE_NP
:
195 case PTHREAD_MUTEX_PI_ERRORCHECK_NP
:
196 case PTHREAD_MUTEX_PI_NORMAL_NP
:
197 case PTHREAD_MUTEX_PI_ADAPTIVE_NP
:
198 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP
:
199 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP
:
200 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP
:
201 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP
:
203 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
204 int robust
= mutex
->__data
.__kind
& PTHREAD_MUTEX_ROBUST_NORMAL_NP
;
207 /* Note: robust PI futexes are signaled by setting bit 0. */
208 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
209 (void *) (((uintptr_t) &mutex
->__data
.__list
.__next
)
212 oldval
= mutex
->__data
.__lock
;
214 /* Check whether we already hold the mutex. */
215 if (__glibc_unlikely ((oldval
& FUTEX_TID_MASK
) == id
))
217 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
219 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
223 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
225 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
227 /* Just bump the counter. */
228 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
229 /* Overflow of the counter. */
232 ++mutex
->__data
.__count
;
239 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
244 if ((oldval
& FUTEX_OWNER_DIED
) == 0)
246 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
253 /* The mutex owner died. The kernel will now take care of
255 int private = (robust
256 ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex
)
257 : PTHREAD_MUTEX_PSHARED (mutex
));
258 INTERNAL_SYSCALL_DECL (__err
);
259 int e
= INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
260 __lll_private_flag (FUTEX_TRYLOCK_PI
,
263 if (INTERNAL_SYSCALL_ERROR_P (e
, __err
)
264 && INTERNAL_SYSCALL_ERRNO (e
, __err
) == EWOULDBLOCK
)
266 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
271 oldval
= mutex
->__data
.__lock
;
274 if (__glibc_unlikely (oldval
& FUTEX_OWNER_DIED
))
276 atomic_and (&mutex
->__data
.__lock
, ~FUTEX_OWNER_DIED
);
278 /* We got the mutex. */
279 mutex
->__data
.__count
= 1;
280 /* But it is inconsistent unless marked otherwise. */
281 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
283 ENQUEUE_MUTEX (mutex
);
284 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
286 /* Note that we deliberately exit here. If we fall
287 through to the end of the function __nusers would be
288 incremented which is not correct because the old owner
289 has to be discounted. */
294 && __builtin_expect (mutex
->__data
.__owner
295 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
297 /* This mutex is now not recoverable. */
298 mutex
->__data
.__count
= 0;
300 INTERNAL_SYSCALL_DECL (__err
);
301 INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
302 __lll_private_flag (FUTEX_UNLOCK_PI
,
303 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
)),
306 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
307 return ENOTRECOVERABLE
;
312 ENQUEUE_MUTEX_PI (mutex
);
313 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
316 mutex
->__data
.__owner
= id
;
317 ++mutex
->__data
.__nusers
;
318 mutex
->__data
.__count
= 1;
323 case PTHREAD_MUTEX_PP_RECURSIVE_NP
:
324 case PTHREAD_MUTEX_PP_ERRORCHECK_NP
:
325 case PTHREAD_MUTEX_PP_NORMAL_NP
:
326 case PTHREAD_MUTEX_PP_ADAPTIVE_NP
:
328 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
330 oldval
= mutex
->__data
.__lock
;
332 /* Check whether we already hold the mutex. */
333 if (mutex
->__data
.__owner
== id
)
335 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
338 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
340 /* Just bump the counter. */
341 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
342 /* Overflow of the counter. */
345 ++mutex
->__data
.__count
;
351 int oldprio
= -1, ceilval
;
354 int ceiling
= (oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
)
355 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
357 if (__pthread_current_priority () > ceiling
)
360 __pthread_tpp_change_priority (oldprio
, -1);
364 int retval
= __pthread_tpp_change_priority (oldprio
, ceiling
);
368 ceilval
= ceiling
<< PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
372 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
373 ceilval
| 1, ceilval
);
375 if (oldval
== ceilval
)
378 while ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
);
380 if (oldval
!= ceilval
)
382 __pthread_tpp_change_priority (oldprio
, -1);
386 assert (mutex
->__data
.__owner
== 0);
387 /* Record the ownership. */
388 mutex
->__data
.__owner
= id
;
389 ++mutex
->__data
.__nusers
;
390 mutex
->__data
.__count
= 1;
397 /* Correct code cannot set any other type. */
404 #ifndef __pthread_mutex_trylock
405 #ifndef pthread_mutex_trylock
406 strong_alias (__pthread_mutex_trylock
, pthread_mutex_trylock
)