Merge from vendor branch GCC:
[dragonfly.git] / contrib / bind-9.3 / lib / isc / pthreads / include / isc / mutex.h
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2002  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: mutex.h,v 1.23.26.3 2004/03/08 09:04:55 marka Exp $ */
19
20 #ifndef ISC_MUTEX_H
21 #define ISC_MUTEX_H 1
22
23 #include <pthread.h>
24 #include <stdio.h>
25
26 #include <isc/result.h>         /* for ISC_R_ codes */
27
28 /*
29  * Supply mutex attributes that enable deadlock detection
30  * (helpful when debugging).  This is system dependent and
31  * currently only supported on NetBSD.
32  */
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
36 #else
37 #define ISC__MUTEX_ATTRS NULL
38 #endif
39
40 /* XXX We could do fancier error handling... */
41
42 /*
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.
47  */
48 #ifndef ISC_MUTEX_PROFILE
49 #define ISC_MUTEX_PROFILE 0
50 #endif
51
52 #if ISC_MUTEX_PROFILE
53 typedef struct isc_mutexstats isc_mutexstats_t;
54
55 typedef struct {
56         pthread_mutex_t         mutex;  /* The actual mutex. */
57         isc_mutexstats_t *      stats;  /* Mutex statistics. */
58 } isc_mutex_t;
59 #else
60 typedef pthread_mutex_t isc_mutex_t;
61 #endif
62
63
64 #if ISC_MUTEX_PROFILE
65 #define isc_mutex_init(mp) \
66         isc_mutex_init_profile((mp), __FILE__, __LINE__)
67 #else
68 #if ISC_MUTEX_DEBUG && defined(PTHREAD_MUTEX_ERRORCHECK)
69 #define isc_mutex_init(mp) \
70         isc_mutex_init_errcheck((mp))
71 #else
72 #define isc_mutex_init(mp) \
73         ((pthread_mutex_init((mp), ISC__MUTEX_ATTRS) == 0) ? \
74          ISC_R_SUCCESS : ISC_R_UNEXPECTED)
75 #endif
76 #endif
77
78 #if ISC_MUTEX_PROFILE
79 #define isc_mutex_lock(mp) \
80         isc_mutex_lock_profile((mp), __FILE__, __LINE__)
81 #else
82 #define isc_mutex_lock(mp) \
83         ((pthread_mutex_lock((mp)) == 0) ? \
84          ISC_R_SUCCESS : ISC_R_UNEXPECTED)
85 #endif
86
87 #if ISC_MUTEX_PROFILE
88 #define isc_mutex_unlock(mp) \
89         isc_mutex_unlock_profile((mp), __FILE__, __LINE__)
90 #else
91 #define isc_mutex_unlock(mp) \
92         ((pthread_mutex_unlock((mp)) == 0) ? \
93          ISC_R_SUCCESS : ISC_R_UNEXPECTED)
94 #endif
95
96 #if ISC_MUTEX_PROFILE
97 #define isc_mutex_trylock(mp) \
98         ((pthread_mutex_trylock((&(mp)->mutex)) == 0) ? \
99          ISC_R_SUCCESS : ISC_R_LOCKBUSY)
100 #else
101 #define isc_mutex_trylock(mp) \
102         ((pthread_mutex_trylock((mp)) == 0) ? \
103          ISC_R_SUCCESS : ISC_R_LOCKBUSY)
104 #endif
105
106 #if ISC_MUTEX_PROFILE
107 #define isc_mutex_destroy(mp) \
108         ((pthread_mutex_destroy((&(mp)->mutex)) == 0) ? \
109          ISC_R_SUCCESS : ISC_R_UNEXPECTED)
110 #else
111 #define isc_mutex_destroy(mp) \
112         ((pthread_mutex_destroy((mp)) == 0) ? \
113          ISC_R_SUCCESS : ISC_R_UNEXPECTED)
114 #endif
115
116 #if ISC_MUTEX_PROFILE
117 #define isc_mutex_stats(fp) isc_mutex_statsprofile(fp);
118 #else
119 #define isc_mutex_stats(fp)
120 #endif
121
122 #if ISC_MUTEX_PROFILE
123
124 isc_result_t
125 isc_mutex_init_profile(isc_mutex_t *mp, const char * _file, int _line);
126 isc_result_t
127 isc_mutex_lock_profile(isc_mutex_t *mp, const char * _file, int _line);
128 isc_result_t
129 isc_mutex_unlock_profile(isc_mutex_t *mp, const char * _file, int _line);
130
131 void
132 isc_mutex_statsprofile(FILE *fp);
133
134 isc_result_t
135 isc_mutex_init_errcheck(isc_mutex_t *mp);
136
137 #endif /* ISC_MUTEX_PROFILE */
138
139 #endif /* ISC_MUTEX_H */