* sysdeps/unix/sysv/linux/syscalls.list: Add vmsplice.
[glibc.git] / nptl / pthread_mutex_lock.c
blob06eef49c71fec4363c1e651af6e8d8e684a6d4ee
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_RECURSIVE_NP:
112 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
113 case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
114 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
115 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
116 &mutex->__data.__list.__next);
118 oldval = mutex->__data.__lock;
121 again:
122 if ((oldval & FUTEX_OWNER_DIED) != 0)
124 /* The previous owner died. Try locking the mutex. */
125 int newval = id;
126 #ifdef NO_INCR
127 newval |= FUTEX_WAITERS;
128 #endif
130 newval
131 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
132 newval, oldval);
134 if (newval != oldval)
136 oldval = newval;
137 goto again;
140 /* We got the mutex. */
141 mutex->__data.__count = 1;
142 /* But it is inconsistent unless marked otherwise. */
143 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
145 ENQUEUE_MUTEX (mutex);
146 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
148 /* Note that we deliberately exit here. If we fall
149 through to the end of the function __nusers would be
150 incremented which is not correct because the old
151 owner has to be discounted. If we are not supposed
152 to increment __nusers we actually have to decrement
153 it here. */
154 #ifdef NO_INCR
155 --mutex->__data.__nusers;
156 #endif
158 return EOWNERDEAD;
161 /* Check whether we already hold the mutex. */
162 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
164 if (mutex->__data.__kind
165 == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
167 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
168 NULL);
169 return EDEADLK;
172 if (mutex->__data.__kind
173 == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
175 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
176 NULL);
178 /* Just bump the counter. */
179 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
180 /* Overflow of the counter. */
181 return EAGAIN;
183 ++mutex->__data.__count;
185 return 0;
189 oldval = LLL_ROBUST_MUTEX_LOCK (mutex->__data.__lock, id);
191 if (__builtin_expect (mutex->__data.__owner
192 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
194 /* This mutex is now not recoverable. */
195 mutex->__data.__count = 0;
196 lll_mutex_unlock (mutex->__data.__lock);
197 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
198 return ENOTRECOVERABLE;
201 while ((oldval & FUTEX_OWNER_DIED) != 0);
203 mutex->__data.__count = 1;
204 ENQUEUE_MUTEX (mutex);
205 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
206 break;
208 default:
209 /* Correct code cannot set any other type. */
210 return EINVAL;
213 /* Record the ownership. */
214 mutex->__data.__owner = id;
215 #ifndef NO_INCR
216 ++mutex->__data.__nusers;
217 #endif
219 return retval;
221 #ifndef __pthread_mutex_lock
222 strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
223 strong_alias (__pthread_mutex_lock, __pthread_mutex_lock_internal)
224 #endif