2 * Copyright (C) 2004, 2005, 2007, 2008, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: mutex.c,v 1.18 2011/01/04 23:47:14 tbox Exp $ */
29 #include <isc/mutex.h>
31 #include <isc/strerror.h>
33 #if HAVE_PTHREADS < 5 /* HP-UX 10.20 has 4, needs this */
34 # define pthread_mutex_init(m, a) \
35 pthread_mutex_init(m, (a) \
36 ? *(const pthread_mutexattr_t *)(a) \
37 : pthread_mutexattr_default)
38 # define PTHREAD_MUTEX_RECURSIVE MUTEX_RECURSIVE_NP
39 # define pthread_mutexattr_settype pthread_mutexattr_setkind_np
45 /*% Operations on timevals; adapted from FreeBSD's sys/time.h */
46 #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
47 #define timevaladd(vvp, uvp) \
49 (vvp)->tv_sec += (uvp)->tv_sec; \
50 (vvp)->tv_usec += (uvp)->tv_usec; \
51 if ((vvp)->tv_usec >= 1000000) { \
53 (vvp)->tv_usec -= 1000000; \
56 #define timevalsub(vvp, uvp) \
58 (vvp)->tv_sec -= (uvp)->tv_sec; \
59 (vvp)->tv_usec -= (uvp)->tv_usec; \
60 if ((vvp)->tv_usec < 0) { \
62 (vvp)->tv_usec += 1000000; \
68 #define ISC_MUTEX_MAX_LOCKERS 32
74 struct timeval locked_total;
75 struct timeval wait_total;
78 struct isc_mutexstats {
79 const char * file; /*%< File mutex was created in. */
80 int line; /*%< Line mutex was created on. */
82 struct timeval lock_t;
83 struct timeval locked_total;
84 struct timeval wait_total;
85 isc_mutexlocker_t * cur_locker;
86 isc_mutexlocker_t lockers[ISC_MUTEX_MAX_LOCKERS];
89 #ifndef ISC_MUTEX_PROFTABLESIZE
90 #define ISC_MUTEX_PROFTABLESIZE (1024 * 1024)
92 static isc_mutexstats_t stats[ISC_MUTEX_PROFTABLESIZE];
93 static int stats_next = 0;
94 static isc_boolean_t stats_init = ISC_FALSE;
95 static pthread_mutex_t statslock = PTHREAD_MUTEX_INITIALIZER;
99 isc_mutex_init_profile(isc_mutex_t *mp, const char *file, int line) {
102 err = pthread_mutex_init(&mp->mutex, NULL);
104 return (ISC_R_NOMEMORY);
106 return (ISC_R_UNEXPECTED);
108 RUNTIME_CHECK(pthread_mutex_lock(&statslock) == 0);
110 if (stats_init == ISC_FALSE)
111 stats_init = ISC_TRUE;
114 * If all statistics entries have been used, give up and trigger an
115 * assertion failure. There would be no other way to deal with this
116 * because we'd like to keep record of all locks for the purpose of
117 * debugging and the number of necessary locks is unpredictable.
118 * If this failure is triggered while debugging, named should be
119 * rebuilt with an increased ISC_MUTEX_PROFTABLESIZE.
121 RUNTIME_CHECK(stats_next < ISC_MUTEX_PROFTABLESIZE);
122 mp->stats = &stats[stats_next++];
124 RUNTIME_CHECK(pthread_mutex_unlock(&statslock) == 0);
126 mp->stats->file = file;
127 mp->stats->line = line;
128 mp->stats->count = 0;
129 timevalclear(&mp->stats->locked_total);
130 timevalclear(&mp->stats->wait_total);
131 for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
132 mp->stats->lockers[i].file = NULL;
133 mp->stats->lockers[i].line = 0;
134 mp->stats->lockers[i].count = 0;
135 timevalclear(&mp->stats->lockers[i].locked_total);
136 timevalclear(&mp->stats->lockers[i].wait_total);
139 return (ISC_R_SUCCESS);
143 isc_mutex_lock_profile(isc_mutex_t *mp, const char *file, int line) {
144 struct timeval prelock_t;
145 struct timeval postlock_t;
146 isc_mutexlocker_t *locker = NULL;
149 gettimeofday(&prelock_t, NULL);
151 if (pthread_mutex_lock(&mp->mutex) != 0)
152 return (ISC_R_UNEXPECTED);
154 gettimeofday(&postlock_t, NULL);
155 mp->stats->lock_t = postlock_t;
157 timevalsub(&postlock_t, &prelock_t);
160 timevaladd(&mp->stats->wait_total, &postlock_t);
162 for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
163 if (mp->stats->lockers[i].file == NULL) {
164 locker = &mp->stats->lockers[i];
168 } else if (mp->stats->lockers[i].file == file &&
169 mp->stats->lockers[i].line == line) {
170 locker = &mp->stats->lockers[i];
175 if (locker != NULL) {
177 timevaladd(&locker->wait_total, &postlock_t);
180 mp->stats->cur_locker = locker;
182 return (ISC_R_SUCCESS);
186 isc_mutex_unlock_profile(isc_mutex_t *mp, const char *file, int line) {
187 struct timeval unlock_t;
192 if (mp->stats->cur_locker != NULL) {
193 gettimeofday(&unlock_t, NULL);
194 timevalsub(&unlock_t, &mp->stats->lock_t);
195 timevaladd(&mp->stats->locked_total, &unlock_t);
196 timevaladd(&mp->stats->cur_locker->locked_total, &unlock_t);
197 mp->stats->cur_locker = NULL;
200 return ((pthread_mutex_unlock((&mp->mutex)) == 0) ? \
201 ISC_R_SUCCESS : ISC_R_UNEXPECTED);
206 isc_mutex_statsprofile(FILE *fp) {
207 isc_mutexlocker_t *locker;
210 fprintf(fp, "Mutex stats (in us)\n");
211 for (i = 0; i < stats_next; i++) {
212 fprintf(fp, "%-12s %4d: %10u %lu.%06lu %lu.%06lu %5d\n",
213 stats[i].file, stats[i].line, stats[i].count,
214 stats[i].locked_total.tv_sec,
215 stats[i].locked_total.tv_usec,
216 stats[i].wait_total.tv_sec,
217 stats[i].wait_total.tv_usec,
219 for (j = 0; j < ISC_MUTEX_MAX_LOCKERS; j++) {
220 locker = &stats[i].lockers[j];
221 if (locker->file == NULL)
223 fprintf(fp, " %-11s %4d: %10u %lu.%06lu %lu.%06lu %5d\n",
224 locker->file, locker->line, locker->count,
225 locker->locked_total.tv_sec,
226 locker->locked_total.tv_usec,
227 locker->wait_total.tv_sec,
228 locker->wait_total.tv_usec,
234 #endif /* ISC_MUTEX_PROFILE */
236 #if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
238 isc_mutex_init_errcheck(isc_mutex_t *mp)
240 pthread_mutexattr_t attr;
243 if (pthread_mutexattr_init(&attr) != 0)
244 return (ISC_R_UNEXPECTED);
246 if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0) {
247 pthread_mutexattr_destroy(&attr);
248 return (ISC_R_UNEXPECTED);
251 err = pthread_mutex_init(mp, &attr) != 0)
252 pthread_mutexattr_destroy(&attr);
254 return (ISC_R_NOMEMORY);
255 return ((err == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED);
259 #if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
260 pthread_mutexattr_t isc__mutex_attrs = {
261 PTHREAD_MUTEX_ERRORCHECK, /* m_type */
262 0 /* m_flags, which appears to be unused. */
266 #if !(ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)) && !ISC_MUTEX_PROFILE
268 isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) {
269 char strbuf[ISC_STRERRORSIZE];
270 isc_result_t result = ISC_R_SUCCESS;
273 err = pthread_mutex_init(mp, ISC__MUTEX_ATTRS);
275 return (ISC_R_NOMEMORY);
277 isc__strerror(errno, strbuf, sizeof(strbuf));
278 UNEXPECTED_ERROR(file, line, "isc_mutex_init() failed: %s",
280 result = ISC_R_UNEXPECTED;