]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
gather all locking related code into src/lxc/lxclock.c
authorS.Çağlar Onur <caglar@10ur.org>
Wed, 13 Nov 2013 21:46:11 +0000 (16:46 -0500)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Thu, 14 Nov 2013 18:00:58 +0000 (12:00 -0600)
Signed-off-by: S.Çağlar Onur <caglar@10ur.org>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
src/lxc/lxclock.c
src/lxc/lxclock.h
src/lxc/utils.c

index 3857ff06cbec1f894de41cb9c62afa3e773b7a24..64823d2a58edbcdfe4ade6e1b3b782e41bfb38a8 100644 (file)
 #include <lxc/log.h>
 #include <lxc/lxccontainer.h>
 
+#ifdef MUTEX_DEBUGGING
+#include <execinfo.h>
+#endif
+
 #define OFLAG (O_CREAT | O_RDWR)
 #define SEMMODE 0660
 #define SEMVALUE 1
@@ -40,10 +44,55 @@ lxc_log_define(lxc_lock, lxc);
 
 #ifdef MUTEX_DEBUGGING
 pthread_mutex_t thread_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
+pthread_mutex_t static_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
+
+inline void dump_stacktrace(void)
+{
+       void *array[MAX_STACKDEPTH];
+       size_t size;
+       char **strings;
+       size_t i;
+
+       size = backtrace(array, MAX_STACKDEPTH);
+       strings = backtrace_symbols(array, size);
+
+       // Using fprintf here as our logging module is not thread safe
+       fprintf(stderr, "\tObtained %zd stack frames.\n", size);
+
+       for (i = 0; i < size; i++)
+               fprintf(stderr, "\t\t%s\n", strings[i]);
+
+       free (strings);
+}
 #else
 pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+inline void dump_stacktrace(void) {;}
 #endif
 
+void lock_mutex(pthread_mutex_t *l)
+{
+       int ret;
+
+       if ((ret = pthread_mutex_lock(l)) != 0) {
+               fprintf(stderr, "pthread_mutex_lock returned:%d %s", ret, strerror(ret));
+               dump_stacktrace();
+               exit(1);
+       }
+}
+
+void unlock_mutex(pthread_mutex_t *l)
+{
+       int ret;
+
+       if ((ret = pthread_mutex_unlock(l)) != 0) {
+               fprintf(stderr, "pthread_mutex_lock returned:%d %s", ret, strerror(ret));
+               dump_stacktrace();
+               exit(1);
+       }
+}
+
 static char *lxclock_name(const char *p, const char *n)
 {
        int ret;
@@ -267,24 +316,23 @@ void lxc_putlock(struct lxc_lock *l)
 
 void process_lock(void)
 {
-       int ret;
-
-       if ((ret = pthread_mutex_lock(&thread_mutex)) != 0) {
-               ERROR("pthread_mutex_lock returned:%d %s", ret, strerror(ret));
-               dump_stacktrace();
-               exit(1);
-       }
+       lock_mutex(&thread_mutex);
 }
 
 void process_unlock(void)
 {
-       int ret;
+       unlock_mutex(&thread_mutex);
+}
 
-       if ((ret = pthread_mutex_unlock(&thread_mutex)) != 0) {
-               ERROR("pthread_mutex_unlock returned:%d %s", ret, strerror(ret));
-               dump_stacktrace();
-               exit(1);
-       }
+/* Protects static const values inside the lxc_global_config_value funtion */
+void static_lock(void)
+{
+       lock_mutex(&static_mutex);
+}
+
+void static_unlock(void)
+{
+       unlock_mutex(&static_mutex);
 }
 
 int container_mem_lock(struct lxc_container *c)
index dcdf79d2d8cd3f453b951f8230ed8110f424b2ac..12ba82750607fab33b2e9c07cfbe76f2dee8961b 100644 (file)
@@ -87,6 +87,9 @@ extern void lxc_putlock(struct lxc_lock *l);
 
 extern void process_lock(void);
 extern void process_unlock(void);
+extern void static_lock(void);
+extern void static_unlock(void);
+
 struct lxc_container;
 extern int container_mem_lock(struct lxc_container *c);
 extern void container_mem_unlock(struct lxc_container *c);
index 4bc2c35e4de9a60098f17e745a9cce9707fe3e56..3fab9ae7ebadbb87b1a9010593039553ea4f45df 100644 (file)
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <assert.h>
-#include <pthread.h>
-
-#ifdef MUTEX_DEBUGGING
-#include <execinfo.h>
-#endif
 
 #ifndef HAVE_GETLINE
 #ifdef HAVE_FGETLN
 
 lxc_log_define(lxc_utils, lxc);
 
-
-#ifdef MUTEX_DEBUGGING
-static pthread_mutex_t static_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
-
-inline void dump_stacktrace(void)
-{
-       void *array[MAX_STACKDEPTH];
-       size_t size;
-       char **strings;
-       size_t i;
-
-       size = backtrace(array, MAX_STACKDEPTH);
-       strings = backtrace_symbols(array, size);
-
-       // Using fprintf here as our logging module is not thread safe
-       fprintf(stderr, "\tObtained %zd stack frames.\n", size);
-
-       for (i = 0; i < size; i++)
-               fprintf(stderr, "\t\t%s\n", strings[i]);
-
-       free (strings);
-}
-#else
-static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-inline void dump_stacktrace(void) {;}
-#endif
-
-/* Protects static const values inside the lxc_global_config_value funtion */
-static void static_lock(void)
-{
-       int ret;
-
-       if ((ret = pthread_mutex_lock(&static_mutex)) != 0) {
-               ERROR("pthread_mutex_lock returned:%d %s", ret, strerror(ret));
-               dump_stacktrace();
-               exit(1);
-       }
-}
-
-static void static_unlock(void)
-{
-       int ret;
-
-       if ((ret = pthread_mutex_unlock(&static_mutex)) != 0) {
-               ERROR("pthread_mutex_unlock returned:%d %s", ret, strerror(ret));
-               dump_stacktrace();
-               exit(1);
-       }
-}
-
 static int _recursive_rmdir_onedev(char *dirname, dev_t pdev)
 {
        struct dirent dirent, *direntp;
@@ -392,6 +336,7 @@ out:
        if (fin)
                fclose(fin);
        process_unlock();
+
        static_lock();
        value = values[i];
        static_unlock();