1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1998-2002, 2004, 2005, 2007, 2016 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
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/.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
9a4ce0c25809073f31226faa6ed94c70474cf363Bob Halley
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater/* $Id: mutex.h,v 1.30 2007/06/19 23:47:18 tbox Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#ifndef ISC_MUTEX_H
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#define ISC_MUTEX_H 1
9a4ce0c25809073f31226faa6ed94c70474cf363Bob Halley
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
9a4ce0c25809073f31226faa6ed94c70474cf363Bob Halley#include <pthread.h>
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#include <stdio.h>
9a4ce0c25809073f31226faa6ed94c70474cf363Bob Halley
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews#include <isc/lang.h>
9c4f33b6718407e94d50dbfb4977e16d3f83de9dDavid Lawrence#include <isc/result.h> /* for ISC_R_ codes */
9c4f33b6718407e94d50dbfb4977e16d3f83de9dDavid Lawrence
18d0b5e54be891a1aa938c165b6d439859121ec8Mark AndrewsISC_LANG_BEGINDECLS
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*!
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.
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson */
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
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson#else
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson#define ISC__MUTEX_ATTRS NULL
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson#endif
9afcd92352224325ed65919f69f7f58282fc6623Andreas Gustafsson
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley/* XXX We could do fancier error handling... */
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*!
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.
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington */
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#ifndef ISC_MUTEX_PROFILE
01956482905dd861a9b07d417d469955466b728dDamien Neil#define ISC_MUTEX_PROFILE 0
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#endif
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#if ISC_MUTEX_PROFILE
b2a6ebf1bd4dad1410afba9012a61d87090f03adDamien Neiltypedef struct isc_mutexstats isc_mutexstats_t;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neiltypedef struct {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein pthread_mutex_t mutex; /*%< The actual mutex. */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_mutexstats_t * stats; /*%< Mutex statistics. */
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil} isc_mutex_t;
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#else
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neiltypedef pthread_mutex_t isc_mutex_t;
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#endif
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#if ISC_MUTEX_PROFILE
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#define isc_mutex_init(mp) \
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil isc_mutex_init_profile((mp), __FILE__, __LINE__)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#else
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff#if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff#define isc_mutex_init(mp) \
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews isc_mutex_init_errcheck((mp))
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff#else
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#define isc_mutex_init(mp) \
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews isc__mutex_init((mp), __FILE__, __LINE__)
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrewsisc_result_t isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#endif
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff#endif
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#if ISC_MUTEX_PROFILE
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#define isc_mutex_lock(mp) \
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil isc_mutex_lock_profile((mp), __FILE__, __LINE__)
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#else
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#define isc_mutex_lock(mp) \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ((pthread_mutex_lock((mp)) == 0) ? \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ISC_R_SUCCESS : ISC_R_UNEXPECTED)
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#endif
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#if ISC_MUTEX_PROFILE
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#define isc_mutex_unlock(mp) \
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil isc_mutex_unlock_profile((mp), __FILE__, __LINE__)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#else
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#define isc_mutex_unlock(mp) \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ((pthread_mutex_unlock((mp)) == 0) ? \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ISC_R_SUCCESS : ISC_R_UNEXPECTED)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#endif
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#if ISC_MUTEX_PROFILE
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#define isc_mutex_trylock(mp) \
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil ((pthread_mutex_trylock((&(mp)->mutex)) == 0) ? \
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil ISC_R_SUCCESS : ISC_R_LOCKBUSY)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#else
f671a5c51cc59e266620c0c4026b054908fdd80cBob Halley#define isc_mutex_trylock(mp) \
e4b9761b0ef03597c35d1ef1d86e12514c621f90Michael Graff ((pthread_mutex_trylock((mp)) == 0) ? \
f671a5c51cc59e266620c0c4026b054908fdd80cBob Halley ISC_R_SUCCESS : ISC_R_LOCKBUSY)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#endif
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#if ISC_MUTEX_PROFILE
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#define isc_mutex_destroy(mp) \
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil ((pthread_mutex_destroy((&(mp)->mutex)) == 0) ? \
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil ISC_R_SUCCESS : ISC_R_UNEXPECTED)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#else
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#define isc_mutex_destroy(mp) \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ((pthread_mutex_destroy((mp)) == 0) ? \
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley ISC_R_SUCCESS : ISC_R_UNEXPECTED)
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neil#endif
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#if ISC_MUTEX_PROFILE
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#define isc_mutex_stats(fp) isc_mutex_statsprofile(fp);
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#else
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#define isc_mutex_stats(fp)
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#endif
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#if ISC_MUTEX_PROFILE
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellingtonisc_result_t
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_init_profile(isc_mutex_t *mp, const char * _file, int _line);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_result_t
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_lock_profile(isc_mutex_t *mp, const char * _file, int _line);
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_result_t
e9453d609db9aed9efd2bb4fd287ff3ad11da0b2Damien Neilisc_mutex_unlock_profile(isc_mutex_t *mp, const char * _file, int _line);
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellingtonvoid
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellingtonisc_mutex_statsprofile(FILE *fp);
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graffisc_result_t
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graffisc_mutex_init_errcheck(isc_mutex_t *mp);
8f7b56e275abdaaec08ccac32ffc6174841ae60eMichael Graff
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington#endif /* ISC_MUTEX_PROFILE */
26d20cd51c968e111b4122536825368a17b5ca82Brian Wellington
18d0b5e54be891a1aa938c165b6d439859121ec8Mark AndrewsISC_LANG_ENDDECLS
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley#endif /* ISC_MUTEX_H */