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