]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/lockmgr.h
Change copyright as per agreement with FSFE
[bacula/bacula] / bacula / src / lib / lockmgr.h
index 405b51ede1d8573c4084b10c2a1f23c84f7a6c77..394864998638705b6690e6ca2d0a9f5d50f2a306 100644 (file)
@@ -1,33 +1,24 @@
 /*
-   Bacula® - The Network Backup Solution
-
-   Copyright (C) 2008-2008 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
-   License as published by the Free Software Foundation, which is 
-   listed in the file LICENSE.
-
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   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
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA.
-
-   Bacula® is a registered trademark of Kern Sibbald.
-   The licensor of Bacula is the Free Software Foundation Europe
-   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
-   Switzerland, email:ftf@fsfeurope.org.
+   Bacula(R) - The Network Backup Solution
+
+   Copyright (C) 2000-2016 Kern Sibbald
+
+   The original author of Bacula is Kern Sibbald, with contributions
+   from many others, a complete list can be found in the file AUTHORS.
+
+   You may use this file and others of this release according to the
+   license defined in the LICENSE file, which includes the Affero General
+   Public License, v3.0 ("AGPLv3") and some additional permissions and
+   terms pursuant to its AGPLv3 Section 7.
+
+   This notice must be preserved when any source code is 
+   conveyed and/or propagated.
+
+   Bacula(R) is a registered trademark of Kern Sibbald.
 */
 
-#ifndef _LOCKMGR_H
-#define _LOCKMGR_H 1
+#ifndef LOCKMGR_H
+#define LOCKMGR_H 1
 
 #include "mutex_list.h"     /* Manage mutex with priority in a central place */
 
 void lmgr_p(pthread_mutex_t *m);
 void lmgr_v(pthread_mutex_t *m);
 
