1 /* Copyright (C) 2002-2015 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 FORCE_ELISION(m, s)
34 __pthread_mutex_trylock (mutex
)
35 pthread_mutex_t
*mutex
;
38 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
40 switch (__builtin_expect (PTHREAD_MUTEX_TYPE_ELISION (mutex
),
41 PTHREAD_MUTEX_TIMED_NP
))
43 /* Recursive mutex. */
44 case PTHREAD_MUTEX_RECURSIVE_NP
|PTHREAD_MUTEX_ELISION_NP
:
45 case PTHREAD_MUTEX_RECURSIVE_NP
:
46 /* Check whether we already hold the mutex. */
47 if (mutex
->__data
.__owner
== id
)
49 /* Just bump the counter. */
50 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
51 /* Overflow of the counter. */
54 ++mutex
->__data
.__count
;
58 if (lll_trylock (mutex
->__data
.__lock
) == 0)
60 /* Record the ownership. */
61 mutex
->__data
.__owner
= id
;
62 mutex
->__data
.__count
= 1;
63 ++mutex
->__data
.__nusers
;
68 case PTHREAD_MUTEX_TIMED_ELISION_NP
:
69 elision
: __attribute__((unused
))
70 if (lll_trylock_elision (mutex
->__data
.__lock
,
71 mutex
->__data
.__elision
) != 0)
73 /* Don't record the ownership. */
76 case PTHREAD_MUTEX_TIMED_NP
:
77 FORCE_ELISION (mutex
, goto elision
);
79 case PTHREAD_MUTEX_ADAPTIVE_NP
:
80 case PTHREAD_MUTEX_ERRORCHECK_NP
:
81 if (lll_trylock (mutex
->__data
.__lock
) != 0)
84 /* Record the ownership. */
85 mutex
->__data
.__owner
= id
;
86 ++mutex
->__data
.__nusers
;
90 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
:
91 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
:
92 case PTHREAD_MUTEX_ROBUST_NORMAL_NP
:
93 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP
:
94 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
95 &mutex
->__data
.__list
.__next
);
97 oldval
= mutex
->__data
.__lock
;
101 if ((oldval
& FUTEX_OWNER_DIED
) != 0)
103 /* The previous owner died. Try locking the mutex. */
104 int newval
= id
| (oldval
& FUTEX_WAITERS
);
107 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
110 if (newval
!= oldval
)
116 /* We got the mutex. */
117 mutex
->__data
.__count
= 1;
118 /* But it is inconsistent unless marked otherwise. */
119 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
121 ENQUEUE_MUTEX (mutex
);
122 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
124 /* Note that we deliberately exist here. If we fall
125 through to the end of the function __nusers would be
126 incremented which is not correct because the old
127 owner has to be discounted. */
131 /* Check whether we already hold the mutex. */
132 if (__glibc_unlikely ((oldval
& FUTEX_TID_MASK
) == id
))
134 int kind
= PTHREAD_MUTEX_TYPE (mutex
);
135 if (kind
== PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
)
137 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
142 if (kind
== PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
)
144 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
147 /* Just bump the counter. */
148 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
149 /* Overflow of the counter. */
152 ++mutex
->__data
.__count
;
158 oldval
= atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
160 if (oldval
!= 0 && (oldval
& FUTEX_OWNER_DIED
) == 0)
162 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
167 if (__builtin_expect (mutex
->__data
.__owner
168 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
170 /* This mutex is now not recoverable. */
171 mutex
->__data
.__count
= 0;
173 lll_unlock (mutex
->__data
.__lock
,
174 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
));
175 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
176 return ENOTRECOVERABLE
;
179 while ((oldval
& FUTEX_OWNER_DIED
) != 0);
181 ENQUEUE_MUTEX (mutex
);
182 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
184 mutex
->__data
.__owner
= id
;
185 ++mutex
->__data
.__nusers
;
186 mutex
->__data
.__count
= 1;
190 /* The PI support requires the Linux futex system call. If that's not
191 available, pthread_mutex_init should never have allowed the type to
192 be set. So it will get the default case for an invalid type. */
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;
322 #endif /* __NR_futex. */
324 case PTHREAD_MUTEX_PP_RECURSIVE_NP
:
325 case PTHREAD_MUTEX_PP_ERRORCHECK_NP
:
326 case PTHREAD_MUTEX_PP_NORMAL_NP
:
327 case PTHREAD_MUTEX_PP_ADAPTIVE_NP
:
329 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
331 oldval
= mutex
->__data
.__lock
;
333 /* Check whether we already hold the mutex. */
334 if (mutex
->__data
.__owner
== id
)
336 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
339 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
341 /* Just bump the counter. */
342 if (__glibc_unlikely (mutex
->__data
.__count
+ 1 == 0))
343 /* Overflow of the counter. */
346 ++mutex
->__data
.__count
;
352 int oldprio
= -1, ceilval
;
355 int ceiling
= (oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
)
356 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
358 if (__pthread_current_priority () > ceiling
)
361 __pthread_tpp_change_priority (oldprio
, -1);
365 int retval
= __pthread_tpp_change_priority (oldprio
, ceiling
);
369 ceilval
= ceiling
<< PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
373 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
374 ceilval
| 1, ceilval
);
376 if (oldval
== ceilval
)
379 while ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
);
381 if (oldval
!= ceilval
)
383 __pthread_tpp_change_priority (oldprio
, -1);
387 assert (mutex
->__data
.__owner
== 0);
388 /* Record the ownership. */
389 mutex
->__data
.__owner
= id
;
390 ++mutex
->__data
.__nusers
;
391 mutex
->__data
.__count
= 1;
398 /* Correct code cannot set any other type. */
405 #ifndef __pthread_mutex_trylock
406 #ifndef pthread_mutex_trylock
407 strong_alias (__pthread_mutex_trylock
, pthread_mutex_trylock
)