]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/lockmgr.c
Fix typo.
[bacula/bacula] / bacula / src / lib / lockmgr.c
index 24f6aa2c58910650491ae2b712b1bcac8aa6d3b3..5e2e09237c2d6333345dc7a1e35e9900fc22094f 100644 (file)
@@ -1,12 +1,12 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2008-2009 Free Software Foundation Europe e.V.
+   Copyright (C) 2008-2010 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.
    This program is Free Software; you can redistribute it and/or
-   modify it under the terms of version two of the GNU General Public
+   modify it under the terms of version three of the GNU Affero General Public
    License as published by the Free Software Foundation, which is 
    listed in the file LICENSE.
 
@@ -15,7 +15,7 @@
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
 
-   You should have received a copy of the GNU General Public License
+   You should have received a copy of the GNU Affero General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
@@ -30,6 +30,9 @@
   How to use mutex with bad order usage detection
  ------------------------------------------------
 
+ Note: see file mutex_list.h for current mutexes with 
+       defined priorities.
+
  Instead of using:
     pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
     P(mutex);
@@ -37,7 +40,7 @@
     V(mutex);
 
  use:
-    bthread_mutex_t mutex = BTHREAD_MUTEX_PRIORITY_1;
+    bthread_mutex_t mutex = BTHREAD_MUTEX_PRIORITY(1);
     P(mutex);
     ...
     V(mutex);
@@ -47,7 +50,7 @@
  
  With dynamic creation, you can use:
     bthread_mutex_t mutex;
-    pthread_mutex_init(&mutex);     // you can also use bthread_mutex_init()
+    pthread_mutex_init(&mutex);
     bthread_mutex_set_priority(&mutex, 10);
     pthread_mutex_destroy(&mutex);
  
 #undef ASSERT
 #define ASSERT(x) if (!(x)) { \
    char *jcr = NULL; \
-   Pmsg3(000, _("Failed ASSERT: %s\n"), __FILE__, __LINE__, #x); \
+   Pmsg3(000, _("ASSERT failed at %s:%i: %s\n"), __FILE__, __LINE__, #x); \
    jcr[0] = 0; }
 
 #define ASSERT_p(x,f,l) if (!(x)) {              \
    char *jcr = NULL; \
-   Pmsg3(000, _("from %s:%i Failed ASSERT: %s\n"), f, l, #x); \
+   Pmsg3(000, _("ASSERT failed at %s:%i: %s \n"), f, l, #x); \
    jcr[0] = 0; }
 
 /*
@@ -283,9 +286,10 @@ public:
       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=%s %s:%i\n", 
+         fprintf(fp, "   lock=%p state=%s priority=%i %s:%i\n", 
                  lock_list[i].lock, 
                  (lock_list[i].state=='W')?"Wanted ":"Granted",
+                 lock_list[i].priority,
                  lock_list[i].file, lock_list[i].line);
       } 
    }
@@ -302,10 +306,11 @@ public:
     * Call before a lock operation (mark mutex as WANTED)
     */
    virtual void pre_P(void *m, int priority, 
-                      const char *f="*unknown*", int l=0) {
+                      const char *f="*unknown*", int l=0) 
+   {
+      int max_prio = max_priority;
       ASSERT_p(current < LMGR_MAX_LOCK, f, l);
       ASSERT_p(current >= -1, f, l);
-      ASSERT_p(!priority || priority >= max_priority, f, l);
       lmgr_p(&mutex);
       {
          current++;
@@ -319,6 +324,7 @@ public:
          max_priority = MAX(priority, max_priority);
       }
       lmgr_v(&mutex);
+      ASSERT_p(!priority || priority >= max_prio, f, l);
    }
 
    /*
@@ -574,7 +580,7 @@ void *check_deadlock(void *)
       pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old);
       pthread_testcancel();
    }
-   Pmsg0(000, "Undertaker is leaving...\n");
+   Dmsg0(100, "Exit check_deadlock.\n");
    pthread_cleanup_pop(1);
    return NULL;
 }
@@ -682,13 +688,15 @@ void lmgr_cleanup_main()
  */
 void bthread_mutex_set_priority(bthread_mutex_t *m, int prio)
 {
+#ifdef USE_LOCKMGR_PRIORITY
    m->priority = prio;
+#endif
 }
 
 /*
  * Replacement for pthread_mutex_init()
  */
-int bthread_mutex_init(bthread_mutex_t *m, const pthread_mutexattr_t *attr)
+int pthread_mutex_init(bthread_mutex_t *m, const pthread_mutexattr_t *attr)
 {
    m->priority = 0;
    return pthread_mutex_init(&m->mutex, attr);
@@ -697,27 +705,45 @@ int bthread_mutex_init(bthread_mutex_t *m, const pthread_mutexattr_t *attr)
 /*
  * Replacement for pthread_mutex_destroy()
  */
-int bthread_mutex_destroy(bthread_mutex_t *m)
+int pthread_mutex_destroy(bthread_mutex_t *m)
 {
    return pthread_mutex_destroy(&m->mutex);
 }
 
-/*
- * Replacement for pthread_mutex_init() for pthread_mutex_t
+/* 
+ * Replacement for pthread_kill (only with USE_LOCKMGR_SAFEKILL)
  */
-int bthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *attr)
+int bthread_kill(pthread_t thread, int sig, 
+                 const char *file, int line)
 {
-   return pthread_mutex_init(m, attr);
-}
+   bool thread_found_in_process=false;
+   
+   /* We doesn't allow to send signal to ourself */
+   ASSERT(!pthread_equal(thread, pthread_self()));
 
-/*
- * Replacement for pthread_mutex_destroy() for pthread_mutex_t
- */
-int bthread_mutex_destroy(pthread_mutex_t *m)
-{
-   return pthread_mutex_destroy(m);
-}
+   /* This loop isn't very efficient with dozens of threads but we don't use
+    * signal very much, and this feature is for testing only
+    */
+   lmgr_p(&lmgr_global_mutex);
+   {
+      lmgr_thread_t *item;
+      foreach_dlist(item, global_mgr) {
+         if (pthread_equal(thread, item->thread_id)) {
+            thread_found_in_process=true;
+            break;
+         }
+      }
+   }
+   lmgr_v(&lmgr_global_mutex);
 
+   /* Sending a signal to non existing thread can create problem
+    * so, we can stop here.
+    */
+   ASSERT(thread_found_in_process == true);
+
+   Dmsg3(100, "%s:%d send kill to existing thread %p\n", file, line, thread);
+   return pthread_kill(thread, sig);
+}
 
 /*
  * Replacement for pthread_mutex_lock()
@@ -832,6 +858,25 @@ int bthread_cond_timedwait_p(pthread_cond_t *cond,
    return ret;
 }
 
+/*  Test if this mutex is locked by the current thread 
+ *  returns:
+ *     0 - unlocked
+ *     1 - locked by the current thread
+ *     2 - locked by an other thread
+ */
+int lmgr_mutex_is_locked(void *m)
+{
+   lmgr_thread_t *self = lmgr_get_thread_info();
+
+   for(int i=0; i <= self->current; i++) {
+      if (self->lock_list[i].lock == m) {
+         return 1;              /* locked by us */
+      }
+   }
+
+   return 0;                    /* not locked by us */
+}
+
 /*
  * Use this function when the caller handle the mutex directly
  *
@@ -927,10 +972,7 @@ void dbg_print_lock(FILE *fp)
 
 #include "lockmgr.h"
 #define BTHREAD_MUTEX_NO_PRIORITY      {PTHREAD_MUTEX_INITIALIZER, 0}
-#define BTHREAD_MUTEX_PRIORITY_1       {PTHREAD_MUTEX_INITIALIZER, 1}
-#define BTHREAD_MUTEX_PRIORITY_2       {PTHREAD_MUTEX_INITIALIZER, 2}
-#define BTHREAD_MUTEX_PRIORITY_3       {PTHREAD_MUTEX_INITIALIZER, 3}
-#define BTHREAD_MUTEX_PRIORITY_4       {PTHREAD_MUTEX_INITIALIZER, 4}
+#define BTHREAD_MUTEX_PRIORITY(p)      {PTHREAD_MUTEX_INITIALIZER, p}
 #undef P
 #undef V
 #define P(x) bthread_mutex_lock_p(&(x), __FILE__, __LINE__)
@@ -943,9 +985,9 @@ bthread_mutex_t mutex3 = BTHREAD_MUTEX_NO_PRIORITY;
 bthread_mutex_t mutex4 = BTHREAD_MUTEX_NO_PRIORITY;
 bthread_mutex_t mutex5 = BTHREAD_MUTEX_NO_PRIORITY;
 bthread_mutex_t mutex6 = BTHREAD_MUTEX_NO_PRIORITY;
-bthread_mutex_t mutex_p1 = BTHREAD_MUTEX_PRIORITY_1;
-bthread_mutex_t mutex_p2 = BTHREAD_MUTEX_PRIORITY_2;
-bthread_mutex_t mutex_p3 = BTHREAD_MUTEX_PRIORITY_3;
+bthread_mutex_t mutex_p1 = BTHREAD_MUTEX_PRIORITY(1);
+bthread_mutex_t mutex_p2 = BTHREAD_MUTEX_PRIORITY(2);
+bthread_mutex_t mutex_p3 = BTHREAD_MUTEX_PRIORITY(3);
 static const char *my_prog;
 
 void *self_lock(void *temp)
@@ -1106,10 +1148,13 @@ int main(int argc, char **argv)
    void *ret=NULL;
    lmgr_thread_t *self;
    pthread_t id1, id2, id3, tab[200];
+   bthread_mutex_t bmutex1;
+   pthread_mutex_t pmutex2;
    my_prog = argv[0];
 
    use_undertaker = false;
    lmgr_init_thread();
+   self = lmgr_get_thread_info();
 
    if (argc == 2) {             /* do priority check */
       P(mutex_p2);                /* not permited */
@@ -1119,6 +1164,18 @@ int main(int argc, char **argv)
       return 0;
    }
 
