3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2000-2002, 2004, 2005, 2007, 2008, 2011, 2012, 2014-2016 Internet Systems Consortium, Inc. ("ISC")
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington */
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington
1da9dbcf48dcfc5c1589def3e785844935d1882cAutomatic Updater/* $Id: mutex.c,v 1.18 2011/01/04 23:47:14 tbox Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington#include <config.h>
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington#include <stdio.h>
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington#include <time.h>
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington#include <sys/time.h>
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews#include <errno.h>
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington#include <isc/mutex.h>
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington#include <isc/util.h>
3759f10fc543747668b1ca4b4671f35b0dea8445Francis Dupont#include <isc/print.h>
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews#include <isc/strerror.h>
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman#include <isc/once.h>
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington#if ISC_MUTEX_PROFILE
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*@{*/
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*% Operations on timevals; adapted from FreeBSD's sys/time.h */
e535db4a214f5a69114101311fef69a86655e74fDamien Neil#define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman#define timevaladd(vvp, uvp) \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman do { \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman (vvp)->tv_sec += (uvp)->tv_sec; \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman (vvp)->tv_usec += (uvp)->tv_usec; \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman if ((vvp)->tv_usec >= 1000000) { \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman (vvp)->tv_sec++; \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman (vvp)->tv_usec -= 1000000; \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman } \
f052a01ff268cdbbf3e6f600b06d02ed8573badfAutomatic Updater } while (0)
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman#define timevalsub(vvp, uvp) \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman do { \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman (vvp)->tv_sec -= (uvp)->tv_sec; \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman (vvp)->tv_usec -= (uvp)->tv_usec; \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman if ((vvp)->tv_usec < 0) { \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman (vvp)->tv_sec--; \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman (vvp)->tv_usec += 1000000; \
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman } \
f052a01ff268cdbbf3e6f600b06d02ed8573badfAutomatic Updater } while (0)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*@}*/
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#define ISC_MUTEX_MAX_LOCKERS 32
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neiltypedef struct {
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil const char * file;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil int line;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil unsigned count;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil struct timeval locked_total;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil struct timeval wait_total;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil} isc_mutexlocker_t;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e535db4a214f5a69114101311fef69a86655e74fDamien Neilstruct isc_mutexstats {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein const char * file; /*%< File mutex was created in. */
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman int line; /*%< Line mutex was created on. */
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil unsigned count;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil struct timeval lock_t;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil struct timeval locked_total;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil struct timeval wait_total;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil isc_mutexlocker_t * cur_locker;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil isc_mutexlocker_t lockers[ISC_MUTEX_MAX_LOCKERS];
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil};
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉#ifndef ISC_MUTEX_PROFTABLESIZE
bdf5cdc32c658fbdfccfc3a1b9404188f1d3df98Evan Hunt#define ISC_MUTEX_PROFTABLESIZE (1024 * 1024)
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉#endif
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉static isc_mutexstats_t stats[ISC_MUTEX_PROFTABLESIZE];
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉static int stats_next = 0;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilstatic isc_boolean_t stats_init = ISC_FALSE;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilstatic pthread_mutex_t statslock = PTHREAD_MUTEX_INITIALIZER;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_result_t
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_init_profile(isc_mutex_t *mp, const char *file, int line) {
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews int i, err;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews err = pthread_mutex_init(&mp->mutex, NULL);
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews if (err == ENOMEM)
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews return (ISC_R_NOMEMORY);
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews if (err != 0)
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews return (ISC_R_UNEXPECTED);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil RUNTIME_CHECK(pthread_mutex_lock(&statslock) == 0);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 if (stats_init == ISC_FALSE)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil stats_init = ISC_TRUE;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 /*
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 * If all statistics entries have been used, give up and trigger an
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 * assertion failure. There would be no other way to deal with this
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 * because we'd like to keep record of all locks for the purpose of
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 * debugging and the number of necessary locks is unpredictable.
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 * If this failure is triggered while debugging, named should be
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 * rebuilt with an increased ISC_MUTEX_PROFTABLESIZE.
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 */
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 RUNTIME_CHECK(stats_next < ISC_MUTEX_PROFTABLESIZE);
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 mp->stats = &stats[stats_next++];
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil RUNTIME_CHECK(pthread_mutex_unlock(&statslock) == 0);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil mp->stats->file = file;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil mp->stats->line = line;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil mp->stats->count = 0;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevalclear(&mp->stats->locked_total);
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevalclear(&mp->stats->wait_total);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil mp->stats->lockers[i].file = NULL;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil mp->stats->lockers[i].line = 0;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil mp->stats->lockers[i].count = 0;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevalclear(&mp->stats->lockers[i].locked_total);
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevalclear(&mp->stats->lockers[i].wait_total);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil }
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews return (ISC_R_SUCCESS);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil}
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_result_t
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_lock_profile(isc_mutex_t *mp, const char *file, int line) {
e535db4a214f5a69114101311fef69a86655e74fDamien Neil struct timeval prelock_t;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil struct timeval postlock_t;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil isc_mutexlocker_t *locker = NULL;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil int i;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e535db4a214f5a69114101311fef69a86655e74fDamien Neil gettimeofday(&prelock_t, NULL);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil if (pthread_mutex_lock(&mp->mutex) != 0)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil return (ISC_R_UNEXPECTED);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e535db4a214f5a69114101311fef69a86655e74fDamien Neil gettimeofday(&postlock_t, NULL);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil mp->stats->lock_t = postlock_t;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevalsub(&postlock_t, &prelock_t);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil mp->stats->count++;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevaladd(&mp->stats->wait_total, &postlock_t);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews if (mp->stats->lockers[i].file == NULL) {
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews locker = &mp->stats->lockers[i];
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews locker->file = file;
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews locker->line = line;
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews break;
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews } else if (mp->stats->lockers[i].file == file &&
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews mp->stats->lockers[i].line == line) {
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews locker = &mp->stats->lockers[i];
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews break;
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews }
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews }
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil if (locker != NULL) {
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil locker->count++;
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevaladd(&locker->wait_total, &postlock_t);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil }
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil mp->stats->cur_locker = locker;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews return (ISC_R_SUCCESS);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil}
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_result_t
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_unlock_profile(isc_mutex_t *mp, const char *file, int line) {
e535db4a214f5a69114101311fef69a86655e74fDamien Neil struct timeval unlock_t;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil UNUSED(file);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil UNUSED(line);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil if (mp->stats->cur_locker != NULL) {
e535db4a214f5a69114101311fef69a86655e74fDamien Neil gettimeofday(&unlock_t, NULL);
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevalsub(&unlock_t, &mp->stats->lock_t);
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevaladd(&mp->stats->locked_total, &unlock_t);
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevaladd(&mp->stats->cur_locker->locked_total, &unlock_t);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil mp->stats->cur_locker = NULL;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil }
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil return ((pthread_mutex_unlock((&mp->mutex)) == 0) ? \
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil ISC_R_SUCCESS : ISC_R_UNEXPECTED);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil}
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilvoid
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_statsprofile(FILE *fp) {
e535db4a214f5a69114101311fef69a86655e74fDamien Neil isc_mutexlocker_t *locker;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil int i, j;
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil fprintf(fp, "Mutex stats (in us)\n");
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 for (i = 0; i < stats_next; i++) {
bdf5cdc32c658fbdfccfc3a1b9404188f1d3df98Evan Hunt fprintf(fp, "%-12s %4d: %10u %lu.%06lu %lu.%06lu %5d\n",
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil stats[i].file, stats[i].line, stats[i].count,
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil stats[i].locked_total.tv_sec,
e535db4a214f5a69114101311fef69a86655e74fDamien Neil stats[i].locked_total.tv_usec,
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil stats[i].wait_total.tv_sec,
bdf5cdc32c658fbdfccfc3a1b9404188f1d3df98Evan Hunt stats[i].wait_total.tv_usec,
bdf5cdc32c658fbdfccfc3a1b9404188f1d3df98Evan Hunt i);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil for (j = 0; j < ISC_MUTEX_MAX_LOCKERS; j++) {
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil locker = &stats[i].lockers[j];
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil if (locker->file == NULL)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil continue;
bdf5cdc32c658fbdfccfc3a1b9404188f1d3df98Evan Hunt fprintf(fp, " %-11s %4d: %10u %lu.%06lu %lu.%06lu %5d\n",
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil locker->file, locker->line, locker->count,
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil locker->locked_total.tv_sec,
e535db4a214f5a69114101311fef69a86655e74fDamien Neil locker->locked_total.tv_usec,
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil locker->wait_total.tv_sec,
bdf5cdc32c658fbdfccfc3a1b9404188f1d3df98Evan Hunt locker->wait_total.tv_usec,
bdf5cdc32c658fbdfccfc3a1b9404188f1d3df98Evan Hunt i);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil }
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil }
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil}
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington#endif /* ISC_MUTEX_PROFILE */
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff#if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanstatic isc_boolean_t errcheck_initialized = ISC_FALSE;
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanstatic pthread_mutexattr_t errcheck;
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanstatic isc_once_t once_errcheck = ISC_ONCE_INIT;
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanstatic void
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramaninitialize_errcheck(void) {
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman RUNTIME_CHECK(pthread_mutexattr_init(&errcheck) == 0);
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman RUNTIME_CHECK(pthread_mutexattr_settype
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman (&errcheck, PTHREAD_MUTEX_ERRORCHECK) == 0);
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman errcheck_initialized = ISC_TRUE;
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman}
f052a01ff268cdbbf3e6f600b06d02ed8573badfAutomatic Updater
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanisc_result_t
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanisc_mutex_init_errcheck(isc_mutex_t *mp) {
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman isc_result_t result;
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman int err;
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman result = isc_once_do(&once_errcheck, initialize_errcheck);
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman RUNTIME_CHECK(result == ISC_R_SUCCESS);
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman err = pthread_mutex_init(mp, &errcheck);
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews if (err == ENOMEM)
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews return (ISC_R_NOMEMORY);
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews return ((err == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED);
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff}
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff#endif
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff
d1dc805692ff816e28849396577affa9b4890e41Andreas Gustafsson#if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafssonpthread_mutexattr_t isc__mutex_attrs = {
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson PTHREAD_MUTEX_ERRORCHECK, /* m_type */
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson 0 /* m_flags, which appears to be unused. */
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson};
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson#endif
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉#if !(ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)) && !ISC_MUTEX_PROFILE
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman
42580072debcea85ce4a07585803411b98eec3e7Mark Andrews#ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanstatic isc_boolean_t attr_initialized = ISC_FALSE;
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanstatic pthread_mutexattr_t attr;
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanstatic isc_once_t once_attr = ISC_ONCE_INIT;
42580072debcea85ce4a07585803411b98eec3e7Mark Andrews#endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman#ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanstatic void
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramaninitialize_attr(void) {
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman RUNTIME_CHECK(pthread_mutexattr_init(&attr) == 0);
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman RUNTIME_CHECK(pthread_mutexattr_settype
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman (&attr, PTHREAD_MUTEX_ADAPTIVE_NP) == 0);
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman attr_initialized = ISC_TRUE;
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman}
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman#endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrewsisc_result_t
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrewsisc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) {
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews char strbuf[ISC_STRERRORSIZE];
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews isc_result_t result = ISC_R_SUCCESS;
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews int err;
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt#ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman result = isc_once_do(&once_attr, initialize_attr);
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman RUNTIME_CHECK(result == ISC_R_SUCCESS);
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt err = pthread_mutex_init(mp, &attr);
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt#else /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews err = pthread_mutex_init(mp, ISC__MUTEX_ATTRS);
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt#endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews if (err == ENOMEM)
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews return (ISC_R_NOMEMORY);
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews if (err != 0) {
fe12a8f1077c1556922f1a3be2f592c761917838Mukund Sivaraman isc__strerror(err, strbuf, sizeof(strbuf));
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews UNEXPECTED_ERROR(file, line, "isc_mutex_init() failed: %s",
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews strbuf);
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews result = ISC_R_UNEXPECTED;
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews }
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews return (result);
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews}
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉#endif