-#ifdef _USE_LOCKMGR
+/*
+ * Get integer thread id
+ */
+intptr_t bthread_get_thread_id();
+
+#ifdef USE_LOCKMGR
 
 typedef struct bthread_mutex_t
 {
@@ -46,7 +42,7 @@ typedef struct bthread_mutex_t
    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
@@ -71,7 +67,7 @@ int bthread_cond_timedwait_p(pthread_cond_t *cond,
                              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, 
+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 */
@@ -79,14 +75,20 @@ int bthread_mutex_unlock_p(pthread_mutex_t *m,
                            const char *file="*unknown*", int line=0);
 
 /* Replacement of pthread_mutex_lock() */
-int bthread_mutex_lock_p(bthread_mutex_t *m, 
+int bthread_mutex_lock_p(bthread_mutex_t *m,
                          const char *file="*unknown*", int line=0);
 
 /* Replacement of pthread_mutex_unlock() */
-int bthread_mutex_unlock_p(bthread_mutex_t *m, 
+int bthread_mutex_unlock_p(bthread_mutex_t *m,
                            const char *file="*unknown*", int line=0);
 
-/* 
+/*  Test if this mutex is locked by the current thread
+ *     0 - not locked by the current thread
+ *     1 - locked by the current thread
+ */
+int lmgr_mutex_is_locked(void *m);
+
+/*
  * Use them when you want use your lock yourself (ie rwlock)
  */
 
@@ -94,15 +96,15 @@ int bthread_mutex_unlock_p(bthread_mutex_t *m,
 void lmgr_pre_lock(void *m, int prio=0,
                    const char *file="*unknown*", int line=0);
 
-/* Call after getting it */ 
+/* Call after getting it */
 void lmgr_post_lock();
 
 /* Same as pre+post lock */
-void lmgr_do_lock(void *m, int prio=0, 
+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); 
+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);
@@ -122,7 +124,7 @@ void lmgr_init_thread();
 void lmgr_cleanup_thread();
 
 /*
- * Call this at the end of the program, it will release the 
+ * Call this at the end of the program, it will release the
  * global lock manager
  */
 void lmgr_cleanup_main();
@@ -137,6 +139,19 @@ void lmgr_dump();
  */
 bool lmgr_detect_deadlock();
 
+/* Bit flags */
+#define LMGR_EVENT_NONE    0
+#define LMGR_EVENT_DUP     1       /* use strdup() to copy the comment (will set FREE) */
+#define LMGR_EVENT_FREE    2       /* use free() when overwriting/deleting the comment */
+#define LMGR_EVENT_INVALID 4       /* Used to mark the record invalid */
+
+/*
+ * Add event to the thread event list
+ */
+void lmgr_add_event_p(const char *comment, intptr_t user_data, int32_t flags, const char *file, int32_t line);
+#define lmgr_add_event(c, u) lmgr_add_event_p(c, u, 0, __FILE__, __LINE__)
+#define lmgr_add_event_flag(c, u, f) lmgr_add_event_p(c, u, (f), __FILE__, __LINE__)
+
 /*
  * Search a deadlock after a fatal signal
  * no lock are granted, so the program must be
@@ -152,6 +167,12 @@ int lmgr_thread_create(pthread_t *thread,
                        const pthread_attr_t *attr,
                        void *(*start_routine)(void*), void *arg);
 
+/*
+ * Can use SAFEKILL to check if the argument is a valid threadid
+ */
+int bthread_kill(pthread_t thread, int sig,
+                 const char *file="*unknown*", int line=0);
+
 #define BTHREAD_MUTEX_NO_PRIORITY      {PTHREAD_MUTEX_INITIALIZER, 0}
 #define BTHREAD_MUTEX_INITIALIZER      BTHREAD_MUTEX_NO_PRIORITY
 
@@ -167,26 +188,38 @@ int lmgr_thread_create(pthread_t *thread,
 #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
+/*
+ * Define LOCKMGR_COMPLIANT to use real pthread functions
  */
-
-#ifdef _LOCKMGR_COMPLIANT
-# define P(x) lmgr_p(&(x))
-# define V(x) lmgr_v(&(x))
+#define real_P(x) lmgr_p(&(x))
+#define real_V(x) lmgr_v(&(x))
+
+#ifdef LOCKMGR_COMPLIANT
+# define P(x)  lmgr_p(&(x))
+# define pP(x) lmgr_p(x)
+# define V(x)  lmgr_v(&(x))
+# define pV(x) lmgr_v(x)
 #else
-# define P(x)                   bthread_mutex_lock_p(&(x), __FILE__, __LINE__)
-# define V(x)                   bthread_mutex_unlock_p(&(x), __FILE__, __LINE__)
+# define P(x)  bthread_mutex_lock_p(&(x), __FILE__, __LINE__)
+# define pP(x) bthread_mutex_lock_p((x), __FILE__, __LINE__)
+# define V(x)  bthread_mutex_unlock_p(&(x), __FILE__, __LINE__)
+# define pV(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)
+
+# ifdef USE_LOCKMGR_SAFEKILL
+#  define pthread_kill(a,b)      bthread_kill((a),(b), __FILE__, __LINE__)
+# endif
 #endif
 
-#else   /* _USE_LOCKMGR */
+#else   /* !USE_LOCKMGR */
 
 # define lmgr_detect_deadloc()
+# define lmgr_add_event_p(c, u, f, l)
+# define lmgr_add_event(c, u)
 # define lmgr_dump()
 # define lmgr_init_thread()
 # define lmgr_cleanup_thread()
@@ -196,16 +229,22 @@ int lmgr_thread_create(pthread_t *thread,
 # define lmgr_do_unlock(m)
 # define lmgr_cleanup_main()
 # define bthread_mutex_set_priority(a,b)
-# 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_lock(a)           pthread_mutex_lock(a)
+# define bthread_mutex_lock_p(a, f, l)   pthread_mutex_lock(a)
+# define bthread_mutex_unlock(a)         pthread_mutex_unlock(a)
+# define bthread_mutex_unlock_p(a, f, l) 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 pP(x) lmgr_p((x))
+# define V(x)  lmgr_v(&(x))
+# define pV(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 */
+# define lmgr_mutex_is_locked(m)        (1)
+# define bthread_cond_wait_p(w, x, y, z) pthread_cond_wait(w,x)
+#endif  /* USE_LOCKMGR */
 
-#endif  /* _LOCKMGR_H */
+#endif  /* LOCKMGR_H */