4 * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000-2002 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: mutex.c,v 1.16 2008/04/04 23:47:01 tbox Exp */
31 #include <isc/mutex.h>
33 #include <isc/strerror.h>
38 /*% Operations on timevals; adapted from FreeBSD's sys/time.h */
39 #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
40 #define timevaladd(vvp, uvp) \
42 (vvp)->tv_sec += (uvp)->tv_sec; \
43 (vvp)->tv_usec += (uvp)->tv_usec; \
44 if ((vvp)->tv_usec >= 1000000) { \
46 (vvp)->tv_usec -= 1000000; \
49 #define timevalsub(vvp, uvp) \
51 (vvp)->tv_sec -= (uvp)->tv_sec; \
52 (vvp)->tv_usec -= (uvp)->tv_usec; \
53 if ((vvp)->tv_usec < 0) { \
55 (vvp)->tv_usec += 1000000; \
61 #define ISC_MUTEX_MAX_LOCKERS 32
67 struct timeval locked_total
;
68 struct timeval wait_total
;
71 struct isc_mutexstats
{
72 const char * file
; /*%< File mutex was created in. */
73 int line
; /*%< Line mutex was created on. */
75 struct timeval lock_t
;
76 struct timeval locked_total
;
77 struct timeval wait_total
;
78 isc_mutexlocker_t
* cur_locker
;
79 isc_mutexlocker_t lockers
[ISC_MUTEX_MAX_LOCKERS
];
82 #ifndef ISC_MUTEX_PROFTABLESIZE
83 #define ISC_MUTEX_PROFTABLESIZE (16 * 1024)
85 static isc_mutexstats_t stats
[ISC_MUTEX_PROFTABLESIZE
];
86 static int stats_next
= 0;
87 static isc_boolean_t stats_init
= ISC_FALSE
;
88 static pthread_mutex_t statslock
= PTHREAD_MUTEX_INITIALIZER
;
92 isc_mutex_init_profile(isc_mutex_t
*mp
, const char *file
, int line
) {
95 err
= pthread_mutex_init(&mp
->mutex
, NULL
);
97 return (ISC_R_NOMEMORY
);
99 return (ISC_R_UNEXPECTED
);
101 RUNTIME_CHECK(pthread_mutex_lock(&statslock
) == 0);
103 if (stats_init
== ISC_FALSE
)
104 stats_init
= ISC_TRUE
;
107 * If all statistics entries have been used, give up and trigger an
108 * assertion failure. There would be no other way to deal with this
109 * because we'd like to keep record of all locks for the purpose of
110 * debugging and the number of necessary locks is unpredictable.
111 * If this failure is triggered while debugging, named should be
112 * rebuilt with an increased ISC_MUTEX_PROFTABLESIZE.
114 RUNTIME_CHECK(stats_next
< ISC_MUTEX_PROFTABLESIZE
);
115 mp
->stats
= &stats
[stats_next
++];
117 RUNTIME_CHECK(pthread_mutex_unlock(&statslock
) == 0);
119 mp
->stats
->file
= file
;
120 mp
->stats
->line
= line
;
121 mp
->stats
->count
= 0;
122 timevalclear(&mp
->stats
->locked_total
);
123 timevalclear(&mp
->stats
->wait_total
);
124 for (i
= 0; i
< ISC_MUTEX_MAX_LOCKERS
; i
++) {
125 mp
->stats
->lockers
[i
].file
= NULL
;
126 mp
->stats
->lockers
[i
].line
= 0;
127 mp
->stats
->lockers
[i
].count
= 0;
128 timevalclear(&mp
->stats
->lockers
[i
].locked_total
);
129 timevalclear(&mp
->stats
->lockers
[i
].wait_total
);
132 return (ISC_R_SUCCESS
);
136 isc_mutex_lock_profile(isc_mutex_t
*mp
, const char *file
, int line
) {
137 struct timeval prelock_t
;
138 struct timeval postlock_t
;
139 isc_mutexlocker_t
*locker
= NULL
;
142 gettimeofday(&prelock_t
, NULL
);
144 if (pthread_mutex_lock(&mp
->mutex
) != 0)
145 return (ISC_R_UNEXPECTED
);
147 gettimeofday(&postlock_t
, NULL
);
148 mp
->stats
->lock_t
= postlock_t
;
150 timevalsub(&postlock_t
, &prelock_t
);
153 timevaladd(&mp
->stats
->wait_total
, &postlock_t
);
155 for (i
= 0; i
< ISC_MUTEX_MAX_LOCKERS
; i
++) {
156 if (mp
->stats
->lockers
[i
].file
== NULL
) {
157 locker
= &mp
->stats
->lockers
[i
];
161 } else if (mp
->stats
->lockers
[i
].file
== file
&&
162 mp
->stats
->lockers
[i
].line
== line
) {
163 locker
= &mp
->stats
->lockers
[i
];
168 if (locker
!= NULL
) {
170 timevaladd(&locker
->wait_total
, &postlock_t
);
173 mp
->stats
->cur_locker
= locker
;
175 return (ISC_R_SUCCESS
);
179 isc_mutex_unlock_profile(isc_mutex_t
*mp
, const char *file
, int line
) {
180 struct timeval unlock_t
;
185 if (mp
->stats
->cur_locker
!= NULL
) {
186 gettimeofday(&unlock_t
, NULL
);
187 timevalsub(&unlock_t
, &mp
->stats
->lock_t
);
188 timevaladd(&mp
->stats
->locked_total
, &unlock_t
);
189 timevaladd(&mp
->stats
->cur_locker
->locked_total
, &unlock_t
);
190 mp
->stats
->cur_locker
= NULL
;
193 return ((pthread_mutex_unlock((&mp
->mutex
)) == 0) ? \
194 ISC_R_SUCCESS
: ISC_R_UNEXPECTED
);
199 isc_mutex_statsprofile(FILE *fp
) {
200 isc_mutexlocker_t
*locker
;
203 fprintf(fp
, "Mutex stats (in us)\n");
204 for (i
= 0; i
< stats_next
; i
++) {
205 fprintf(fp
, "%-12s %4d: %10u %lu.%06lu %lu.%06lu\n",
206 stats
[i
].file
, stats
[i
].line
, stats
[i
].count
,
207 stats
[i
].locked_total
.tv_sec
,
208 stats
[i
].locked_total
.tv_usec
,
209 stats
[i
].wait_total
.tv_sec
,
210 stats
[i
].wait_total
.tv_usec
212 for (j
= 0; j
< ISC_MUTEX_MAX_LOCKERS
; j
++) {
213 locker
= &stats
[i
].lockers
[j
];
214 if (locker
->file
== NULL
)
216 fprintf(fp
, " %-11s %4d: %10u %lu.%06lu %lu.%06lu\n",
217 locker
->file
, locker
->line
, locker
->count
,
218 locker
->locked_total
.tv_sec
,
219 locker
->locked_total
.tv_usec
,
220 locker
->wait_total
.tv_sec
,
221 locker
->wait_total
.tv_usec
227 #endif /* ISC_MUTEX_PROFILE */
229 #if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
231 isc_mutex_init_errcheck(isc_mutex_t
*mp
)
233 pthread_mutexattr_t attr
;
236 if (pthread_mutexattr_init(&attr
) != 0)
237 return (ISC_R_UNEXPECTED
);
239 if (pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_ERRORCHECK
) != 0)
240 return (ISC_R_UNEXPECTED
);
242 err
= pthread_mutex_init(mp
, &attr
) != 0)
244 return (ISC_R_NOMEMORY
);
245 return ((err
== 0) ? ISC_R_SUCCESS
: ISC_R_UNEXPECTED
);
249 #if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
250 pthread_mutexattr_t isc__mutex_attrs
= {
251 PTHREAD_MUTEX_ERRORCHECK
, /* m_type */
252 0 /* m_flags, which appears to be unused. */
256 #if !(ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)) && !ISC_MUTEX_PROFILE
258 isc__mutex_init(isc_mutex_t
*mp
, const char *file
, unsigned int line
) {
259 char strbuf
[ISC_STRERRORSIZE
];
260 isc_result_t result
= ISC_R_SUCCESS
;
263 err
= pthread_mutex_init(mp
, ISC__MUTEX_ATTRS
);
265 return (ISC_R_NOMEMORY
);
267 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
268 UNEXPECTED_ERROR(file
, line
, "isc_mutex_init() failed: %s",
270 result
= ISC_R_UNEXPECTED
;