]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/lockmgr.h
ebl Add a new lock manager that can detect deadlock situation
[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
52 /* Replacement of pthread_mutex_lock() */
53 int lmgr_mutex_lock(pthread_mutex_t *m, 
54                     const char *file="*unknown*", int line=0);
55
56 /* Replacement of pthread_mutex_unlock() */
57 int lmgr_mutex_unlock(pthread_mutex_t *m, 
58                       const char *file="*unknown*", int line=0);
59
60 /* 
61  * Use them when you want use your lock yourself (ie rwlock)
62  */
63 void lmgr_pre_lock(void *m);    /* Call before requesting the lock */
64 void lmgr_post_lock();          /* Call after getting it */
65 void lmgr_do_lock(void *m);     /* Same as pre+post lock */
66 void lmgr_do_unlock(void *m);   /* Call just before releasing the lock */
67
68 /*
69  * Each thread have to call this function to put a lmgr_thread_t object
70  * in the stack and be able to call mutex_lock/unlock
71  */
72 void lmgr_init_thread();
73
74 /*
75  * Call this function at the end of the thread
76  */
77 void lmgr_cleanup_thread();
78
79 /*
80  * Call this at the end of the program, it will release the 
81  * global lock manager
82  */
83 void lmgr_cleanup_main();
84
85 /*
86  * Dump each lmgr_thread_t object to stdout
87  */
88 void lmgr_dump();
89
90 /*
91  * Search a deadlock
92  */
93 bool lmgr_detect_deadlock();
94
95 /*
96  * Search a deadlock after a fatal signal
97  * no lock are granted, so the program must be
98  * stopped.
99  */
100 bool lmgr_detect_deadlock_unlocked();
101
102 /*
103  * This function will run your thread with lmgr_init_thread() and
104  * lmgr_cleanup_thread().
105  */
106 int lmgr_thread_create(pthread_t *thread,
107                        const pthread_attr_t *attr,
108                        void *(*start_routine)(void*), void *arg);
109
110 /* 
111  * Define _LOCKMGR_COMPLIANT to use real pthread functions
112  */
113
114 #ifdef _LOCKMGR_COMPLIANT
115 # define P(x) lmgr_p(&(x))
116 # define V(x) lmgr_v(&(x))
117 #else
118 # define P(x) lmgr_mutex_lock(&(x), __FILE__, __LINE__)
119 # define V(x) lmgr_mutex_unlock(&(x), __FILE__, __LINE__)
120 # define pthread_mutex_lock(x)       lmgr_mutex_lock(x, __FILE__, __LINE__)
121 # define pthread_mutex_unlock(x)     lmgr_mutex_unlock(x, __FILE__, __LINE__)
122 # define pthread_cond_wait(x,y)      lmgr_cond_wait(x,y)
123 # define pthread_create(a, b, c, d)  lmgr_thread_create(a,b,c,d)
124 #endif
125
126 #else   /* _USE_LOCKMGR */
127
128 # define lmgr_detect_deadloc()
129 # define lmgr_dump()
130 # define lmgr_init_thread()
131 # define lmgr_cleanup_thread()
132 # define lmgr_pre_lock(m)
133 # define lmgr_post_lock()
134 # define lmgr_do_lock(m)
135 # define lmgr_do_unlock(m)
136 # define lmgr_cleanup_main()
137 # define P(x) lmgr_p(&(x))
138 # define V(x) lmgr_v(&(x))
139
140 #endif  /* _USE_LOCKMGR */
141
142 #endif  /* _LOCKMGR_H */