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
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)
35 __pthread_mutex_lock (mutex
)
36 pthread_mutex_t
*mutex
;
38 assert (sizeof (mutex
->__size
) >= sizeof (mutex
->__data
));
41 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
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. */
56 ++mutex
->__data
.__count
;
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;
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))
76 case PTHREAD_MUTEX_TIMED_NP
:
79 LLL_MUTEX_LOCK (mutex
->__data
.__lock
);
80 assert (mutex
->__data
.__owner
== 0);
83 case PTHREAD_MUTEX_ADAPTIVE_NP
:
87 if (LLL_MUTEX_TRYLOCK (mutex
->__data
.__lock
) != 0)
90 int max_cnt
= MIN (MAX_ADAPTIVE_COUNT
,
91 mutex
->__data
.__spins
* 2 + 10);
96 LLL_MUTEX_LOCK (mutex
->__data
.__lock
);
104 while (LLL_MUTEX_TRYLOCK (mutex
->__data
.__lock
) != 0);
106 mutex
->__data
.__spins
+= (cnt
- mutex
->__data
.__spins
) / 8;
108 assert (mutex
->__data
.__owner
== 0);
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. */
123 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
127 if ((newval
& FUTEX_OWNER_DIED
) == 0)
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
146 --mutex
->__data
.__nusers
;
153 /* Check whether we already hold the mutex. */
154 if (__builtin_expect ((mutex
->__data
.__lock
& FUTEX_TID_MASK
)
157 if (mutex
->__data
.__kind
158 == PTHREAD_MUTEX_ROBUST_PRIVATE_ERRORCHECK_NP
)
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. */
169 ++mutex
->__data
.__count
;
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
);
193 /* Correct code cannot set any other type. */
197 /* Record the ownership. */
198 mutex
->__data
.__owner
= id
;
200 ++mutex
->__data
.__nusers
;
205 #ifndef __pthread_mutex_lock
206 strong_alias (__pthread_mutex_lock
, pthread_mutex_lock
)
207 strong_alias (__pthread_mutex_lock
, __pthread_mutex_lock_internal
)