]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/lockmgr.c
Detect mount/junction points and ignore junctions in Windows
[bacula/bacula] / bacula / src / lib / lockmgr.c
index 11d83fa293968a67f7ebdc8e0f94e261da215d91..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);
 #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; }
 
 /*
@@ -577,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;
 }
@@ -707,6 +710,41 @@ int pthread_mutex_destroy(bthread_mutex_t *m)
    return pthread_mutex_destroy(&m->mutex);
 }
 
+/* 
+ * Replacement for pthread_kill (only with USE_LOCKMGR_SAFEKILL)
+ */
+int bthread_kill(pthread_t thread, int sig, 
+                 const char *file, int line)
+{
+   bool thread_found_in_process=false;
+   
+   /* We doesn't allow to send signal to ourself */
+   ASSERT(!pthread_equal(thread, pthread_self()));
+
+   /* 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()
  * Returns always ok 
@@ -820,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
  *
@@ -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);