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_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
;
122 if ((oldval
& FUTEX_OWNER_DIED
) != 0)
124 /* The previous owner died. Try locking the mutex. */
127 newval
|= FUTEX_WAITERS
;
131 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
134 if (newval
!= oldval
)
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
155 --mutex
->__data
.__nusers
;
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
,
172 if (mutex
->__data
.__kind
173 == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
)
175 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
178 /* Just bump the counter. */
179 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
180 /* Overflow of the counter. */
183 ++mutex
->__data
.__count
;
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
);
209 /* Correct code cannot set any other type. */
213 /* Record the ownership. */
214 mutex
->__data
.__owner
= id
;
216 ++mutex
->__data
.__nusers
;
221 #ifndef __pthread_mutex_lock
222 strong_alias (__pthread_mutex_lock
, pthread_mutex_lock
)
223 strong_alias (__pthread_mutex_lock
, __pthread_mutex_lock_internal
)