2.9
[glibc/nacl-glibc.git] / nptl / pthread_mutex_lock.c
blob3eb5636955b8a35d8bc36bc3c39b463e811ed861
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
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 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,
278 private), 1, 0);
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. */
292 while (1)
293 pause_not_cancel ();
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. */
318 #ifdef NO_INCR
319 --mutex->__data.__nusers;
320 #endif
322 return EOWNERDEAD;
325 if (robust
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)
337 0, 0);
339 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
340 return ENOTRECOVERABLE;
343 mutex->__data.__count = 1;
344 if (robust)
346 ENQUEUE_MUTEX_PI (mutex);
347 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
350 break;
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)
365 return EDEADLK;
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. */
372 return EAGAIN;
374 ++mutex->__data.__count;
376 return 0;
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)
388 if (oldprio != -1)
389 __pthread_tpp_change_priority (oldprio, -1);
390 return EINVAL;
393 retval = __pthread_tpp_change_priority (oldprio, ceiling);
394 if (retval)
395 return retval;
397 ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
398 oldprio = ceiling;
400 oldval
401 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
402 #ifdef NO_INCR
403 ceilval | 2,
404 #else
405 ceilval | 1,
406 #endif
407 ceilval);
409 if (oldval == ceilval)
410 break;
414 oldval
415 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
416 ceilval | 2,
417 ceilval | 1);
419 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
420 break;
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)
428 != ceilval);
430 while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
432 assert (mutex->__data.__owner == 0);
433 mutex->__data.__count = 1;
435 break;
437 default:
438 /* Correct code cannot set any other type. */
439 return EINVAL;
442 /* Record the ownership. */
443 mutex->__data.__owner = id;
444 #ifndef NO_INCR
445 ++mutex->__data.__nusers;
446 #endif
448 return retval;
450 #ifndef __pthread_mutex_lock
451 strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
452 strong_alias (__pthread_mutex_lock, __pthread_mutex_lock_internal)
453 #endif