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
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)
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
);
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. */
54 ++mutex
->__data
.__count
;
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;
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))
74 case PTHREAD_MUTEX_TIMED_NP
:
77 LLL_MUTEX_LOCK (mutex
->__data
.__lock
);
78 assert (mutex
->__data
.__owner
== 0);
81 case PTHREAD_MUTEX_ADAPTIVE_NP
:
85 if (LLL_MUTEX_TRYLOCK (mutex
->__data
.__lock
) != 0)
88 int max_cnt
= MIN (MAX_ADAPTIVE_COUNT
,
89 mutex
->__data
.__spins
* 2 + 10);
94 LLL_MUTEX_LOCK (mutex
->__data
.__lock
);
102 while (LLL_MUTEX_TRYLOCK (mutex
->__data
.__lock
) != 0);
104 mutex
->__data
.__spins
+= (cnt
- mutex
->__data
.__spins
) / 8;
106 assert (mutex
->__data
.__owner
== 0);
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. */
118 ++mutex
->__data
.__count
;
123 /* We have to get the mutex. */
124 LLL_MUTEX_LOCK (mutex
->__data
.__lock
);
126 mutex
->__data
.__count
= 1;
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))
137 case PTHREAD_MUTEX_ROBUST_PRIVATE_NP
:
138 case PTHREAD_MUTEX_ROBUST_PRIVATE_ADAPTIVE_NP
:
139 LLL_MUTEX_LOCK (mutex
->__data
.__lock
);
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))
159 /* We signal ownership of a not yet recovered robust mutex
160 by storing the negative thread ID. */
164 ENQUEUE_MUTEX (mutex
);
168 /* Correct code cannot set any other type. */
172 /* Record the ownership. */
173 mutex
->__data
.__owner
= id
;
175 ++mutex
->__data
.__nusers
;
180 #ifndef __pthread_mutex_lock
181 strong_alias (__pthread_mutex_lock
, pthread_mutex_lock
)
182 strong_alias (__pthread_mutex_lock
, __pthread_mutex_lock_internal
)