mutex.c revision 8f7b56e275abdaaec08ccac32ffc6174841ae60e
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer/*
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * Copyright (C) 2000, 2001 Internet Software Consortium.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer *
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 *
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.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer/* $Id: mutex.c,v 1.7 2002/09/08 18:32:38 explorer Exp $ */
47058d17266420179fa294de6b82d8fb5b918df4Michael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <config.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <stdio.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <time.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <sys/time.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <isc/mutex.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/util.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#if ISC_MUTEX_PROFILE
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer/* Operations on timevals; adapted from FreeBSD's sys/time.h */
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#define timevaladd(vvp, uvp) \
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer do { \
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer (vvp)->tv_sec += (uvp)->tv_sec; \
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer (vvp)->tv_usec += (uvp)->tv_usec; \
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer if ((vvp)->tv_usec >= 1000000) { \
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer (vvp)->tv_sec++; \
1893b56ef9f5f2bc2a0fbe80d3c6b69df1bdc7c2Michael Sawyer (vvp)->tv_usec -= 1000000; \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer } \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer } while (0)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#define timevalsub(vvp, uvp) \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer do { \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer (vvp)->tv_sec -= (uvp)->tv_sec; \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer (vvp)->tv_usec -= (uvp)->tv_usec; \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if ((vvp)->tv_usec < 0) { \
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer (vvp)->tv_sec--; \
d821f1cd7e97552401296e880e7518c98c9ebea1Michael Sawyer (vvp)->tv_usec += 1000000; \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer } \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer } while (0)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#define ISC_MUTEX_MAX_LOCKERS 32
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyertypedef struct {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer const char * file;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer int line;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer unsigned count;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer struct timeval locked_total;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer struct timeval wait_total;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer} isc_mutexlocker_t;
1893b56ef9f5f2bc2a0fbe80d3c6b69df1bdc7c2Michael Sawyer
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyerstruct isc_mutexstats {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer const char * file; /* File mutex was created in. */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer int line; /* Line mutex was created on. */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer unsigned count;
6c6a62933dda281cb9193de1d54d4c9e74515f5aMichael Sawyer struct timeval lock_t;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer struct timeval locked_total;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer struct timeval wait_total;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_mutexlocker_t * cur_locker;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_mutexlocker_t lockers[ISC_MUTEX_MAX_LOCKERS];
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer};
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#define TABLESIZE (8 * 1024)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerstatic isc_mutexstats_t stats[TABLESIZE];
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerstatic isc_boolean_t stats_init = ISC_FALSE;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerstatic pthread_mutex_t statslock = PTHREAD_MUTEX_INITIALIZER;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerisc_result_t
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerisc_mutex_init_profile(isc_mutex_t *mp, const char *file, int line) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer int i;
a5ed46c9fd270775c39770bfd0250a52d374ebf2Michael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (pthread_mutex_init(&mp->mutex, NULL) != 0)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return ISC_R_UNEXPECTED;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer RUNTIME_CHECK(pthread_mutex_lock(&statslock) == 0);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (stats_init == ISC_FALSE) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer for (i = 0; i < TABLESIZE; i++) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer stats[i].file = NULL;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer stats_init = ISC_TRUE;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer mp->stats = NULL;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer for (i = 0; i < TABLESIZE; i++) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (stats[i].file == NULL) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer mp->stats = &stats[i];
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer break;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
a5ed46c9fd270775c39770bfd0250a52d374ebf2Michael Sawyer RUNTIME_CHECK(mp->stats != NULL);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer RUNTIME_CHECK(pthread_mutex_unlock(&statslock) == 0);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer mp->stats->file = file;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer mp->stats->line = line;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer mp->stats->count = 0;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer timevalclear(&mp->stats->locked_total);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer timevalclear(&mp->stats->wait_total);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer mp->stats->lockers[i].file = NULL;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer mp->stats->lockers[i].line = 0;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer mp->stats->lockers[i].count = 0;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer timevalclear(&mp->stats->lockers[i].locked_total);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer timevalclear(&mp->stats->lockers[i].wait_total);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return ISC_R_SUCCESS;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerisc_result_t
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerisc_mutex_lock_profile(isc_mutex_t *mp, const char *file, int line) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer struct timeval prelock_t;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer struct timeval postlock_t;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_mutexlocker_t *locker = NULL;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer int i;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (mp->stats->lockers[i].file == NULL) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer locker = &mp->stats->lockers[i];
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer locker->file = file;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer locker->line = line;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer break;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (mp->stats->lockers[i].file == file &&
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer mp->stats->lockers[i].line == line) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer locker = &mp->stats->lockers[i];
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer break;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer gettimeofday(&prelock_t, NULL);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (pthread_mutex_lock(&mp->mutex) != 0)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (ISC_R_UNEXPECTED);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer gettimeofday(&postlock_t, NULL);
cd720113a2fc8a781d4e33350b8a2b62857b31d8David Lawrence mp->stats->lock_t = postlock_t;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer timevalsub(&postlock_t, &prelock_t);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer mp->stats->count++;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer timevaladd(&mp->stats->wait_total, &postlock_t);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (locker != NULL) {
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer locker->count++;
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer timevaladd(&locker->wait_total, &postlock_t);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer mp->stats->cur_locker = locker;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return ISC_R_SUCCESS;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerisc_result_t
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerisc_mutex_unlock_profile(isc_mutex_t *mp, const char *file, int line) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer struct timeval unlock_t;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer UNUSED(file);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer UNUSED(line);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (mp->stats->cur_locker != NULL) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer gettimeofday(&unlock_t, NULL);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer timevalsub(&unlock_t, &mp->stats->lock_t);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer timevaladd(&mp->stats->locked_total, &unlock_t);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer timevaladd(&mp->stats->cur_locker->locked_total, &unlock_t);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer mp->stats->cur_locker = NULL;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return ((pthread_mutex_unlock((&mp->mutex)) == 0) ? \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer ISC_R_SUCCESS : ISC_R_UNEXPECTED);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyervoid
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerisc_mutex_statsprofile(FILE *fp) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_mutexlocker_t *locker;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer int i, j;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer fprintf(fp, "Mutex stats (in us)\n");
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer for (i = 0; i < TABLESIZE; i++) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (stats[i].file == NULL)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer continue;
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 stats[i].locked_total.tv_sec,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer stats[i].locked_total.tv_usec,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer stats[i].wait_total.tv_sec,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer stats[i].wait_total.tv_usec
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer );
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer for (j = 0; j < ISC_MUTEX_MAX_LOCKERS; j++) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer locker = &stats[i].lockers[j];
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (locker->file == NULL)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer continue;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer fprintf(fp, " %-11s %4d: %10u %lu.%06lu %lu.%06lu\n",
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer locker->file, locker->line, locker->count,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer locker->locked_total.tv_sec,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer locker->locked_total.tv_usec,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer locker->wait_total.tv_sec,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer locker->wait_total.tv_usec
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer );
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer#endif /* ISC_MUTEX_PROFILE */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerisc_result_t
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerisc_mutex_init_errcheck(isc_mutex_t *mp)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer{
d821f1cd7e97552401296e880e7518c98c9ebea1Michael Sawyer pthread_mutexattr_t attr;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (pthread_mutexattr_init(&attr) != 0)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return ISC_R_UNEXPECTED;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return ISC_R_UNEXPECTED;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (pthread_mutex_init(mp, &attr) != 0)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return ISC_R_UNEXPECTED;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return ISC_R_SUCCESS;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer#endif
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer#if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerpthread_mutexattr_t isc__mutex_attrs = {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer PTHREAD_MUTEX_ERRORCHECK, /* m_type */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer 0 /* m_flags, which appears to be unused. */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer};
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer#endif
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer