]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/lockmgr.h
Tweak mutex order for SD
[bacula/bacula] / bacula / src / lib / lockmgr.h
index 83d9b3c879bf0ed73fd95c9300b94dc6075515ed..03210a9880001b8e242988efa362fa4e53d44ac8 100644 (file)
@@ -29,7 +29,7 @@
 #ifndef _LOCKMGR_H
 #define _LOCKMGR_H 1
 
-#include "bacula.h"
+#include "mutex_list.h"     /* Manage mutex with priority in a central place */
 
 /*
  * P and V op that don't use the lock manager (for memory allocation or on
@@ -40,30 +40,75 @@ void lmgr_v(pthread_mutex_t *m);
 
 #ifdef _USE_LOCKMGR
 
+typedef struct bthread_mutex_t
+{
+   pthread_mutex_t mutex;
+   int priority;
+} bthread_mutex_t;
+
 /* 
  * We decide that a thread won't lock more than LMGR_MAX_LOCK at the same time
  */
 #define LMGR_MAX_LOCK 32
 
-/* Not yet working */
-int lmgr_cond_wait(pthread_cond_t *cond,
-                   pthread_mutex_t *mutex);
+int bthread_cond_wait_p(pthread_cond_t *cond,
+                        bthread_mutex_t *mutex,
+                        const char *file="*unknown*", int line=0);
+
+int bthread_cond_timedwait_p(pthread_cond_t *cond,
+                             bthread_mutex_t *mutex,
+                             const struct timespec * abstime,
+                             const char *file="*unknown*", int line=0);
+
+/* Same with real pthread_mutex_t */
+int bthread_cond_wait_p(pthread_cond_t *cond,
+                        pthread_mutex_t *mutex,
+                        const char *file="*unknown*", int line=0);
+
+int bthread_cond_timedwait_p(pthread_cond_t *cond,
+                             pthread_mutex_t *mutex,
+                             const struct timespec * abstime,
+                             const char *file="*unknown*", int line=0);
+
+/* Replacement of pthread_mutex_lock()  but with real pthread_mutex_t */
+int bthread_mutex_lock_p(pthread_mutex_t *m, 
+                         const char *file="*unknown*", int line=0);
+
+/* Replacement for pthread_mutex_unlock() but with real pthread_mutex_t */
+int bthread_mutex_unlock_p(pthread_mutex_t *m,
+                           const char *file="*unknown*", int line=0);
 
 /* Replacement of pthread_mutex_lock() */
-int lmgr_mutex_lock(pthread_mutex_t *m, 
-                    const char *file="*unknown*", int line=0);
+int bthread_mutex_lock_p(bthread_mutex_t *m, 
+                         const char *file="*unknown*", int line=0);
 
 /* Replacement of pthread_mutex_unlock() */
-int lmgr_mutex_unlock(pthread_mutex_t *m, 
-                      const char *file="*unknown*", int line=0);
+int bthread_mutex_unlock_p(bthread_mutex_t *m, 
+                           const char *file="*unknown*", int line=0);
 
 /* 
  * Use them when you want use your lock yourself (ie rwlock)
  */
