1 /* Copyright (C) 2002,2003,2004,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 <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 INTERNAL_SYSCALL_DECL (__err
);
273 int e
= INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
274 FUTEX_LOCK_PI
, 1, 0);
276 if (INTERNAL_SYSCALL_ERROR_P (e
, __err
)
277 && (INTERNAL_SYSCALL_ERRNO (e
, __err
) == ESRCH
278 || INTERNAL_SYSCALL_ERRNO (e
, __err
) == EDEADLK
))
280 assert (INTERNAL_SYSCALL_ERRNO (e
, __err
) != EDEADLK
281 || (kind
!= PTHREAD_MUTEX_ERRORCHECK_NP
282 && kind
!= PTHREAD_MUTEX_RECURSIVE_NP
));
283 /* ESRCH can happen only for non-robust PI mutexes where
284 the owner of the lock died. */
285 assert (INTERNAL_SYSCALL_ERRNO (e
, __err
) != ESRCH
|| !robust
);
287 /* Delay the thread indefinitely. */
292 oldval
= mutex
->__data
.__lock
;
294 assert (robust
|| (oldval
& FUTEX_OWNER_DIED
) == 0);
297 if (__builtin_expect (oldval
& FUTEX_OWNER_DIED
, 0))
299 atomic_and (&mutex
->__data
.__lock
, ~FUTEX_OWNER_DIED
);
301 /* We got the mutex. */
302 mutex
->__data
.__count
= 1;
303 /* But it is inconsistent unless marked otherwise. */
304 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
306 ENQUEUE_MUTEX_PI (mutex
);
307 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
309 /* Note that we deliberately exit here. If we fall
310 through to the end of the function __nusers would be
311 incremented which is not correct because the old owner
312 has to be discounted. If we are not supposed to
313 increment __nusers we actually have to decrement it here. */
315 --mutex
->__data
.__nusers
;
322 && __builtin_expect (mutex
->__data
.__owner
323 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
325 /* This mutex is now not recoverable. */
326 mutex
->__data
.__count
= 0;
328 INTERNAL_SYSCALL_DECL (__err
);
329 INTERNAL_SYSCALL (futex
, __err
, 4, &mutex
->__data
.__lock
,
330 FUTEX_UNLOCK_PI
, 0, 0);
332 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
333 return ENOTRECOVERABLE
;
336 mutex
->__data
.__count
= 1;
339 ENQUEUE_MUTEX_PI (mutex
);
340 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
345 case PTHREAD_MUTEX_PP_RECURSIVE_NP
:
346 case PTHREAD_MUTEX_PP_ERRORCHECK_NP
:
347 case PTHREAD_MUTEX_PP_NORMAL_NP
:
348 case PTHREAD_MUTEX_PP_ADAPTIVE_NP
:
350 int kind
= mutex
->__data
.__kind
& PTHREAD_MUTEX_KIND_MASK_NP
;
352 oldval
= mutex
->__data
.__lock
;
354 /* Check whether we already hold the mutex. */
355 if (mutex
->__data
.__owner
== id
)
357 if (kind
== PTHREAD_MUTEX_ERRORCHECK_NP
)
360 if (kind
== PTHREAD_MUTEX_RECURSIVE_NP
)
362 /* Just bump the counter. */
363 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
364 /* Overflow of the counter. */
367 ++mutex
->__data
.__count
;
373 int oldprio
= -1, ceilval
;
376 int ceiling
= (oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
)
377 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
379 if (__pthread_current_priority () > ceiling
)
382 __pthread_tpp_change_priority (oldprio
, -1);
386 retval
= __pthread_tpp_change_priority (oldprio
, ceiling
);
390 ceilval
= ceiling
<< PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
394 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
402 if (oldval
== ceilval
)
408 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
412 if ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
)
415 if (oldval
!= ceilval
)
416 lll_futex_wait (&mutex
->__data
.__lock
, ceilval
| 2,
417 PTHREAD_MUTEX_PSHARED (mutex
));
419 while (atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
420 ceilval
| 2, ceilval
)
423 while ((oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
) != ceilval
);
425 assert (mutex
->__data
.__owner
== 0);
426 mutex
->__data
.__count
= 1;
431 /* Correct code cannot set any other type. */
435 /* Record the ownership. */
436 mutex
->__data
.__owner
= id
;
438 ++mutex
->__data
.__nusers
;
443 #ifndef __pthread_mutex_lock
444 strong_alias (__pthread_mutex_lock
, pthread_mutex_lock
)
445 strong_alias (__pthread_mutex_lock
, __pthread_mutex_lock_internal
)