[BZ #6724]
[glibc.git] / nptl / pthread_mutex_lock.c
blobed98dfc6c39e689b78ae438a1102847d964ee33c
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
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <not-cancel.h>
25 #include "pthreadP.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))
37 #endif
40 int
41 __pthread_mutex_lock (mutex)
42 pthread_mutex_t *mutex;
44 assert (sizeof (mutex->__size) >= sizeof (mutex->__data));
46 int oldval;
47 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
49 int retval = 0;
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. */
61 return EAGAIN;
63 ++mutex->__data.__count;
65 return 0;
68 /* We have to get the mutex. */
69 LLL_MUTEX_LOCK (mutex);
71 assert (mutex->__data.__owner == 0);
72 mutex->__data.__count = 1;
73 break;
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))
79 return EDEADLK;
81 /* FALLTHROUGH */
83 case PTHREAD_MUTEX_TIMED_NP:
84 simple:
85 /* Normal mutex. */
86 LLL_MUTEX_LOCK (mutex);
87 assert (mutex->__data.__owner == 0);
88 break;
90 case PTHREAD_MUTEX_ADAPTIVE_NP:
91 if (! __is_smp)
92 goto simple;
94 if (LLL_MUTEX_TRYLOCK (mutex) != 0)
96 int cnt = 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);
104 break;
107 #ifdef BUSY_WAIT_NOP
108 BUSY_WAIT_NOP;
109 #endif
111 while (LLL_MUTEX_TRYLOCK (mutex) != 0);
113 mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
115 assert (mutex->__data.__owner == 0);
116 break;
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;
128 again:
129 if ((oldval & FUTEX_OWNER_DIED) != 0)
131 /* The previous owner died. Try locking the mutex. */
132 int newval = id;
133 #ifdef NO_INCR
134 newval |= FUTEX_WAITERS;
135 #else
136 newval |= (oldval & FUTEX_WAITERS);
137 #endif
139 newval
140 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
141 newval, oldval);
143 if (newval != oldval)
145 oldval = newval;
146 goto again;
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
162 it here. */
163 #ifdef NO_INCR
164 --mutex->__data.__nusers;
165 #endif
167 return EOWNERDEAD;
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,
177 NULL);
178 return EDEADLK;
181 if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
183 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
184 NULL);
186 /* Just bump the counter. */
187 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
188 /* Overflow of the counter. */
189 return EAGAIN;
191 ++mutex->__data.__count;
193 return 0;
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);
215 break;
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;
229 if (robust)
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)
233 | 1));
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);
243 return EDEADLK;
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. */
253 return EAGAIN;
255 ++mutex->__data.__count;
257 return 0;
261 int newval = id;
262 #ifdef NO_INCR
263 newval |= FUTEX_WAITERS;
264 #endif
265 oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
266 newval, 0);
268 if (oldval != 0)
270 /* The mutex is locked. The kernel will now take care of
271 everything. */
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. */
288 while (1)
289 pause_not_cancel ();
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. */
314 #ifdef NO_INCR
315 --mutex->__data.__nusers;
316 #endif
318 return EOWNERDEAD;
321 if (robust
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;
337 if (robust)
339 ENQUEUE_MUTEX_PI (mutex);
340 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
343 break;
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)
358 return EDEADLK;
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. */
365 return EAGAIN;
367 ++mutex->__data.__count;
369 return 0;
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)
381 if (oldprio != -1)
382 __pthread_tpp_change_priority (oldprio, -1);
383 return EINVAL;
386 retval = __pthread_tpp_change_priority (oldprio, ceiling);
387 if (retval)
388 return retval;
390 ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
391 oldprio = ceiling;
393 oldval
394 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
395 #ifdef NO_INCR
396 ceilval | 2,
397 #else
398 ceilval | 1,
399 #endif
400 ceilval);
402 if (oldval == ceilval)
403 break;
407 oldval
408 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
409 ceilval | 2,
410 ceilval | 1);
412 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
413 break;
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)
421 != ceilval);
423 while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
425 assert (mutex->__data.__owner == 0);
426 mutex->__data.__count = 1;
428 break;
430 default:
431 /* Correct code cannot set any other type. */
432 return EINVAL;
435 /* Record the ownership. */
436 mutex->__data.__owner = id;
437 #ifndef NO_INCR
438 ++mutex->__data.__nusers;
439 #endif
441 return retval;
443 #ifndef __pthread_mutex_lock
444 strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
445 strong_alias (__pthread_mutex_lock, __pthread_mutex_lock_internal)
446 #endif