2 Bacula® - The Network Backup Solution
4 Copyright (C) 2008-2010 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation, which is
11 listed in the file LICENSE.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
32 #include "mutex_list.h" /* Manage mutex with priority in a central place */
35 * P and V op that don't use the lock manager (for memory allocation or on
38 void lmgr_p(pthread_mutex_t *m);
39 void lmgr_v(pthread_mutex_t *m);
43 typedef struct bthread_mutex_t
45 pthread_mutex_t mutex;
50 * We decide that a thread won't lock more than LMGR_MAX_LOCK at the same time
52 #define LMGR_MAX_LOCK 32
54 int bthread_cond_wait_p(pthread_cond_t *cond,
55 bthread_mutex_t *mutex,
56 const char *file="*unknown*", int line=0);
58 int bthread_cond_timedwait_p(pthread_cond_t *cond,
59 bthread_mutex_t *mutex,
60 const struct timespec * abstime,
61 const char *file="*unknown*", int line=0);
63 /* Same with real pthread_mutex_t */
64 int bthread_cond_wait_p(pthread_cond_t *cond,
65 pthread_mutex_t *mutex,
66 const char *file="*unknown*", int line=0);
68 int bthread_cond_timedwait_p(pthread_cond_t *cond,
69 pthread_mutex_t *mutex,
70 const struct timespec * abstime,
71 const char *file="*unknown*", int line=0);
73 /* Replacement of pthread_mutex_lock() but with real pthread_mutex_t */
74 int bthread_mutex_lock_p(pthread_mutex_t *m,
75 const char *file="*unknown*", int line=0);
77 /* Replacement for pthread_mutex_unlock() but with real pthread_mutex_t */
78 int bthread_mutex_unlock_p(pthread_mutex_t *m,
79 const char *file="*unknown*", int line=0);
81 /* Replacement of pthread_mutex_lock() */
82 int bthread_mutex_lock_p(bthread_mutex_t *m,
83 const char *file="*unknown*", int line=0);
85 /* Replacement of pthread_mutex_unlock() */
86 int bthread_mutex_unlock_p(bthread_mutex_t *m,
87 const char *file="*unknown*", int line=0);
89 /* Test if this mutex is locked by the current thread
90 * 0 - not locked by the current thread
91 * 1 - locked by the current thread
93 int lmgr_mutex_is_locked(void *m);
96 * Use them when you want use your lock yourself (ie rwlock)
99 /* Call before requesting the lock */
100 void lmgr_pre_lock(void *m, int prio=0,
101 const char *file="*unknown*", int line=0);
103 /* Call after getting it */
104 void lmgr_post_lock();
106 /* Same as pre+post lock */
107 void lmgr_do_lock(void *m, int prio=0,
108 const char *file="*unknown*", int line=0);
110 /* Call just before releasing the lock */
111 void lmgr_do_unlock(void *m);
113 /* We use C++ mangling to make integration eaysier */
114 int pthread_mutex_init(bthread_mutex_t *m, const pthread_mutexattr_t *attr);
115 int pthread_mutex_destroy(bthread_mutex_t *m);
117 void bthread_mutex_set_priority(bthread_mutex_t *m, int prio);
120 * Each thread have to call this function to put a lmgr_thread_t object
121 * in the stack and be able to call mutex_lock/unlock
123 void lmgr_init_thread();
126 * Call this function at the end of the thread
128 void lmgr_cleanup_thread();
131 * Call this at the end of the program, it will release the
132 * global lock manager
134 void lmgr_cleanup_main();
137 * Dump each lmgr_thread_t object to stdout
144 bool lmgr_detect_deadlock();
147 * Search a deadlock after a fatal signal
148 * no lock are granted, so the program must be
151 bool lmgr_detect_deadlock_unlocked();
154 * This function will run your thread with lmgr_init_thread() and
155 * lmgr_cleanup_thread().
157 int lmgr_thread_create(pthread_t *thread,
158 const pthread_attr_t *attr,
159 void *(*start_routine)(void*), void *arg);
162 * Can use SAFEKILL to check if the argument is a valid threadid
164 int bthread_kill(pthread_t thread, int sig,
165 const char *file="*unknown*", int line=0);
167 #define BTHREAD_MUTEX_NO_PRIORITY {PTHREAD_MUTEX_INITIALIZER, 0}
168 #define BTHREAD_MUTEX_INITIALIZER BTHREAD_MUTEX_NO_PRIORITY
170 /* Define USE_LOCKMGR_PRIORITY to detect mutex wrong order */
171 #ifdef USE_LOCKMGR_PRIORITY
172 # define BTHREAD_MUTEX_PRIORITY(p) {PTHREAD_MUTEX_INITIALIZER, p}
174 # define BTHREAD_MUTEX_PRIORITY(p) BTHREAD_MUTEX_NO_PRIORITY
177 #define bthread_mutex_lock(x) bthread_mutex_lock_p(x, __FILE__, __LINE__)
178 #define bthread_mutex_unlock(x) bthread_mutex_unlock_p(x, __FILE__, __LINE__)
179 #define bthread_cond_wait(x,y) bthread_cond_wait_p(x,y, __FILE__, __LINE__)
180 #define bthread_cond_timedwait(x,y,z) bthread_cond_timedwait_p(x,y,z, __FILE__, __LINE__)
183 * Define _LOCKMGR_COMPLIANT to use real pthread functions
186 #ifdef _LOCKMGR_COMPLIANT
187 # define P(x) lmgr_p(&(x))
188 # define V(x) lmgr_v(&(x))
190 # define P(x) bthread_mutex_lock_p(&(x), __FILE__, __LINE__)
191 # define V(x) bthread_mutex_unlock_p(&(x), __FILE__, __LINE__)
192 # define pthread_create(a, b, c, d) lmgr_thread_create(a,b,c,d)
193 # define pthread_mutex_lock(x) bthread_mutex_lock(x)
194 # define pthread_mutex_unlock(x) bthread_mutex_unlock(x)
195 # define pthread_cond_wait(x,y) bthread_cond_wait(x,y)
196 # define pthread_cond_timedwait(x,y,z) bthread_cond_timedwait(x,y,z)
198 # ifdef USE_LOCKMGR_SAFEKILL
199 # define pthread_kill(a,b) bthread_kill((a),(b), __FILE__, __LINE__)
203 #else /* _USE_LOCKMGR */
205 # define lmgr_detect_deadloc()
207 # define lmgr_init_thread()
208 # define lmgr_cleanup_thread()
209 # define lmgr_pre_lock(m, prio, f, l)
210 # define lmgr_post_lock()
211 # define lmgr_do_lock(m, prio, f, l)
212 # define lmgr_do_unlock(m)
213 # define lmgr_cleanup_main()
214 # define bthread_mutex_set_priority(a,b)
215 # define bthread_mutex_lock(a) pthread_mutex_lock(a)
216 # define bthread_mutex_lock_p(a, f, l) pthread_mutex_lock(a)
217 # define bthread_mutex_unlock(a) pthread_mutex_unlock(a)
218 # define bthread_mutex_unlock_p(a, f, l) pthread_mutex_unlock(a)
219 # define lmgr_cond_wait(a,b) pthread_cond_wait(a,b)
220 # define lmgr_cond_timedwait(a,b,c) pthread_cond_timedwait(a,b,c)
221 # define bthread_mutex_t pthread_mutex_t
222 # define P(x) lmgr_p(&(x))
223 # define V(x) lmgr_v(&(x))
224 # define BTHREAD_MUTEX_PRIORITY(p) PTHREAD_MUTEX_INITIALIZER
225 # define BTHREAD_MUTEX_NO_PRIORITY PTHREAD_MUTEX_INITIALIZER
226 # define BTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
227 # define lmgr_mutex_is_locked(m) (1)
229 #endif /* _USE_LOCKMGR */
231 #endif /* _LOCKMGR_H */