* sysdeps/unix/sysv/linux/not-cancel.h (__openat_not_cancel,
[glibc.git] / nptl / pthread_mutex_lock.c
blobdd22567c71dbca4e5545e5cc611bc1081451ff50
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006 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 "pthreadP.h"
24 #include <lowlevellock.h>
27 #ifndef LLL_MUTEX_LOCK
28 # define LLL_MUTEX_LOCK(mutex) lll_mutex_lock (mutex)
29 # define LLL_MUTEX_TRYLOCK(mutex) lll_mutex_trylock (mutex)
30 # define LLL_ROBUST_MUTEX_LOCK(mutex, id) lll_robust_mutex_lock (mutex, id)
31 #endif
34 int
35 __pthread_mutex_lock (mutex)
36 pthread_mutex_t *mutex;
38 assert (sizeof (mutex->__size) >= sizeof (mutex->__data));
40 int oldval;
41 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
43 int retval = 0;
44 switch (__builtin_expect (mutex->__data.__kind, PTHREAD_MUTEX_TIMED_NP))
46 /* Recursive mutex. */
47 case PTHREAD_MUTEX_RECURSIVE_NP:
48 /* Check whether we already hold the mutex. */
49 if (mutex->__data.__owner == id)
51 /* Just bump the counter. */
52 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
53 /* Overflow of the counter. */
54 return EAGAIN;
56 ++mutex->__data.__count;
58 return 0;
61 /* We have to get the mutex. */
62 LLL_MUTEX_LOCK (mutex->__data.__lock);
64 assert (mutex->__data.__owner == 0);
65 mutex->__data.__count = 1;
66 break;
68 /* Error checking mutex. */
69 case PTHREAD_MUTEX_ERRORCHECK_NP:
70 /* Check whether we already hold the mutex. */
71 if (__builtin_expect (mutex->__data.__owner == id, 0))
72 return EDEADLK;
74 /* FALLTHROUGH */
76 case PTHREAD_MUTEX_TIMED_NP:
77 simple:
78 /* Normal mutex. */
79 LLL_MUTEX_LOCK (mutex->__data.__lock);
80 assert (mutex->__data.__owner == 0);
81 break;
83 case PTHREAD_MUTEX_ADAPTIVE_NP:
84 if (! __is_smp)
85 goto simple;
87 if (LLL_MUTEX_TRYLOCK (mutex->__data.__lock) != 0)
89 int cnt = 0;
90 int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
91 mutex->__data.__spins * 2 + 10);
94 if (cnt++ >= max_cnt)
96 LLL_MUTEX_LOCK (mutex->__data.__lock);
97 break;
100 #ifdef BUSY_WAIT_NOP
101 BUSY_WAIT_NOP;
102 #endif
104 while (LLL_MUTEX_TRYLOCK (mutex->__data.__lock) != 0);
106 mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
108 assert (mutex->__data.__owner == 0);
109 break;
111 case PTHREAD_MUTEX_ROBUST_PRIVATE_RECURSIVE_NP:
112 case PTHREAD_MUTEX_ROBUST_PRIVATE_ERRORCHECK_NP:
113 case PTHREAD_MUTEX_ROBUST_PRIVATE_NP:
114 case PTHREAD_MUTEX_ROBUST_PRIVATE_ADAPTIVE_NP:
115 oldval = mutex->__data.__lock;
118 if ((oldval & FUTEX_OWNER_DIED) != 0)
120 /* The previous owner died. Try locking the mutex. */
121 int newval;
122 while ((newval
123 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
124 id, oldval))
125 != oldval)
127 if ((newval & FUTEX_OWNER_DIED) == 0)
128 goto normal;
129 oldval = newval;
132 /* We got the mutex. */
133 mutex->__data.__count = 1;
134 /* But it is inconsistent unless marked otherwise. */
135 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
137 ENQUEUE_MUTEX (mutex);
139 /* Note that we deliberately exit here. If we fall
140 through to the end of the function __nusers would be
141 incremented which is not correct because the old
142 owner has to be discounted. If we are not supposed
143 to increment __nusers we actually have to decrement
144 it here. */
145 #ifdef NO_INCR
146 --mutex->__data.__nusers;
147 #endif
149 return EOWNERDEAD;
152 normal:
153 /* Check whether we already hold the mutex. */
154 if (__builtin_expect ((mutex->__data.__lock & FUTEX_TID_MASK)
155 == id, 0))
157 if (mutex->__data.__kind
158 == PTHREAD_MUTEX_ROBUST_PRIVATE_ERRORCHECK_NP)
159 return EDEADLK;
161 if (mutex->__data.__kind
162 == PTHREAD_MUTEX_ROBUST_PRIVATE_RECURSIVE_NP)
164 /* Just bump the counter. */
165 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
166 /* Overflow of the counter. */
167 return EAGAIN;
169 ++mutex->__data.__count;
171 return 0;
175 oldval = LLL_ROBUST_MUTEX_LOCK (mutex->__data.__lock, id);
177 if (__builtin_expect (mutex->__data.__owner
178 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
180 /* This mutex is now not recoverable. */
181 mutex->__data.__count = 0;
182 lll_mutex_unlock (mutex->__data.__lock);
183 return ENOTRECOVERABLE;
186 while ((oldval & FUTEX_OWNER_DIED) != 0);
188 mutex->__data.__count = 1;
189 ENQUEUE_MUTEX (mutex);
190 break;
192 default:
193 /* Correct code cannot set any other type. */
194 return EINVAL;
197 /* Record the ownership. */
198 mutex->__data.__owner = id;
199 #ifndef NO_INCR
200 ++mutex->__data.__nusers;
201 #endif
203 return retval;
205 #ifndef __pthread_mutex_lock
206 strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
207 strong_alias (__pthread_mutex_lock, __pthread_mutex_lock_internal)
208 #endif