"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/lib/isc/pthreads/include/isc/mutex.h" (4 Sep 2020, 4178 Bytes) of package /linux/misc/dns/bind9/9.17.5/bind-9.17.5.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mutex.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 #ifndef ISC_MUTEX_H
   13 #define ISC_MUTEX_H 1
   14 
   15 /*! \file */
   16 
   17 #include <pthread.h>
   18 #include <stdio.h>
   19 
   20 #include <isc/lang.h>
   21 #include <isc/result.h> /* for ISC_R_ codes */
   22 
   23 ISC_LANG_BEGINDECLS
   24 
   25 /*!
   26  * Supply mutex attributes that enable deadlock detection
   27  * (helpful when debugging).  This is system dependent and
   28  * currently only supported on NetBSD.
   29  */
   30 #if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
   31 extern pthread_mutexattr_t isc__mutex_attrs;
   32 #define ISC__MUTEX_ATTRS &isc__mutex_attrs
   33 #else /* if ISC_MUTEX_DEBUG && defined(__NetBSD__) && \
   34        * defined(PTHREAD_MUTEX_ERRORCHECK) */
   35 #define ISC__MUTEX_ATTRS NULL
   36 #endif /* if ISC_MUTEX_DEBUG && defined(__NetBSD__) && \
   37     * defined(PTHREAD_MUTEX_ERRORCHECK) */
   38 
   39 /* XXX We could do fancier error handling... */
   40 
   41 /*!
   42  * Define ISC_MUTEX_PROFILE to turn on profiling of mutexes by line.  When
   43  * enabled, isc_mutex_stats() can be used to print a table showing the
   44  * number of times each type of mutex was locked and the amount of time
   45  * waiting to obtain the lock.
   46  */
   47 #ifndef ISC_MUTEX_PROFILE
   48 #define ISC_MUTEX_PROFILE 0
   49 #endif /* ifndef ISC_MUTEX_PROFILE */
   50 
   51 #if ISC_MUTEX_PROFILE
   52 typedef struct isc_mutexstats isc_mutexstats_t;
   53 
   54 typedef struct {
   55     pthread_mutex_t   mutex; /*%< The actual mutex. */
   56     isc_mutexstats_t *stats; /*%< Mutex statistics. */
   57 } isc_mutex_t;
   58 #else  /* if ISC_MUTEX_PROFILE */
   59 typedef pthread_mutex_t isc_mutex_t;
   60 #endif /* if ISC_MUTEX_PROFILE */
   61 
   62 #if ISC_MUTEX_PROFILE
   63 #define isc_mutex_init(mp) isc_mutex_init_profile((mp), __FILE__, __LINE__)
   64 #else /* if ISC_MUTEX_PROFILE */
   65 #if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
   66 #define isc_mutex_init(mp) isc_mutex_init_errcheck((mp))
   67 #else /* if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK) */
   68 #define isc_mutex_init(mp) isc__mutex_init((mp), __FILE__, __LINE__)
   69 void
   70 isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line);
   71 #endif /* if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK) */
   72 #endif /* if ISC_MUTEX_PROFILE */
   73 
   74 #if ISC_MUTEX_PROFILE
   75 #define isc_mutex_lock(mp) isc_mutex_lock_profile((mp), __FILE__, __LINE__)
   76 #else /* if ISC_MUTEX_PROFILE */
   77 #define isc_mutex_lock(mp) \
   78     ((pthread_mutex_lock((mp)) == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED)
   79 #endif /* if ISC_MUTEX_PROFILE */
   80 
   81 #if ISC_MUTEX_PROFILE
   82 #define isc_mutex_unlock(mp) isc_mutex_unlock_profile((mp), __FILE__, __LINE__)
   83 #else /* if ISC_MUTEX_PROFILE */
   84 #define isc_mutex_unlock(mp) \
   85     ((pthread_mutex_unlock((mp)) == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED)
   86 #endif /* if ISC_MUTEX_PROFILE */
   87 
   88 #if ISC_MUTEX_PROFILE
   89 #define isc_mutex_trylock(mp)                                         \
   90     ((pthread_mutex_trylock((&(mp)->mutex)) == 0) ? ISC_R_SUCCESS \
   91                               : ISC_R_LOCKBUSY)
   92 #else /* if ISC_MUTEX_PROFILE */
   93 #define isc_mutex_trylock(mp) \
   94     ((pthread_mutex_trylock((mp)) == 0) ? ISC_R_SUCCESS : ISC_R_LOCKBUSY)
   95 #endif /* if ISC_MUTEX_PROFILE */
   96 
   97 #if ISC_MUTEX_PROFILE
   98 #define isc_mutex_destroy(mp) \
   99     RUNTIME_CHECK(pthread_mutex_destroy((&(mp)->mutex)) == 0)
  100 #else /* if ISC_MUTEX_PROFILE */
  101 #define isc_mutex_destroy(mp) RUNTIME_CHECK(pthread_mutex_destroy((mp)) == 0)
  102 #endif /* if ISC_MUTEX_PROFILE */
  103 
  104 #if ISC_MUTEX_PROFILE
  105 #define isc_mutex_stats(fp) isc_mutex_statsprofile(fp);
  106 #else /* if ISC_MUTEX_PROFILE */
  107 #define isc_mutex_stats(fp)
  108 #endif /* if ISC_MUTEX_PROFILE */
  109 
  110 #if ISC_MUTEX_PROFILE
  111 
  112 void
  113 isc_mutex_init_profile(isc_mutex_t *mp, const char *_file, int _line);
  114 isc_result_t
  115 isc_mutex_lock_profile(isc_mutex_t *mp, const char *_file, int _line);
  116 isc_result_t
  117 isc_mutex_unlock_profile(isc_mutex_t *mp, const char *_file, int _line);
  118 
  119 void
  120 isc_mutex_statsprofile(FILE *fp);
  121 
  122 void
  123 isc_mutex_init_errcheck(isc_mutex_t *mp);
  124 
  125 #endif /* ISC_MUTEX_PROFILE */
  126 
  127 ISC_LANG_ENDDECLS
  128 #endif /* ISC_MUTEX_H */