2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: mutex.h,v 1.23.2.1 2004/03/09 06:12:08 marka Exp $ */
26 #include <isc/result.h> /* for ISC_R_ codes */
29 * Supply mutex attributes that enable deadlock detection
30 * (helpful when debugging). This is system dependent and
31 * currently only supported on NetBSD.
33 #if ISC_MUTEX_DEBUG && defined(__NetBSD__) && defined(PTHREAD_MUTEX_ERRORCHECK)
34 extern pthread_mutexattr_t isc__mutex_attrs;
35 #define ISC__MUTEX_ATTRS &isc__mutex_attrs
37 #define ISC__MUTEX_ATTRS NULL
40 /* XXX We could do fancier error handling... */
43 * Define ISC_MUTEX_PROFILE to turn on profiling of mutexes by line. When
44 * enabled, isc_mutex_stats() can be used to print a table showing the
45 * number of times each type of mutex was locked and the amount of time
46 * waiting to obtain the lock.
48 #ifndef ISC_MUTEX_PROFILE
49 #define ISC_MUTEX_PROFILE 0
53 typedef struct isc_mutexstats isc_mutexstats_t;
56 pthread_mutex_t mutex; /* The actual mutex. */
57 isc_mutexstats_t * stats; /* Mutex statistics. */
60 typedef pthread_mutex_t isc_mutex_t;
65 #define isc_mutex_init(mp) \
66 isc_mutex_init_profile((mp), __FILE__, __LINE__)
68 #define isc_mutex_init(mp) \
69 ((pthread_mutex_init((mp), ISC__MUTEX_ATTRS) == 0) ? \
70 ISC_R_SUCCESS : ISC_R_UNEXPECTED)
74 #define isc_mutex_lock(mp) \
75 isc_mutex_lock_profile((mp), __FILE__, __LINE__)
77 #define isc_mutex_lock(mp) \
78 ((pthread_mutex_lock((mp)) == 0) ? \
79 ISC_R_SUCCESS : ISC_R_UNEXPECTED)
83 #define isc_mutex_unlock(mp) \
84 isc_mutex_unlock_profile((mp), __FILE__, __LINE__)
86 #define isc_mutex_unlock(mp) \
87 ((pthread_mutex_unlock((mp)) == 0) ? \
88 ISC_R_SUCCESS : ISC_R_UNEXPECTED)
92 #define isc_mutex_trylock(mp) \
93 ((pthread_mutex_trylock((&(mp)->mutex)) == 0) ? \
94 ISC_R_SUCCESS : ISC_R_LOCKBUSY)
96 #define isc_mutex_trylock(mp) \
97 ((pthread_mutex_trylock((mp)) == 0) ? \
98 ISC_R_SUCCESS : ISC_R_LOCKBUSY)
101 #if ISC_MUTEX_PROFILE
102 #define isc_mutex_destroy(mp) \
103 ((pthread_mutex_destroy((&(mp)->mutex)) == 0) ? \
104 ISC_R_SUCCESS : ISC_R_UNEXPECTED)
106 #define isc_mutex_destroy(mp) \
107 ((pthread_mutex_destroy((mp)) == 0) ? \
108 ISC_R_SUCCESS : ISC_R_UNEXPECTED)
111 #if ISC_MUTEX_PROFILE
112 #define isc_mutex_stats(fp) isc_mutex_statsprofile(fp);
114 #define isc_mutex_stats(fp)
117 #if ISC_MUTEX_PROFILE
120 isc_mutex_init_profile(isc_mutex_t *mp, const char * _file, int _line);
122 isc_mutex_lock_profile(isc_mutex_t *mp, const char * _file, int _line);
124 isc_mutex_unlock_profile(isc_mutex_t *mp, const char * _file, int _line);
127 isc_mutex_statsprofile(FILE *fp);
129 #endif /* ISC_MUTEX_PROFILE */
131 #endif /* ISC_MUTEX_H */