0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2000-2002, 2004, 2005, 2007, 2008, 2011, 2012, 2014-2016 Internet Systems Consortium, Inc. ("ISC")
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/.
1da9dbcf48dcfc5c1589def3e785844935d1882cAutomatic Updater/* $Id: mutex.c,v 1.18 2011/01/04 23:47:14 tbox Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*% Operations on timevals; adapted from FreeBSD's sys/time.h */
e535db4a214f5a69114101311fef69a86655e74fDamien Neil#define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neiltypedef struct {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein const char * file; /*%< File mutex was created in. */
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman int line; /*%< Line mutex was created on. */
e535db4a214f5a69114101311fef69a86655e74fDamien Neil isc_mutexlocker_t lockers[ISC_MUTEX_MAX_LOCKERS];
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉static isc_mutexstats_t stats[ISC_MUTEX_PROFTABLESIZE];
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilstatic pthread_mutex_t statslock = PTHREAD_MUTEX_INITIALIZER;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_init_profile(isc_mutex_t *mp, const char *file, int line) {
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil RUNTIME_CHECK(pthread_mutex_lock(&statslock) == 0);
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 神明達哉 RUNTIME_CHECK(stats_next < ISC_MUTEX_PROFTABLESIZE);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil RUNTIME_CHECK(pthread_mutex_unlock(&statslock) == 0);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevalclear(&mp->stats->lockers[i].locked_total);
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevalclear(&mp->stats->lockers[i].wait_total);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_lock_profile(isc_mutex_t *mp, const char *file, int line) {
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevaladd(&mp->stats->wait_total, &postlock_t);
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews for (i = 0; i < ISC_MUTEX_MAX_LOCKERS; i++) {
5e5b467e8c8abda496b7896241a46b05256cd22cMark Andrews } else if (mp->stats->lockers[i].file == file &&
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_unlock_profile(isc_mutex_t *mp, const char *file, int line) {
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevaladd(&mp->stats->locked_total, &unlock_t);
e535db4a214f5a69114101311fef69a86655e74fDamien Neil timevaladd(&mp->stats->cur_locker->locked_total, &unlock_t);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil return ((pthread_mutex_unlock((&mp->mutex)) == 0) ? \
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉 for (i = 0; i < stats_next; i++) {
bdf5cdc32c658fbdfccfc3a1b9404188f1d3df98Evan Hunt fprintf(fp, "%-12s %4d: %10u %lu.%06lu %lu.%06lu %5d\n",
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil for (j = 0; j < ISC_MUTEX_MAX_LOCKERS; j++) {
bdf5cdc32c658fbdfccfc3a1b9404188f1d3df98Evan Hunt fprintf(fp, " %-11s %4d: %10u %lu.%06lu %lu.%06lu %5d\n",
3f160155daf02b1133855fb3368b1250f0baf147Brian Wellington#endif /* ISC_MUTEX_PROFILE */
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff#if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanstatic isc_boolean_t errcheck_initialized = ISC_FALSE;
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanstatic isc_once_t once_errcheck = ISC_ONCE_INIT;
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman RUNTIME_CHECK(pthread_mutexattr_init(&errcheck) == 0);
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman (&errcheck, PTHREAD_MUTEX_ERRORCHECK) == 0);
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman result = isc_once_do(&once_errcheck, initialize_errcheck);
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews return ((err == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED);
d1dc805692ff816e28849396577affa9b4890e41Andreas Gustafsson#if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson 0 /* m_flags, which appears to be unused. */
c4fadc88619959582ac89b841c489be0519ae79dTatuya JINMEI 神明達哉#if !(ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)) && !ISC_MUTEX_PROFILE
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanstatic isc_boolean_t attr_initialized = ISC_FALSE;
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaramanstatic isc_once_t once_attr = ISC_ONCE_INIT;
42580072debcea85ce4a07585803411b98eec3e7Mark Andrews#endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman RUNTIME_CHECK(pthread_mutexattr_init(&attr) == 0);
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman#endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrewsisc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) {
07dd40e8eeff964d7e8018817378d903ded690e3Mukund Sivaraman result = isc_once_do(&once_attr, initialize_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 */
fe12a8f1077c1556922f1a3be2f592c761917838Mukund Sivaraman isc__strerror(err, strbuf, sizeof(strbuf));
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews UNEXPECTED_ERROR(file, line, "isc_mutex_init() failed: %s",