]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/lockmgr.h
Merge branch 'master' into basejobv3
[bacula/bacula] / bacula / src / lib / lockmgr.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2008-2008 Free Software Foundation Europe e.V.
5
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.
12
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.
17
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
21    02110-1301, USA.
22
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.
27 */
28
29 #ifndef _LOCKMGR_H
30 #define _LOCKMGR_H 1
31
32 #include "bacula.h"
33
34 /*
35  * P and V op that don't use the lock manager (for memory allocation or on
36  * win32)
37  */
38 void lmgr_p(pthread_mutex_t *m);
39 void lmgr_v(pthread_mutex_t *m);
40
41 #ifdef _USE_LOCKMGR
42
43 /* 
44  * We decide that a thread won't lock more than LMGR_MAX_LOCK at the same time
45  */
46 #define LMGR_MAX_LOCK 32
47
48 /* Not yet working */
49 int lmgr_cond_wait(pthread_cond_t *cond,
50                    pthread_mutex_t *mutex,
51                    const char *file="*unknown*", int line=0);
52
53 /* Replacement of pthread_mutex_lock() */
54 int lmgr_mutex_lock(pthread_mutex_t *m, 
55                     const char *file="*unknown*", int line=0);
56
57 /* Replacement of pthread_mutex_unlock() */
58 int lmgr_mutex_unlock(pthread_mutex_t *m, 
59                       const char *file="*unknown*", int line=0);
60
61 /* 
62  * Use them when you want use your lock yourself (ie rwlock)
63  */
64
65 /* Call before requesting the lock */
66 void lmgr_pre_lock(void *m, const char *file="*unknown*", int line=0);
67
68 /* Call after getting it */ 
69 void lmgr_post_lock();
70
71 /* Same as pre+post lock */
72 void lmgr_do_lock(void *m, const char *file="*unknown*", int line=0);
73
74 /* Call just before releasing the lock */
75 void lmgr_do_unlock(void *m); 
76
77 /*
78  * Each thread have to call this function to put a lmgr_thread_t object
79  * in the stack and be able to call mutex_lock/unlock
80  */
81 void lmgr_init_thread();
82
83 /*
84  * Call this function at the end of the thread
85  */
86 void lmgr_cleanup_thread();
87
88 /*
89  * Call this at the end of the program, it will release the 
90  * global lock manager
91  */
92 void lmgr_cleanup_main();
93
94 /*
95  * Dump each lmgr_thread_t object to stdout
96  */
97 void lmgr_dump();
98
99 /*
100  * Search a deadlock
101  */
102 bool lmgr_detect_deadlock();
103
104 /*
105  * Search a deadlock after a fatal signal
106  * no lock are granted, so the program must be
107  * stopped.
108  */
109 bool lmgr_detect_deadlock_unlocked();
110
111 /*
112  * This function will run your thread with lmgr_init_thread() and
113  * lmgr_cleanup_thread().
114  */
115 int lmgr_thread_create(pthread_t *thread,
116                        const pthread_attr_t *attr,
117                        void *(*start_routine)(void*), void *arg);
118
119 /* 
120  * Define _LOCKMGR_COMPLIANT to use real pthread functions
121  */
122
123 #ifdef _LOCKMGR_COMPLIANT
124 # define P(x) lmgr_p(&(x))
125 # define V(x) lmgr_v(&(x))
126 #else
127 # define P(x) lmgr_mutex_lock(&(x), __FILE__, __LINE__)
128 # define V(x) lmgr_mutex_unlock(&(x), __FILE__, __LINE__)
129 # define pthread_mutex_lock(x)       lmgr_mutex_lock(x, __FILE__, __LINE__)
130 # define pthread_mutex_unlock(x)     lmgr_mutex_unlock(x, __FILE__, __LINE__)
131 # define pthread_cond_wait(x,y)      lmgr_cond_wait(x,y, __FILE__, __LINE__)
132 # define pthread_create(a, b, c, d)  lmgr_thread_create(a,b,c,d)
133 #endif
134
135 #else   /* _USE_LOCKMGR */
136
137 # define lmgr_detect_deadloc()
138 # define lmgr_dump()
139 # define lmgr_init_thread()
140 # define lmgr_cleanup_thread()
141 # define lmgr_pre_lock(m)
142 # define lmgr_post_lock()
143 # define lmgr_do_lock(m)
144 # define lmgr_do_unlock(m)
145 # define lmgr_cleanup_main()
146 # define P(x) lmgr_p(&(x))
147 # define V(x) lmgr_v(&(x))
148
149 #endif  /* _USE_LOCKMGR */
150
151 #endif  /* _LOCKMGR_H */