2 Bacula® - The Network Backup Solution
4 Copyright (C) 2008-2008 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 two of the GNU 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 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.
33 * P and V op that don't use the lock manager (for memory allocation or on
36 void lmgr_p(pthread_mutex_t *m);
37 void lmgr_v(pthread_mutex_t *m);
41 typedef struct bthread_mutex_t
43 pthread_mutex_t mutex;
48 * We decide that a thread won't lock more than LMGR_MAX_LOCK at the same time
50 #define LMGR_MAX_LOCK 32
52 int bthread_cond_wait_p(pthread_cond_t *cond,
53 bthread_mutex_t *mutex,
54 const char *file="*unknown*", int line=0);
56 int bthread_cond_timedwait_p(pthread_cond_t *cond,
57 bthread_mutex_t *mutex,
58 const struct timespec * abstime,
59 const char *file="*unknown*", int line=0);
61 /* Same with real pthread_mutex_t */
62 int bthread_cond_wait_p(pthread_cond_t *cond,
63 pthread_mutex_t *mutex,
64 const char *file="*unknown*", int line=0);
66 int bthread_cond_timedwait_p(pthread_cond_t *cond,
67 pthread_mutex_t *mutex,
68 const struct timespec * abstime,
69 const char *file="*unknown*", int line=0);
71 /* Replacement of pthread_mutex_lock() but with real pthread_mutex_t */
72 int bthread_mutex_lock_p(pthread_mutex_t *m,
73 const char *file="*unknown*", int line=0);
75 /* Replacement for pthread_mutex_unlock() but with real pthread_mutex_t */
76 int bthread_mutex_unlock_p(pthread_mutex_t *m,
77 const char *file="*unknown*", int line=0);
79 /* Replacement of pthread_mutex_lock() */
80 int bthread_mutex_lock_p(bthread_mutex_t *m,
81 const char *file="*unknown*", int line=0);
83 /* Replacement of pthread_mutex_unlock() */
84 int bthread_mutex_unlock_p(bthread_mutex_t *m,
85 const char *file="*unknown*", int line=0);
88 * Use them when you want use your lock yourself (ie rwlock)
91 /* Call before requesting the lock */
92 void lmgr_pre_lock(void *m, int prio=0,
93 const char *file="*unknown*", int line=0);
95 /* Call after getting it */
96 void lmgr_post_lock();
98 /* Same as pre+post lock */
99 void lmgr_do_lock(void *m, int prio=0,
100 const char *file="*unknown*", int line=0);
102 /* Call just before releasing the lock */
103 void lmgr_do_unlock(void *m);
105 /* We use C++ mangling to make integration eaysier */
106 int pthread_mutex_init(bthread_mutex_t *m, const pthread_mutexattr_t *attr);
107 int pthread_mutex_destroy(bthread_mutex_t *m);
109 void bthread_mutex_set_priority(bthread_mutex_t *m, int prio);
112 * Each thread have to call this function to put a lmgr_thread_t object
113 * in the stack and be able to call mutex_lock/unlock
115 void lmgr_init_thread();
118 * Call this function at the end of the thread
120 void lmgr_cleanup_thread();
123 * Call this at the end of the program, it will release the
124 * global lock manager
126 void lmgr_cleanup_main();
129 * Dump each lmgr_thread_t object to stdout
136 bool lmgr_detect_deadlock();
139 * Search a deadlock after a fatal signal
140 * no lock are granted, so the program must be
143 bool lmgr_detect_deadlock_unlocked();
146 * This function will run your thread with lmgr_init_thread() and
147 * lmgr_cleanup_thread().
149 int lmgr_thread_create(pthread_t *thread,
150 const pthread_attr_t *attr,
151 void *(*start_routine)(void*), void *arg);
154 * Define _LOCKMGR_COMPLIANT to use real pthread functions
157 #define BTHREAD_MUTEX_PRIORITY(p) {PTHREAD_MUTEX_INITIALIZER, p}
158 #define BTHREAD_MUTEX_NO_PRIORITY {PTHREAD_MUTEX_INITIALIZER, 0}
159 #define BTHREAD_MUTEX_PRIORITY_1 {PTHREAD_MUTEX_INITIALIZER, 1}
160 #define BTHREAD_MUTEX_PRIORITY_2 {PTHREAD_MUTEX_INITIALIZER, 2}
161 #define BTHREAD_MUTEX_PRIORITY_3 {PTHREAD_MUTEX_INITIALIZER, 3}
162 #define BTHREAD_MUTEX_PRIORITY_4 {PTHREAD_MUTEX_INITIALIZER, 4}
163 #define BTHREAD_MUTEX_INITIALIZER BTHREAD_MUTEX_NO_PRIORITY
164 #define bthread_mutex_lock(x) bthread_mutex_lock_p(x, __FILE__, __LINE__)
165 #define bthread_mutex_unlock(x) bthread_mutex_unlock_p(x, __FILE__, __LINE__)
166 #define bthread_cond_wait(x,y) bthread_cond_wait_p(x,y, __FILE__, __LINE__)
167 #define bthread_cond_timedwait(x,y,z) bthread_cond_timedwait_p(x,y,z, __FILE__, __LINE__)
169 #ifdef _LOCKMGR_COMPLIANT
170 # define P(x) lmgr_p(&(x))
171 # define V(x) lmgr_v(&(x))
173 # define P(x) bthread_mutex_lock_p(&(x), __FILE__, __LINE__)
174 # define V(x) bthread_mutex_unlock_p(&(x), __FILE__, __LINE__)
175 # define pthread_create(a, b, c, d) lmgr_thread_create(a,b,c,d)
176 # define pthread_mutex_lock(x) bthread_mutex_lock(x)
177 # define pthread_mutex_unlock(x) bthread_mutex_unlock(x)
178 # define pthread_cond_wait(x,y) bthread_cond_wait(x,y)
179 # define pthread_cond_timedwait(x,y,z) bthread_cond_timedwait(x,y,z)
182 #else /* _USE_LOCKMGR */
184 # define lmgr_detect_deadloc()
186 # define lmgr_init_thread()
187 # define lmgr_cleanup_thread()
188 # define lmgr_pre_lock(m, f, l)
189 # define lmgr_post_lock()
190 # define lmgr_do_lock(m, f, l)
191 # define lmgr_do_unlock(m)
192 # define lmgr_cleanup_main()
193 # define bthread_mutex_set_priority(a)
194 # define bthread_mutex_lock(a) pthread_mutex_lock(a)
195 # define bthread_mutex_unlock(a) pthread_mutex_unlock(a)
196 # define lmgr_cond_wait(a,b) pthread_cond_wait(a,b)
197 # define lmgr_cond_timedwait(a,b,c) pthread_cond_timedwait(a,b,c)
198 # define bthread_mutex_t pthread_mutex_t
199 # define P(x) lmgr_p(&(x))
200 # define V(x) lmgr_v(&(x))
201 # define BTHREAD_MUTEX_PRIORITY(p) PTHREAD_MUTEX_INITIALIZER
202 # define BTHREAD_MUTEX_NO_PRIORITY PTHREAD_MUTEX_INITIALIZER
203 # define BTHREAD_MUTEX_PRIORITY_1 PTHREAD_MUTEX_INITIALIZER
204 # define BTHREAD_MUTEX_PRIORITY_2 PTHREAD_MUTEX_INITIALIZER
205 # define BTHREAD_MUTEX_PRIORITY_3 PTHREAD_MUTEX_INITIALIZER
206 # define BTHREAD_MUTEX_PRIORITY_4 PTHREAD_MUTEX_INITIALIZER
207 # define BTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
208 #endif /* _USE_LOCKMGR */
210 #include "mutex_list.h" /* Manage mutex with priority in a central place */
212 #endif /* _LOCKMGR_H */