1 /* Copyright (C) 2002-2007, 2008 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 <not-cancel.h>
26 #include <lowlevellock.h>
29 #ifndef LLL_MUTEX_LOCK
30 # define LLL_MUTEX_LOCK(mutex) \
31 lll_lock ((mutex)->__data.__lock, PTHREAD_MUTEX_PSHARED (mutex))
32 # define LLL_MUTEX_TRYLOCK(mutex) \
33 lll_trylock ((mutex)->__data.__lock)
34 # define LLL_ROBUST_MUTEX_LOCK(mutex, id) \
35 lll_robust_lock ((mutex)->__data.__lock, id, \
36 PTHREAD_ROBUST_MUTEX_PSHARED (mutex))
41 __pthread_mutex_lock (mutex
)
42 pthread_mutex_t
*mutex
;
44 assert (sizeof (mutex
->__size
) >= sizeof (mutex
->__data
));
47 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
50 switch (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex
),
51 PTHREAD_MUTEX_TIMED_NP
))
53 /* Recursive mutex. */
54 case PTHREAD_MUTEX_RECURSIVE_NP
:
55 /* Check whether we already hold the mutex. */
56 if (mutex
->__data
.__owner
== id
)
58 /* Just bump the counter. */
59 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
60 /* Overflow of the counter. */
63 ++mutex
->__data
.__count
;
68 /* We have to get the mutex. */
69 LLL_MUTEX_LOCK (mutex
);
71 assert (mutex
->__data
.__owner
== 0);
72 mutex
->__data
.__count
= 1;
75 /* Error checking mutex. */
76 case PTHREAD_MUTEX_ERRORCHECK_NP
:
77 /* Check whether we already hold the mutex. */
78 if (__builtin_expect (mutex
->__data
.__owner
== id
, 0))
83 case PTHREAD_MUTEX_TIMED_NP
:
86 LLL_MUTEX_LOCK (mutex
);
87 assert (mutex
->__data
.__owner
== 0);
90 case PTHREAD_MUTEX_ADAPTIVE_NP
:
94 if (LLL_MUTEX_TRYLOCK (mutex
) != 0)
97 int max_cnt
= MIN (MAX_ADAPTIVE_COUNT
,
98 mutex
->__data
.__spins
* 2 + 10);
101 if (cnt
++ >= max_cnt
)
103 LLL_MUTEX_LOCK (mutex
);
111 while (LLL_MUTEX_TRYLOCK (mutex
) != 0);
113 mutex
->__data
.__spins
+= (cnt
- mutex
->__data
.__spins
) / 8;
115 assert (mutex
->__data
.__owner
== 0);
118 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
:
119 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
:
120 case PTHREAD_MUTEX_ROBUST_NORMAL_NP
:
121 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP
:
122 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
123 &mutex
->__data
.__list
.__next
);
125 oldval
= mutex
->__data
.__lock
;
129 if ((oldval
& FUTEX_OWNER_DIED
) != 0)
131 /* The previous owner died. Try locking the mutex. */
134 newval
|= FUTEX_WAITERS
;
136 newval
|= (oldval
& FUTEX_WAITERS
);
140 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
143 if (newval
!= oldval
)
149 /* We got the mutex. */
150 mutex
->__data
.__count
= 1;
151 /* But it is inconsistent unless marked otherwise. */
152 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
154 ENQUEUE_MUTEX (mutex
);
155 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
157 /* Note that we deliberately exit here. If we fall
158 through to the end of the function __nusers would be
159 incremented which is not correct because the old
160 owner has to be discounted. If we are not supposed
161 to increment __nusers we actually have to decrement
164 --mutex
->__data
.__nusers
;
170 /* Check whether we already hold the mutex. */
171 if (__builtin_expect ((oldval
& FUTEX_TID_MASK
) == id
, 0))
173 int kind
= PTHREAD_MUTEX_TYPE (mutex
);
174 if (kind
== PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
)
176 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
181 if (kind
== PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
)
183 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
186 /* Just bump the counter. */
187 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
188 /* Overflow of the counter. */
191 ++mutex
->__data
.__count
;
197 oldval
= LLL_ROBUST_MUTEX_LOCK (mutex
, id
);
199 if (__builtin_expect (mutex
->__data
.__owner
200 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
202 /* This mutex is now not recoverable. */
203 mutex
->__data
.__count
= 0;
204 lll_unlock (mutex
->__data
.__lock
,
205 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
));
206 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
207 return ENOTRECOVERABLE
;
210 while ((oldval
& FUTEX_OWNER_DIED
) != 0);
212 mutex
->__data
.__count
= 1;
213 ENQUEUE_MUTEX (mutex
);
214 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
217 case PTHREAD_MUTEX_PI_RECURSIVE_NP
:
218 case PTHREAD_MUTEX_PI_ERRORCHECK_NP
:
219 case PTHREAD_MUTEX_PI_NORMAL_NP
:
220 case PTHREAD_MUTEX_PI_ADAPTIVE_NP
:
221 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP
:
222 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP
:
223 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP
:
224 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP
:
226 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
227 int robust
= mutex
->__data
.__kind
& PTHREAD_MUTEX_ROBUST_NORMAL_NP
;
230 /* Note: robust PI futexes are signaled by setting bit 0. */
231 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
232 (void *) (((uintptr_t) &mutex
->__data
.__list
.__next
)
235 oldval
= mutex
->__data
.__lock
;
237 /* Check whether we already hold the mutex. */
238 if (__builtin_expect ((oldval
& FUTEX_TID_MASK
) == id
, 0))
240 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
242 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
246 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
248 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
250 /* Just bump the counter. */
251 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
252 /* Overflow of the counter. */
255 ++mutex
->__data
.__count
;
263 newval
|= FUTEX_WAITERS
;
265 oldval
= atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
270 /* The mutex is locked. The kernel will now take care of
272 int private = (robust
273 ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex
)
274 : PTHREAD_MUTEX_PSHARED (mutex
));
275 INTERNAL_SYSCALL_DECL (__err
);
276 int e
= INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
277 __lll_private_flag (FUTEX_LOCK_PI
,
280 if (INTERNAL_SYSCALL_ERROR_P (e
, __err
)
281 && (INTERNAL_SYSCALL_ERRNO (e
, __err
) == ESRCH
282 || INTERNAL_SYSCALL_ERRNO (e
, __err
) == EDEADLK
))
284 assert (INTERNAL_SYSCALL_ERRNO (e
, __err
) != EDEADLK
285 || (kind
!= PTHREAD_MUTEX_ERRORCHECK_NP
286 && kind
!= PTHREAD_MUTEX_RECURSIVE_NP
));
287 /* ESRCH can happen only for non-robust PI mutexes where
288 the owner of the lock died. */
289 assert (INTERNAL_SYSCALL_ERRNO (e
, __err
) != ESRCH
|| !robust
);
291 /* Delay the thread indefinitely. */
296 oldval
= mutex
->__data
.__lock
;
298 assert (robust
|| (oldval
& FUTEX_OWNER_DIED
) == 0);
301 if (__builtin_expect (oldval
& FUTEX_OWNER_DIED
, 0))
303 atomic_and (&mutex
->__data
.__lock
, ~FUTEX_OWNER_DIED
);
305 /* We got the mutex. */
306 mutex
->__data
.__count
= 1;
307 /* But it is inconsistent unless marked otherwise. */
308 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
310 ENQUEUE_MUTEX_PI (mutex
);
311 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
313 /* Note that we deliberately exit here. If we fall
314 through to the end of the function __nusers would be
315 incremented which is not correct because the old owner
316 has to be discounted. If we are not supposed to
317 increment __nusers we actually have to decrement it here. */
319 --mutex
->__data
.__nusers
;
326 && __builtin_expect (mutex
->__data
.__owner
327 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
329 /* This mutex is now not recoverable. */
330 mutex
->__data
.__count
= 0;
332 INTERNAL_SYSCALL_DECL (__err
);
333 INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
334 __lll_private_flag (FUTEX_UNLOCK_PI
,
335 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
)
339 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
340 return ENOTRECOVERABLE
;
343 mutex
->__data
.__count
= 1;
346 ENQUEUE_MUTEX_PI (mutex
);
347 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
352 case PTHREAD_MUTEX_PP_RECURSIVE_NP
:
353 case PTHREAD_MUTEX_PP_ERRORCHECK_NP
:
354 case PTHREAD_MUTEX_PP_NORMAL_NP
:
355 case PTHREAD_MUTEX_PP_ADAPTIVE_NP
:
357 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
359 oldval
= mutex
->__data
.__lock
;
361 /* Check whether we already hold the mutex. */
362 if (mutex
->__data
.__owner
== id
)
364 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
367 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
369 /* Just bump the counter. */
370 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
371 /* Overflow of the counter. */
374 ++mutex
->__data
.__count
;
380 int oldprio
= -1, ceilval
;
383 int ceiling
= (oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
)
384 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
386 if (__pthread_current_priority () > ceiling
)
389 __pthread_tpp_change_priority (oldprio
, -1);
393 retval
= __pthread_tpp_change_priority (oldprio
, ceiling
);
397 ceilval
= ceiling
<< PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
401 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
409 if (oldval
== ceilval
)
415 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
419 if ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
)
422 if (oldval
!= ceilval
)
423 lll_futex_wait (&mutex
->__data
.__lock
, ceilval
| 2,
424 PTHREAD_MUTEX_PSHARED (mutex
));
426 while (atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
427 ceilval
| 2, ceilval
)
430 while ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
);
432 assert (mutex
->__data
.__owner
== 0);
433 mutex
->__data
.__count
= 1;
438 /* Correct code cannot set any other type. */
442 /* Record the ownership. */
443 mutex
->__data
.__owner
= id
;
445 ++mutex
->__data
.__nusers
;
450 #ifndef __pthread_mutex_lock
451 strong_alias (__pthread_mutex_lock
, pthread_mutex_lock
)
452 strong_alias (__pthread_mutex_lock
, __pthread_mutex_lock_internal
)