2 Bacula® - The Network Backup Solution
4 Copyright (C) 2008-2014 Free Software Foundation Europe e.V.
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.
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.
14 Bacula® is a registered trademark of Kern Sibbald.
20 #include "mutex_list.h" /* Manage mutex with priority in a central place */
23 * P and V op that don't use the lock manager (for memory allocation or on
26 void lmgr_p(pthread_mutex_t *m);
27 void lmgr_v(pthread_mutex_t *m);
31 typedef struct bthread_mutex_t
33 pthread_mutex_t mutex;
38 * We decide that a thread won't lock more than LMGR_MAX_LOCK at the same time
40 #define LMGR_MAX_LOCK 32
42 int bthread_cond_wait_p(pthread_cond_t *cond,
43 bthread_mutex_t *mutex,
44 const char *file="*unknown*", int line=0);
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);
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);
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);
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);
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);
69 /* Replacement of pthread_mutex_lock() */
70 int bthread_mutex_lock_p(bthread_mutex_t *m,
71 const char *file="*unknown*", int line=0);
73 /* Replacement of pthread_mutex_unlock() */
74 int bthread_mutex_unlock_p(bthread_mutex_t *m,
75 const char *file="*unknown*", int line=0);
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
81 int lmgr_mutex_is_locked(void *m);
84 * Use them when you want use your lock yourself (ie rwlock)
87 /* Call before requesting the lock */
88 void lmgr_pre_lock(void *m, int prio=0,
89 const char *file="*unknown*", int line=0);
91 /* Call after getting it */
92 void lmgr_post_lock();
94 /* Same as pre+post lock */
95 void lmgr_do_lock(void *m, int prio=0,
96 const char *file="*unknown*", int line=0);
98 /* Call just before releasing the lock */
99 void lmgr_do_unlock(void *m);
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);
105 void bthread_mutex_set_priority(bthread_mutex_t *m, int prio);
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
111 void lmgr_init_thread();
114 * Call this function at the end of the thread
116 void lmgr_cleanup_thread();
119 * Call this at the end of the program, it will release the
120 * global lock manager
122 void lmgr_cleanup_main();
125 * Dump each lmgr_thread_t object to stdout
132 bool lmgr_detect_deadlock();
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 */
141 * Add event to the thread event list
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__)
148 * Search a deadlock after a fatal signal
149 * no lock are granted, so the program must be
152 bool lmgr_detect_deadlock_unlocked();
155 * This function will run your thread with lmgr_init_thread() and
156 * lmgr_cleanup_thread().
158 int lmgr_thread_create(pthread_t *thread,
159 const pthread_attr_t *attr,
160 void *(*start_routine)(void*), void *arg);
163 * Can use SAFEKILL to check if the argument is a valid threadid
165 int bthread_kill(pthread_t thread, int sig,
166 const char *file="*unknown*", int line=0);
168 #define BTHREAD_MUTEX_NO_PRIORITY {PTHREAD_MUTEX_INITIALIZER, 0}
169 #define BTHREAD_MUTEX_INITIALIZER BTHREAD_MUTEX_NO_PRIORITY
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}
175 # define BTHREAD_MUTEX_PRIORITY(p) BTHREAD_MUTEX_NO_PRIORITY
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__)
184 * Define _LOCKMGR_COMPLIANT to use real pthread functions
186 #define real_P(x) lmgr_p(&(x))
187 #define real_V(x) lmgr_v(&(x))
189 #ifdef _LOCKMGR_COMPLIANT
190 # define P(x) lmgr_p(&(x))
191 # define V(x) lmgr_v(&(x))
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)
201 # ifdef USE_LOCKMGR_SAFEKILL
202 # define pthread_kill(a,b) bthread_kill((a),(b), __FILE__, __LINE__)
206 #else /* _USE_LOCKMGR */
208 # define lmgr_detect_deadloc()
209 # define lmgr_add_event_p(c, u, f, l)
210 # define lmgr_add_event(c, u)
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)
235 #endif /* _USE_LOCKMGR */
237 #endif /* _LOCKMGR_H */