]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/lockmgr.h
Fix #2047 about bthread_cond_wait_p not declared
[bacula/bacula] / bacula / src / lib / lockmgr.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2008-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16
17 #ifndef _LOCKMGR_H
18 #define _LOCKMGR_H 1
19
20 #include "mutex_list.h"     /* Manage mutex with priority in a central place */
21
22 /*
23  * P and V op that don't use the lock manager (for memory allocation or on
24  * win32)
25  */
26 void lmgr_p(pthread_mutex_t *m);
27 void lmgr_v(pthread_mutex_t *m);
28
29 #ifdef _USE_LOCKMGR
30
31 typedef struct bthread_mutex_t
32 {
33    pthread_mutex_t mutex;
34    int priority;
35 } bthread_mutex_t;
36
37 /*
38  * We decide that a thread won't lock more than LMGR_MAX_LOCK at the same time
39  */
40 #define LMGR_MAX_LOCK 32
41
42 int bthread_cond_wait_p(pthread_cond_t *cond,
43                         bthread_mutex_t *mutex,
44                         const char *file="*unknown*", int line=0);
45
46 int bthread_cond_timedwait_p(pthread_cond_t *cond,
47                              bthread_mutex_t *mutex,
48                              const struct timespec * abstime,
49                              const char *file="*unknown*", int line=0);
50
51 /* Same with real pthread_mutex_t */
52 int bthread_cond_wait_p(pthread_cond_t *cond,
53                         pthread_mutex_t *mutex,
54                         const char *file="*unknown*", int line=0);
55
56 int bthread_cond_timedwait_p(pthread_cond_t *cond,
57                              pthread_mutex_t *mutex,
58                              const struct timespec * abstime,
59                              const char *file="*unknown*", int line=0);
60
61 /* Replacement of pthread_mutex_lock()  but with real pthread_mutex_t */
62 int bthread_mutex_lock_p(pthread_mutex_t *m,
63                          const char *file="*unknown*", int line=0);
64
65 /* Replacement for pthread_mutex_unlock() but with real pthread_mutex_t */
66 int bthread_mutex_unlock_p(pthread_mutex_t *m,
67                            const char *file="*unknown*", int line=0);
68
69 /* Replacement of pthread_mutex_lock() */
70 int bthread_mutex_lock_p(bthread_mutex_t *m,
71                          const char *file="*unknown*", int line=0);
72
73 /* Replacement of pthread_mutex_unlock() */
74 int bthread_mutex_unlock_p(bthread_mutex_t *m,
75                            const char *file="*unknown*", int line=0);
76
77 /*  Test if this mutex is locked by the current thread
78  *     0 - not locked by the current thread
79  *     1 - locked by the current thread
80  */
81 int lmgr_mutex_is_locked(void *m);
82
83 /*
84  * Use them when you want use your lock yourself (ie rwlock)
85  */
86
87 /* Call before requesting the lock */
88 void lmgr_pre_lock(void *m, int prio=0,
89                    const char *file="*unknown*", int line=0);
90
91 /* Call after getting it */
92 void lmgr_post_lock();
93
94 /* Same as pre+post lock */
95 void lmgr_do_lock(void *m, int prio=0,
96                   const char *file="*unknown*", int line=0);
97
98 /* Call just before releasing the lock */
99 void lmgr_do_unlock(void *m);
100
101 /* We use C++ mangling to make integration eaysier */
102 int pthread_mutex_init(bthread_mutex_t *m, const pthread_mutexattr_t *attr);
103 int pthread_mutex_destroy(bthread_mutex_t *m);
104
105 void bthread_mutex_set_priority(bthread_mutex_t *m, int prio);
106
107 /*
108  * Each thread have to call this function to put a lmgr_thread_t object
109  * in the stack and be able to call mutex_lock/unlock
110  */
111 void lmgr_init_thread();
112
113 /*
114  * Call this function at the end of the thread
115  */
116 void lmgr_cleanup_thread();
117
118 /*
119  * Call this at the end of the program, it will release the
120  * global lock manager
121  */
122 void lmgr_cleanup_main();
123
124 /*
125  * Dump each lmgr_thread_t object to stdout
126  */
127 void lmgr_dump();
128
129 /*
130  * Search a deadlock
131  */
132 bool lmgr_detect_deadlock();
133
134 /* Bit flags */
135 #define LMGR_EVENT_NONE    0
136 #define LMGR_EVENT_DUP     1       /* use strdup() to copy the comment (will set FREE) */
137 #define LMGR_EVENT_FREE    2       /* use free() when overwriting/deleting the comment */
138 #define LMGR_EVENT_INVALID 4       /* Used to mark the record invalid */
139
140 /*
141  * Add event to the thread event list
142  */
143 void lmgr_add_event_p(const char *comment, intptr_t user_data, int32_t flags, const char *file, int32_t line);
144 #define lmgr_add_event(c, u) lmgr_add_event_p(c, u, 0, __FILE__, __LINE__)
145 #define lmgr_add_event_flag(c, u, f) lmgr_add_event_p(c, u, (f), __FILE__, __LINE__)
146
147 /*
148  * Search a deadlock after a fatal signal
149  * no lock are granted, so the program must be
150  * stopped.
151  */
152 bool lmgr_detect_deadlock_unlocked();
153
154 /*
155  * This function will run your thread with lmgr_init_thread() and
156  * lmgr_cleanup_thread().
157  */
158 int lmgr_thread_create(pthread_t *thread,
159                        const pthread_attr_t *attr,
160                        void *(*start_routine)(void*), void *arg);
161
162 /*
163  * Can use SAFEKILL to check if the argument is a valid threadid
164  */
165 int bthread_kill(pthread_t thread, int sig,
166                  const char *file="*unknown*", int line=0);
167
168 #define BTHREAD_MUTEX_NO_PRIORITY      {PTHREAD_MUTEX_INITIALIZER, 0}
169 #define BTHREAD_MUTEX_INITIALIZER      BTHREAD_MUTEX_NO_PRIORITY
170
171 /* Define USE_LOCKMGR_PRIORITY to detect mutex wrong order */
172 #ifdef USE_LOCKMGR_PRIORITY
173 # define BTHREAD_MUTEX_PRIORITY(p)       {PTHREAD_MUTEX_INITIALIZER, p}
174 #else
175 # define BTHREAD_MUTEX_PRIORITY(p)       BTHREAD_MUTEX_NO_PRIORITY
176 #endif
177
178 #define bthread_mutex_lock(x)      bthread_mutex_lock_p(x, __FILE__, __LINE__)
179 #define bthread_mutex_unlock(x)    bthread_mutex_unlock_p(x, __FILE__, __LINE__)
180 #define bthread_cond_wait(x,y)     bthread_cond_wait_p(x,y, __FILE__, __LINE__)
181 #define bthread_cond_timedwait(x,y,z) bthread_cond_timedwait_p(x,y,z, __FILE__, __LINE__)
182
183 /*
184  * Define _LOCKMGR_COMPLIANT to use real pthread functions
185  */
186 #define real_P(x) lmgr_p(&(x))
187 #define real_V(x) lmgr_v(&(x))
188
189 #ifdef _LOCKMGR_COMPLIANT
190 # define P(x) lmgr_p(&(x))
191 # define V(x) lmgr_v(&(x))
192 #else
193 # define P(x)                   bthread_mutex_lock_p(&(x), __FILE__, __LINE__)
194 # define V(x)                   bthread_mutex_unlock_p(&(x), __FILE__, __LINE__)
195 # define pthread_create(a, b, c, d)      lmgr_thread_create(a,b,c,d)
196 # define pthread_mutex_lock(x)           bthread_mutex_lock(x)
197 # define pthread_mutex_unlock(x)         bthread_mutex_unlock(x)
198 # define pthread_cond_wait(x,y)          bthread_cond_wait(x,y)
199 # define pthread_cond_timedwait(x,y,z)   bthread_cond_timedwait(x,y,z)
200
201 # ifdef USE_LOCKMGR_SAFEKILL
202 #  define pthread_kill(a,b)      bthread_kill((a),(b), __FILE__, __LINE__)
203 # endif
204 #endif
205
206 #else   /* _USE_LOCKMGR */
207
208 # define lmgr_detect_deadloc()
209 # define lmgr_add_event_p(c, u, f, l)
210 # define lmgr_add_event(c, u)
211 # define lmgr_dump()
212 # define lmgr_init_thread()
213 # define lmgr_cleanup_thread()
214 # define lmgr_pre_lock(m, prio, f, l)
215 # define lmgr_post_lock()
216 # define lmgr_do_lock(m, prio, f, l)
217 # define lmgr_do_unlock(m)
218 # define lmgr_cleanup_main()
219 # define bthread_mutex_set_priority(a,b)
220 # define bthread_mutex_lock(a)           pthread_mutex_lock(a)
221 # define bthread_mutex_lock_p(a, f, l)   pthread_mutex_lock(a)
222 # define bthread_mutex_unlock(a)         pthread_mutex_unlock(a)
223 # define bthread_mutex_unlock_p(a, f, l) pthread_mutex_unlock(a)
224 # define lmgr_cond_wait(a,b)             pthread_cond_wait(a,b)
225 # define lmgr_cond_timedwait(a,b,c)      pthread_cond_timedwait(a,b,c)
226 # define bthread_mutex_t                 pthread_mutex_t
227 # define P(x) lmgr_p(&(x))
228 # define V(x) lmgr_v(&(x))
229 # define BTHREAD_MUTEX_PRIORITY(p)      PTHREAD_MUTEX_INITIALIZER
230 # define BTHREAD_MUTEX_NO_PRIORITY      PTHREAD_MUTEX_INITIALIZER
231 # define BTHREAD_MUTEX_INITIALIZER      PTHREAD_MUTEX_INITIALIZER
232 # define lmgr_mutex_is_locked(m)        (1)
233 # define bthread_cond_wait_p(w, x, y, z) pthread_cond_wait(w,x)
234
235 #endif  /* _USE_LOCKMGR */
236
237 #endif  /* _LOCKMGR_H */