mutex.c revision 8f7b56e275abdaaec08ccac32ffc6174841ae60e
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * Copyright (C) 2000, 2001 Internet Software Consortium.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * Permission to use, copy, modify, and distribute this software for any
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * purpose with or without fee is hereby granted, provided that the above
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * copyright notice and this permission notice appear in all copies.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer/* $Id: mutex.c,v 1.7 2002/09/08 18:32:38 explorer Exp $ */
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer/* Operations on timevals; adapted from FreeBSD's sys/time.h */
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyertypedef struct {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer const char * file;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer const char * file; /* File mutex was created in. */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_mutexlocker_t lockers[ISC_MUTEX_MAX_LOCKERS];
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerstatic pthread_mutex_t statslock = PTHREAD_MUTEX_INITIALIZER;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerisc_mutex_init_profile(isc_mutex_t *mp, const char *file, int line) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (pthread_mutex_init(&mp->mutex, NULL) != 0)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer RUNTIME_CHECK(pthread_mutex_lock(&statslock) == 0);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer for (i = 0; i < TABLESIZE; i++) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer for (i = 0; i < TABLESIZE; i++) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer RUNTIME_CHECK(pthread_mutex_unlock(&statslock) == 0);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer timevalclear(&mp->stats->lockers[i].locked_total);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer timevalclear(&mp->stats->lockers[i].wait_total);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerisc_mutex_lock_profile(isc_mutex_t *mp, const char *file, int line) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (mp->stats->lockers[i].file == file &&
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer timevaladd(&mp->stats->wait_total, &postlock_t);
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer timevaladd(&locker->wait_total, &postlock_t);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerisc_mutex_unlock_profile(isc_mutex_t *mp, const char *file, int line) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer timevaladd(&mp->stats->locked_total, &unlock_t);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer timevaladd(&mp->stats->cur_locker->locked_total, &unlock_t);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return ((pthread_mutex_unlock((&mp->mutex)) == 0) ? \
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer for (i = 0; i < TABLESIZE; i++) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer fprintf(fp, "%-12s %4d: %10u %lu.%06lu %lu.%06lu\n",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer stats[i].file, stats[i].line, stats[i].count,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer for (j = 0; j < ISC_MUTEX_MAX_LOCKERS; j++) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer fprintf(fp, " %-11s %4d: %10u %lu.%06lu %lu.%06lu\n",
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer#endif /* ISC_MUTEX_PROFILE */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer#if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer 0 /* m_flags, which appears to be unused. */