use mutex for printf output in multithread 31/99931/2
authorBoram Park <boram1288.park@samsung.com>
Thu, 24 Nov 2016 10:10:13 +0000 (19:10 +0900)
committerSooChan Lim <sc1.lim@samsung.com>
Fri, 25 Nov 2016 07:52:40 +0000 (23:52 -0800)
Change-Id: I6f903828f1e22f7c3dfed074173f9e5e78636011

common/tdm_log.c

index 5e1ab2c..fc1a8f1 100644 (file)
@@ -43,6 +43,7 @@
 #include <assert.h>
 #include <sys/syscall.h>
 #include <dlog.h>
+#include <pthread.h>
 
 #include "tdm.h"
 #include "tdm_log.h"
@@ -66,6 +67,9 @@ static unsigned int debug_level = TDM_LOG_LEVEL_INFO;
 
 static unsigned int need_check_env = 1;
 
+static unsigned int log_lock_init;
+static pthread_mutex_t log_lock;
+
 static void
 _tdm_log_check_env(void)
 {
@@ -117,6 +121,12 @@ tdm_log_print(int level, const char *fmt, ...)
 {
        va_list arg;
 
+       if (!log_lock_init) {
+               log_lock_init = 1;
+               pthread_mutex_init(&log_lock, NULL);
+
+       }
+
        if (need_check_env) {
                need_check_env = 0;
                _tdm_log_check_env();
@@ -153,6 +163,8 @@ tdm_log_print(int level, const char *fmt, ...)
 
                clock_gettime(CLOCK_MONOTONIC, &ts);
 
+               pthread_mutex_lock(&log_lock);
+
                if (color_enable)
                        printf("%s", color[level]);
                printf("[%s]", lvl_str[level]);
@@ -162,6 +174,8 @@ tdm_log_print(int level, const char *fmt, ...)
                va_start(arg, fmt);
                vprintf(fmt, arg);
                va_end(arg);
+
+               pthread_mutex_unlock(&log_lock);
        }
 
 #ifdef TDM_CONFIG_ASSERT