+   pthread_mutex_init(&bmutex1, NULL);
+   bthread_mutex_set_priority(&bmutex1, 10);
+
+   pthread_mutex_init(&pmutex2, NULL);
+   P(bmutex1);
+   ok(self->max_priority == 10, "Check self max_priority");
+   P(pmutex2);
+   ok(bmutex1.priority == 10, "Check bmutex_set_priority()");
+   V(pmutex2);
+   V(bmutex1);
+   ok(self->max_priority == 0, "Check self max_priority");
+
    pthread_create(&id1, NULL, self_lock, NULL);
    sleep(2);
    ok(lmgr_detect_deadlock(), "Check self deadlock");
@@ -1189,7 +1246,9 @@ int main(int argc, char **argv)
    P(mutex4);
    P(mutex5);
    P(mutex6);
+   ok(lmgr_mutex_is_locked(&mutex6) == 1, "Check if mutex is locked"); 
    V(mutex6);
+   ok(lmgr_mutex_is_locked(&mutex6) == 0, "Check if mutex is locked"); 
    V(mutex5);
    V(mutex4);
 
@@ -1203,7 +1262,6 @@ int main(int argc, char **argv)
    pthread_join(id3, &ret);
    ok(ret != 0, "Check for priority segfault");
 
-   self = lmgr_get_thread_info();
    P(mutex_p1);
    ok(self->max_priority == 1, "Check max_priority 1/4");
    P(mutex_p2);