0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1998-2002, 2004, 2005, 2007, 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/.
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater/* $Id: mutex.h,v 1.30 2007/06/19 23:47:18 tbox Exp $ */
9c4f33b6718407e94d50dbfb4977e16d3f83de9dDavid Lawrence#include <isc/result.h> /* for ISC_R_ codes */
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson * Supply mutex attributes that enable deadlock detection
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson * (helpful when debugging). This is system dependent and
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson * currently only supported on NetBSD.
d1dc805692ff816e28849396577affa9b4890e41Andreas Gustafsson#if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafssonextern pthread_mutexattr_t isc__mutex_attrs;
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson#define ISC__MUTEX_ATTRS &isc__mutex_attrs
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley/* XXX We could do fancier error handling... */
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington * Define ISC_MUTEX_PROFILE to turn on profiling of mutexes by line. When
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington * enabled, isc_mutex_stats() can be used to print a table showing the
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington * number of times each type of mutex was locked and the amount of time
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington * waiting to obtain the lock.
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neiltypedef struct {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein pthread_mutex_t mutex; /*%< The actual mutex. */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_mutexstats_t * stats; /*%< Mutex statistics. */
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil isc_mutex_init_profile((mp), __FILE__, __LINE__)
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff#if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrewsisc_result_t isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil isc_mutex_lock_profile((mp), __FILE__, __LINE__)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil isc_mutex_unlock_profile((mp), __FILE__, __LINE__)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil ((pthread_mutex_trylock((&(mp)->mutex)) == 0) ? \
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil ((pthread_mutex_destroy((&(mp)->mutex)) == 0) ? \
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#define isc_mutex_stats(fp) isc_mutex_statsprofile(fp);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_init_profile(isc_mutex_t *mp, const char * _file, int _line);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_lock_profile(isc_mutex_t *mp, const char * _file, int _line);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_unlock_profile(isc_mutex_t *mp, const char * _file, int _line);
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#endif /* ISC_MUTEX_PROFILE */
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#endif /* ISC_MUTEX_H */