-void lmgr_pre_lock(void *m);    /* Call before requesting the lock */
-void lmgr_post_lock();          /* Call after getting it */
-void lmgr_do_lock(void *m);     /* Same as pre+post lock */
-void lmgr_do_unlock(void *m);   /* Call just before releasing the lock */
+
+/* Call before requesting the lock */
+void lmgr_pre_lock(void *m, int prio=0,
+                   const char *file="*unknown*", int line=0);
+
+/* Call after getting it */ 
+void lmgr_post_lock();
+
+/* Same as pre+post lock */
+void lmgr_do_lock(void *m, int prio=0, 
+                  const char *file="*unknown*", int line=0);
+
+/* Call just before releasing the lock */
+void lmgr_do_unlock(void *m); 
+
+/* We use C++ mangling to make integration eaysier */
+int pthread_mutex_init(bthread_mutex_t *m, const pthread_mutexattr_t *attr);
+int pthread_mutex_destroy(bthread_mutex_t *m);
+
+void bthread_mutex_set_priority(bthread_mutex_t *m, int prio);
 
 /*
  * Each thread have to call this function to put a lmgr_thread_t object
@@ -107,6 +152,22 @@ int lmgr_thread_create(pthread_t *thread,
                        const pthread_attr_t *attr,
                        void *(*start_routine)(void*), void *arg);
 
+#define BTHREAD_MUTEX_NO_PRIORITY      {PTHREAD_MUTEX_INITIALIZER, 0}
+#define BTHREAD_MUTEX_INITIALIZER      BTHREAD_MUTEX_NO_PRIORITY
+
+/* Define USE_LOCKMGR_PRIORITY to detect mutex wrong order */
+#ifdef USE_LOCKMGR_PRIORITY
+# define BTHREAD_MUTEX_PRIORITY(p)       {PTHREAD_MUTEX_INITIALIZER, p}
+#else
+# define BTHREAD_MUTEX_PRIORITY(p)       BTHREAD_MUTEX_NO_PRIORITY
+# define bthread_mutex_set_priority(x,y)
+#endif
+
+#define bthread_mutex_lock(x)      bthread_mutex_lock_p(x, __FILE__, __LINE__)
+#define bthread_mutex_unlock(x)    bthread_mutex_unlock_p(x, __FILE__, __LINE__)
+#define bthread_cond_wait(x,y)     bthread_cond_wait_p(x,y, __FILE__, __LINE__)
+#define bthread_cond_timedwait(x,y,z) bthread_cond_timedwait_p(x,y,z, __FILE__, __LINE__)
+
 /* 
  * Define _LOCKMGR_COMPLIANT to use real pthread functions
  */
@@ -115,12 +176,13 @@ int lmgr_thread_create(pthread_t *thread,
 # define P(x) lmgr_p(&(x))
 # define V(x) lmgr_v(&(x))
 #else
-# define P(x) lmgr_mutex_lock(&(x), __FILE__, __LINE__)
-# define V(x) lmgr_mutex_unlock(&(x), __FILE__, __LINE__)
-# define pthread_mutex_lock(x)       lmgr_mutex_lock(x, __FILE__, __LINE__)
-# define pthread_mutex_unlock(x)     lmgr_mutex_unlock(x, __FILE__, __LINE__)
-# define pthread_cond_wait(x,y)      lmgr_cond_wait(x,y)
-# define pthread_create(a, b, c, d)  lmgr_thread_create(a,b,c,d)
+# define P(x)                   bthread_mutex_lock_p(&(x), __FILE__, __LINE__)
+# define V(x)                   bthread_mutex_unlock_p(&(x), __FILE__, __LINE__)
+# define pthread_create(a, b, c, d)      lmgr_thread_create(a,b,c,d)
+# define pthread_mutex_lock(x)           bthread_mutex_lock(x)
+# define pthread_mutex_unlock(x)         bthread_mutex_unlock(x)
+# define pthread_cond_wait(x,y)          bthread_cond_wait(x,y)
+# define pthread_cond_timedwait(x,y,z)   bthread_cond_timedwait(x,y,z)
 #endif
 
 #else   /* _USE_LOCKMGR */
@@ -129,14 +191,22 @@ int lmgr_thread_create(pthread_t *thread,
 # define lmgr_dump()
 # define lmgr_init_thread()
 # define lmgr_cleanup_thread()
-# define lmgr_pre_lock(m)
+# define lmgr_pre_lock(m, prio, f, l)
 # define lmgr_post_lock()
-# define lmgr_do_lock(m)
+# define lmgr_do_lock(m, prio, f, l)
 # define lmgr_do_unlock(m)
 # define lmgr_cleanup_main()
+# define bthread_mutex_set_priority(a)
+# define bthread_mutex_lock(a)          pthread_mutex_lock(a)
+# define bthread_mutex_unlock(a)        pthread_mutex_unlock(a)
+# define lmgr_cond_wait(a,b)            pthread_cond_wait(a,b)
+# define lmgr_cond_timedwait(a,b,c)     pthread_cond_timedwait(a,b,c)
+# define bthread_mutex_t                pthread_mutex_t
 # define P(x) lmgr_p(&(x))
 # define V(x) lmgr_v(&(x))
-
+# define BTHREAD_MUTEX_PRIORITY(p)      PTHREAD_MUTEX_INITIALIZER
+# define BTHREAD_MUTEX_NO_PRIORITY      PTHREAD_MUTEX_INITIALIZER
+# define BTHREAD_MUTEX_INITIALIZER      PTHREAD_MUTEX_INITIALIZER
 #endif  /* _USE_LOCKMGR */
 
 #endif  /* _LOCKMGR_H */