]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/lockmgr.c
Cleanup attribute catalog insert errors + cancel SD only once
[bacula/bacula] / bacula / src / lib / lockmgr.c
index 2756cf825fd4d46d4e05f53f06ecf714e22412f9..a790999c1d29de124bd12626bb20e028d6db5ece 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2008-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2008-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
@@ -243,12 +243,14 @@ public:
    }
 
    void _dump(FILE *fp) {
-      fprintf(fp, "threadid=0x%x max=%i current=%i\n", (int)thread_id, max, current);
+      fprintf(fp, "threadid=%p max=%i current=%i\n", 
+              (void *)thread_id, max, current);
       for(int i=0; i<=current; i++) {
-         fprintf(fp, "   lock=%p state=%c %s:%i\n", 
-               lock_list[i].lock, lock_list[i].state,
-               lock_list[i].file, lock_list[i].line);
-      }
+         fprintf(fp, "   lock=%p state=%s %s:%i\n", 
+                 lock_list[i].lock, 
+                 (lock_list[i].state=='W')?"Wanted ":"Granted",
+                 lock_list[i].file, lock_list[i].line);
+      } 
    }
 
    void dump(FILE *fp) {
@@ -352,7 +354,7 @@ class lmgr_dummy_thread_t: public lmgr_thread_t
 pthread_once_t key_lmgr_once = PTHREAD_ONCE_INIT; 
 static pthread_key_t lmgr_key;  /* used to get lgmr_thread_t object */
 
-static dlist *global_mgr=NULL;  /* used to store all lgmr_thread_t objects */
+static dlist *global_mgr = NULL;  /* used to store all lgmr_thread_t objects */
 static pthread_mutex_t lmgr_global_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_t undertaker;
 
@@ -466,8 +468,8 @@ bool lmgr_detect_deadlock()
 
 /*
  * !!! WARNING !!! 
- * Use this function only after a fatal signal
- * We don't use any lock to display information
+ * Use this function is used only after a fatal signal
+ * We don't use locking to display the information
  */
 void dbg_print_lock(FILE *fp)
 {
@@ -522,7 +524,7 @@ void *check_deadlock(void *)
 }
 
 /* This object is used when LMGR is not initialized */
-lmgr_dummy_thread_t dummy_lmgr;
+static lmgr_dummy_thread_t dummy_lmgr;
 
 /*
  * Retrieve the lmgr_thread_t object from the stack
@@ -552,7 +554,8 @@ void create_lmgr_key()
    lmgr_thread_t *n=NULL;
    global_mgr = New(dlist(n, &n->link));
 
-   if (pthread_create(&undertaker, NULL, check_deadlock, NULL) != 0) {
+   status = pthread_create(&undertaker, NULL, check_deadlock, NULL);
+   if (status != 0) {
       berrno be;
       Pmsg1(000, _("pthread_create failed: ERR=%s\n"),
             be.bstrerror(status));
@@ -600,12 +603,15 @@ void lmgr_cleanup_main()
 {
    dlist *temp;
    
+   if (!global_mgr) {
+      return;
+   }
    pthread_cancel(undertaker);
    lmgr_cleanup_thread();
    pthread_mutex_lock(&lmgr_global_mutex);
    {
       temp = global_mgr;
-      global_mgr=NULL;
+      global_mgr = NULL;
       delete temp;
    }
    pthread_mutex_unlock(&lmgr_global_mutex);
@@ -637,13 +643,14 @@ int lmgr_mutex_unlock(pthread_mutex_t *m, const char *file, int line)
 /* TODO: check this
  */
 int lmgr_cond_wait(pthread_cond_t *cond,
-                   pthread_mutex_t *mutex)
+                   pthread_mutex_t *mutex,
+                   const char *file, int line)
 {
    int ret;
    lmgr_thread_t *self = lmgr_get_thread_info();
-   self->do_V(mutex);   
+   self->do_V(mutex, file, line);   
    ret = pthread_cond_wait(cond, mutex);
-   self->pre_P(mutex);
+   self->pre_P(mutex, file, line);
    self->post_P();
    return ret;
 }
@@ -655,10 +662,10 @@ int lmgr_cond_wait(pthread_cond_t *cond,
  * pthread_mutex_lock(m);
  * lmgr_post_lock(m);
  */
-void lmgr_pre_lock(void *m)
+void lmgr_pre_lock(void *m, const char *file, int line)
 {
    lmgr_thread_t *self = lmgr_get_thread_info();
-   self->pre_P(m);
+   self->pre_P(m, file, line);
 }
 
 /*
@@ -673,10 +680,10 @@ void lmgr_post_lock()
 /*
  * Do directly pre_P and post_P (used by trylock)
  */
-void lmgr_do_lock(void *m)
+void lmgr_do_lock(void *m, const char *file, int line)
 {
    lmgr_thread_t *self = lmgr_get_thread_info();
-   self->pre_P(m);
+   self->pre_P(m, file, line);
    self->post_P();
 }
 
@@ -716,19 +723,21 @@ int lmgr_thread_create(pthread_t *thread,
                        const pthread_attr_t *attr,
                        void *(*start_routine)(void*), void *arg)
 {
+   /* lmgr should be active (lmgr_init_thread() call in main()) */
+   ASSERT(lmgr_is_active());
    /* Will be freed by the child */
    lmgr_thread_arg_t *a = (lmgr_thread_arg_t*) malloc(sizeof(lmgr_thread_arg_t));
    a->start_routine = start_routine;
    a->arg = arg;
-   return pthread_create(thread, attr, lmgr_thread_launcher, a);   
+   return pthread_create(thread, attr, lmgr_thread_launcher, a);
 }
 
 #else  /* _USE_LOCKMGR */
 
 /*
  * !!! WARNING !!! 
- * Use this function only after a fatal signal
- * We don't use any lock to display information
+ * Use this function is used only after a fatal signal
+ * We don't use locking to display information
  */
 void dbg_print_lock(FILE *fp)
 {