mutex.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1998-2002  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: mutex.h,v 1.30 2007/06/19 23:47:18 tbox Exp $ */
00019 
00020 #ifndef ISC_MUTEX_H
00021 #define ISC_MUTEX_H 1
00022 
00023 /*! \file */
00024 
00025 #include <pthread.h>
00026 #include <stdio.h>
00027 
00028 #include <isc/lang.h>
00029 #include <isc/result.h>         /* for ISC_R_ codes */
00030 
00031 ISC_LANG_BEGINDECLS
00032 
00033 /*!
00034  * Supply mutex attributes that enable deadlock detection
00035  * (helpful when debugging).  This is system dependent and
00036  * currently only supported on NetBSD.
00037  */
00038 #if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
00039 extern pthread_mutexattr_t isc__mutex_attrs;
00040 #define ISC__MUTEX_ATTRS &isc__mutex_attrs
00041 #else
00042 #define ISC__MUTEX_ATTRS NULL
00043 #endif
00044 
00045 /* XXX We could do fancier error handling... */
00046 
00047 /*!
00048  * Define ISC_MUTEX_PROFILE to turn on profiling of mutexes by line.  When
00049  * enabled, isc_mutex_stats() can be used to print a table showing the
00050  * number of times each type of mutex was locked and the amount of time
00051  * waiting to obtain the lock.
00052  */
00053 #ifndef ISC_MUTEX_PROFILE
00054 #define ISC_MUTEX_PROFILE 0
00055 #endif
00056 
00057 #if ISC_MUTEX_PROFILE
00058 typedef struct isc_mutexstats isc_mutexstats_t;
00059 
00060 typedef struct {
00061         pthread_mutex_t         mutex;  /*%< The actual mutex. */
00062         isc_mutexstats_t *      stats;  /*%< Mutex statistics. */
00063 } isc_mutex_t;
00064 #else
00065 typedef pthread_mutex_t isc_mutex_t;
00066 #endif
00067 
00068 
00069 #if ISC_MUTEX_PROFILE
00070 #define isc_mutex_init(mp) \
00071         isc_mutex_init_profile((mp), __FILE__, __LINE__)
00072 #else
00073 #if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
00074 #define isc_mutex_init(mp) \
00075         isc_mutex_init_errcheck((mp))
00076 #else
00077 #define isc_mutex_init(mp) \
00078         isc__mutex_init((mp), __FILE__, __LINE__)
00079 isc_result_t isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line);
00080 #endif
00081 #endif
00082 
00083 #if ISC_MUTEX_PROFILE
00084 #define isc_mutex_lock(mp) \
00085         isc_mutex_lock_profile((mp), __FILE__, __LINE__)
00086 #else
00087 #define isc_mutex_lock(mp) \
00088         ((pthread_mutex_lock((mp)) == 0) ? \
00089          ISC_R_SUCCESS : ISC_R_UNEXPECTED)
00090 #endif
00091 
00092 #if ISC_MUTEX_PROFILE
00093 #define isc_mutex_unlock(mp) \
00094         isc_mutex_unlock_profile((mp), __FILE__, __LINE__)
00095 #else
00096 #define isc_mutex_unlock(mp) \
00097         ((pthread_mutex_unlock((mp)) == 0) ? \
00098          ISC_R_SUCCESS : ISC_R_UNEXPECTED)
00099 #endif
00100 
00101 #if ISC_MUTEX_PROFILE
00102 #define isc_mutex_trylock(mp) \
00103         ((pthread_mutex_trylock((&(mp)->mutex)) == 0) ? \
00104          ISC_R_SUCCESS : ISC_R_LOCKBUSY)
00105 #else
00106 #define isc_mutex_trylock(mp) \
00107         ((pthread_mutex_trylock((mp)) == 0) ? \
00108          ISC_R_SUCCESS : ISC_R_LOCKBUSY)
00109 #endif
00110 
00111 #if ISC_MUTEX_PROFILE
00112 #define isc_mutex_destroy(mp) \
00113         ((pthread_mutex_destroy((&(mp)->mutex)) == 0) ? \
00114          ISC_R_SUCCESS : ISC_R_UNEXPECTED)
00115 #else
00116 #define isc_mutex_destroy(mp) \
00117         ((pthread_mutex_destroy((mp)) == 0) ? \
00118          ISC_R_SUCCESS : ISC_R_UNEXPECTED)
00119 #endif
00120 
00121 #if ISC_MUTEX_PROFILE
00122 #define isc_mutex_stats(fp) isc_mutex_statsprofile(fp);
00123 #else
00124 #define isc_mutex_stats(fp)
00125 #endif
00126 
00127 #if ISC_MUTEX_PROFILE
00128 
00129 isc_result_t
00130 isc_mutex_init_profile(isc_mutex_t *mp, const char * _file, int _line);
00131 isc_result_t
00132 isc_mutex_lock_profile(isc_mutex_t *mp, const char * _file, int _line);
00133 isc_result_t
00134 isc_mutex_unlock_profile(isc_mutex_t *mp, const char * _file, int _line);
00135 
00136 void
00137 isc_mutex_statsprofile(FILE *fp);
00138 
00139 isc_result_t
00140 isc_mutex_init_errcheck(isc_mutex_t *mp);
00141 
00142 #endif /* ISC_MUTEX_PROFILE */
00143 
00144 ISC_LANG_ENDDECLS
00145 #endif /* ISC_MUTEX_H */

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha