Updated to fedora-glibc-20060109T2152
[glibc/history.git] / nptl / pthread_mutex_lock.c
blob420711a4d43ee75e85955d5a98e22fb419928d56
1 /* Copyright (C) 2002, 2003, 2004, 2005 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 #endif
33 int
34 __pthread_mutex_lock (mutex)
35 pthread_mutex_t *mutex;
37 assert (sizeof (mutex->__size) >= sizeof (mutex->__data));
39 pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
41 int retval = 0;
42 switch (__builtin_expect (mutex->__data.__kind, PTHREAD_MUTEX_TIMED_NP))
44 /* Recursive mutex. */
45 case PTHREAD_MUTEX_RECURSIVE_NP:
46 /* Check whether we already hold the mutex. */
47 if (mutex->__data.__owner == id)
49 /* Just bump the counter. */
50 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
51 /* Overflow of the counter. */
52 return EAGAIN;
54 ++mutex->__data.__count;
56 return 0;
59 /* We have to get the mutex. */
60 LLL_MUTEX_LOCK (mutex->__data.__lock);
62 assert (mutex->__data.__owner == 0);
63 mutex->__data.__count = 1;
64 break;
66 /* Error checking mutex. */
67 case PTHREAD_MUTEX_ERRORCHECK_NP:
68 /* Check whether we already hold the mutex. */
69 if (__builtin_expect (mutex->__data.__owner == id, 0))
70 return EDEADLK;
72 /* FALLTHROUGH */
74 case PTHREAD_MUTEX_TIMED_NP:
75 simple:
76 /* Normal mutex. */
77 LLL_MUTEX_LOCK (mutex->__data.__lock);
78 assert (mutex->__data.__owner == 0);
79 break;
81 case PTHREAD_MUTEX_ADAPTIVE_NP:
82 if (! __is_smp)
83 goto simple;
85 if (LLL_MUTEX_TRYLOCK (mutex->__data.__lock) != 0)
87 int cnt = 0;
88 int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
89 mutex->__data.__spins * 2 + 10);
92 if (cnt++ >= max_cnt)
94 LLL_MUTEX_LOCK (mutex->__data.__lock);
95 break;
98 #ifdef BUSY_WAIT_NOP
99 BUSY_WAIT_NOP;
100 #endif
102 while (LLL_MUTEX_TRYLOCK (mutex->__data.__lock) != 0);
104 mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
106 assert (mutex->__data.__owner == 0);
107 break;
109 case PTHREAD_MUTEX_ROBUST_PRIVATE_RECURSIVE_NP:
110 /* Check whether we already hold the mutex. */
111 if (abs (mutex->__data.__owner) == id)
113 /* Just bump the counter. */
114 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
115 /* Overflow of the counter. */
116 return EAGAIN;
118 ++mutex->__data.__count;
120 return 0;
123 /* We have to get the mutex. */
124 LLL_MUTEX_LOCK (mutex->__data.__lock);
126 mutex->__data.__count = 1;
128 goto robust;
130 case PTHREAD_MUTEX_ROBUST_PRIVATE_ERRORCHECK_NP:
131 /* Check whether we already hold the mutex. */
132 if (__builtin_expect (abs (mutex->__data.__owner) == id, 0))
133 return EDEADLK;
135 /* FALLTHROUGH */
137 case PTHREAD_MUTEX_ROBUST_PRIVATE_NP:
138 case PTHREAD_MUTEX_ROBUST_PRIVATE_ADAPTIVE_NP:
139 LLL_MUTEX_LOCK (mutex->__data.__lock);
141 robust:
142 if (__builtin_expect (mutex->__data.__owner
143 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
145 /* This mutex is now not recoverable. */
146 mutex->__data.__count = 0;
147 lll_mutex_unlock (mutex->__data.__lock);
148 return ENOTRECOVERABLE;
151 /* This mutex is either healthy or we can try to recover it. */
152 assert (mutex->__data.__owner == 0
153 || mutex->__data.__owner == PTHREAD_MUTEX_OWNERDEAD);
155 if (__builtin_expect (mutex->__data.__owner
156 == PTHREAD_MUTEX_OWNERDEAD, 0))
158 retval = EOWNERDEAD;
159 /* We signal ownership of a not yet recovered robust mutex
160 by storing the negative thread ID. */
161 id = -id;
164 ENQUEUE_MUTEX (mutex);
165 break;
167 default:
168 /* Correct code cannot set any other type. */
169 return EINVAL;
172 /* Record the ownership. */
173 mutex->__data.__owner = id;
174 #ifndef NO_INCR
175 ++mutex->__data.__nusers;
176 #endif
178 return retval;
180 #ifndef __pthread_mutex_lock
181 strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
182 strong_alias (__pthread_mutex_lock, __pthread_mutex_lock_internal)
183 #endif