2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2016 Kern Sibbald
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.
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 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
23 #include "mutex_list.h" /* Manage mutex with priority in a central place */
26 * P and V op that don't use the lock manager (for memory allocation or on
29 void lmgr_p(pthread_mutex_t *m);
30 void lmgr_v(pthread_mutex_t *m);
33 * Get integer thread id
35 intptr_t bthread_get_thread_id();
39 typedef struct bthread_mutex_t
41 pthread_mutex_t mutex;
46 * We decide that a thread won't lock more than LMGR_MAX_LOCK at the same time
48 #define LMGR_MAX_LOCK 32
50 int bthread_cond_wait_p(pthread_cond_t *cond,
51 bthread_mutex_t *mutex,
52 const char *file="*unknown*", int line=0);
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);
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);
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);
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);
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);
77 /* Replacement of pthread_mutex_lock() */
78 int bthread_mutex_lock_p(bthread_mutex_t *m,
79 const char *file="*unknown*", int line=0);
81 /* Replacement of pthread_mutex_unlock() */
82 int bthread_mutex_unlock_p(bthread_mutex_t *m,
83 const char *file="*unknown*", int line=0);
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
89 int lmgr_mutex_is_locked(void *m);
92 * Use them when you want use your lock yourself (ie rwlock)
95 /* Call before requesting the lock */
96 void lmgr_pre_lock(void *m, int prio=0,
97 const char *file="*unknown*", int line=0);
99 /* Call after getting it */
100 void lmgr_post_lock();
102 /* Same as pre+post lock */
103 void lmgr_do_lock(void *m, int prio=0,
104 const char *file="*unknown*", int line=0);
106 /* Call just before releasing the lock */
107 void lmgr_do_unlock(void *m);
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);
113 void bthread_mutex_set_priority(bthread_mutex_t *m, int prio);
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
119 void lmgr_init_thread();
122 * Call this function at the end of the thread
124 void lmgr_cleanup_thread();
127 * Call this at the end of the program, it will release the
128 * global lock manager
130 void lmgr_cleanup_main();
133 * Dump each lmgr_thread_t object to stdout
140 bool lmgr_detect_deadlock();
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 */
149 * Add event to the thread event list
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__)
156 * Search a deadlock after a fatal signal
157 * no lock are granted, so the program must be
160 bool lmgr_detect_deadlock_unlocked();
163 * This function will run your thread with lmgr_init_thread() and
164 * lmgr_cleanup_thread().
166 int lmgr_thread_create(pthread_t *thread,
167 const pthread_attr_t *attr,
168 void *(*start_routine)(void*), void *arg);
171 * Can use SAFEKILL to check if the argument is a valid threadid
173 int bthread_kill(pthread_t thread, int sig,
174 const char *file="*unknown*", int line=0);
176 #define BTHREAD_MUTEX_NO_PRIORITY {PTHREAD_MUTEX_INITIALIZER, 0}
177 #define BTHREAD_MUTEX_INITIALIZER BTHREAD_MUTEX_NO_PRIORITY
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}
183 # define BTHREAD_MUTEX_PRIORITY(p) BTHREAD_MUTEX_NO_PRIORITY
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__)
192 * Define LOCKMGR_COMPLIANT to use real pthread functions
194 #define real_P(x) lmgr_p(&(x))
195 #define real_V(x) lmgr_v(&(x))
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)
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)
213 # ifdef USE_LOCKMGR_SAFEKILL
214 # define pthread_kill(a,b) bthread_kill((a),(b), __FILE__, __LINE__)
218 #else /* !USE_LOCKMGR */
220 # define lmgr_detect_deadloc()
221 # define lmgr_add_event_p(c, u, f, l)
222 # define lmgr_add_event(c, u)
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 */
250 #endif /* LOCKMGR_H */