Changes and additions migrated from cvs.devel.redhat.com:/cvs/devel/glibc to fedora...
[glibc.git] / nptl / pthread_mutex_lock.c
blobee39f208203272d54708f2e49437d7ffa84738ae
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
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include "pthreadP.h"
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)
29 #endif
32 int
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. */
50 return EAGAIN;
52 ++mutex->__data.__count;
54 return 0;
57 /* We have to get the mutex. */
58 LLL_MUTEX_LOCK (mutex->__data.__lock);
60 mutex->__data.__count = 1;
61 break;
63 /* Error checking mutex. */
64 case PTHREAD_MUTEX_ERRORCHECK_NP:
65 /* Check whether we already hold the mutex. */
66 if (mutex->__data.__owner == id)
67 return EDEADLK;
69 /* FALLTHROUGH */
71 default:
72 /* Correct code cannot set any other type. */
73 case PTHREAD_MUTEX_TIMED_NP:
74 simple:
75 /* Normal mutex. */
76 LLL_MUTEX_LOCK (mutex->__data.__lock);
77 break;
79 case PTHREAD_MUTEX_ADAPTIVE_NP:
80 if (! __is_smp)
81 goto simple;
83 if (LLL_MUTEX_TRYLOCK (mutex->__data.__lock) != 0)
85 int cnt = 0;
86 int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
87 mutex->__data.__spins * 2 + 10);
90 if (cnt++ >= max_cnt)
92 LLL_MUTEX_LOCK (mutex->__data.__lock);
93 break;
96 #ifdef BUSY_WAIT_NOP
97 BUSY_WAIT_NOP;
98 #endif
100 while (LLL_MUTEX_TRYLOCK (mutex->__data.__lock) != 0);
102 mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
104 break;
107 /* Record the ownership. */
108 assert (mutex->__data.__owner == 0);
109 mutex->__data.__owner = id;
110 #ifndef NO_INCR
111 ++mutex->__data.__nusers;
112 #endif
114 return 0;
116 #ifndef __pthread_mutex_lock
117 strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
118 strong_alias (__pthread_mutex_lock, __pthread_mutex_lock_internal)
119 #endif