1 /* Copyright (C) 2002, 2003, 2004 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
23 #include <lowlevellock.h>
26 #ifndef LLL_MUTEX_LOCK
27 # define LLL_MUTEX_LOCK(mutex) lll_mutex_lock (mutex)
28 # define LLL_MUTEX_TRYLOCK(mutex) lll_mutex_trylock (mutex)
33 __pthread_mutex_lock (mutex
)
34 pthread_mutex_t
*mutex
;
36 assert (sizeof (mutex
->__size
) >= sizeof (mutex
->__data
));
38 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
40 switch (__builtin_expect (mutex
->__data
.__kind
, PTHREAD_MUTEX_TIMED_NP
))
42 /* Recursive mutex. */
43 case PTHREAD_MUTEX_RECURSIVE_NP
:
44 /* Check whether we already hold the mutex. */
45 if (mutex
->__data
.__owner
== id
)
47 /* Just bump the counter. */
48 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
49 /* Overflow of the counter. */
52 ++mutex
->__data
.__count
;
57 /* We have to get the mutex. */
58 LLL_MUTEX_LOCK (mutex
->__data
.__lock
);
60 mutex
->__data
.__count
= 1;
63 /* Error checking mutex. */
64 case PTHREAD_MUTEX_ERRORCHECK_NP
:
65 /* Check whether we already hold the mutex. */
66 if (mutex
->__data
.__owner
== id
)
72 /* Correct code cannot set any other type. */
73 case PTHREAD_MUTEX_TIMED_NP
:
76 LLL_MUTEX_LOCK (mutex
->__data
.__lock
);
79 case PTHREAD_MUTEX_ADAPTIVE_NP
:
83 if (LLL_MUTEX_TRYLOCK (mutex
->__data
.__lock
) != 0)
86 int max_cnt
= MIN (MAX_ADAPTIVE_COUNT
,
87 mutex
->__data
.__spins
* 2 + 10);
92 LLL_MUTEX_LOCK (mutex
->__data
.__lock
);
100 while (LLL_MUTEX_TRYLOCK (mutex
->__data
.__lock
) != 0);
102 mutex
->__data
.__spins
+= (cnt
- mutex
->__data
.__spins
) / 8;
107 /* Record the ownership. */
108 assert (mutex
->__data
.__owner
== 0);
109 mutex
->__data
.__owner
= id
;
111 ++mutex
->__data
.__nusers
;
116 #ifndef __pthread_mutex_lock
117 strong_alias (__pthread_mutex_lock
, pthread_mutex_lock
)
118 strong_alias (__pthread_mutex_lock
, __pthread_mutex_lock_internal
)