From: Wook Song Date: Fri, 23 Aug 2019 10:18:40 +0000 (+0900) Subject: [Core/Utils] Make logging utility functions be thread-safe X-Git-Tag: accepted/tizen/unified/20220103.130045~689 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d1c9a50da073ce1d27f37858302f2d775a3aaae6;p=platform%2Fadaptation%2Fnpu%2Ftrix-engine.git [Core/Utils] Make logging utility functions be thread-safe This patch makes logging utility functions be thread-safe. Signed-off-by: Wook Song --- diff --git a/src/core/ne-utils.c b/src/core/ne-utils.c index 022bbe0..fbcf129 100644 --- a/src/core/ne-utils.c +++ b/src/core/ne-utils.c @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -245,6 +246,8 @@ hash_table_del (hash_table *ht, hash_node *node) ********************************************************************/ #define logfilename "npu-engine.log" +static pthread_mutex_t priv_mutex = PTHREAD_MUTEX_INITIALIZER; + /** * @brief Log Level String in 5 characters */ @@ -283,32 +286,45 @@ static FILE *fp = NULL; * @param[in] m Module designation */ void logwrite(loglevel l, module m, const char *format, ...) { - va_list args; - time_t ltime; + time_t ltime = time (NULL); char* time_str; + va_list args; - ltime = time(NULL); - + pthread_mutex_lock (&priv_mutex); if (fp == NULL) { char filename[FILENAME_MAX]; snprintf(filename, FILENAME_MAX, "%s%s", conf->log_dir, logfilename); fp = fopen(filename, "a"); } + pthread_mutex_unlock (&priv_mutex); assert(fp != NULL); - /* asctime() generates a new line; so remove it */ + /** + * localtime() and asctime() are not MT-safe. There are alternatives, + * localtime_r() and asctime_r(), when __USE_POSIX is set. Without them, we + * need critical section here. + */ + pthread_mutex_lock (&priv_mutex); time_str = asctime(localtime(<ime)); - time_str[strlen(time_str) - 1] = '\x00'; - fprintf(fp, "[%s][%s][%s] ", loglevelstr[l], modulestr[m], time_str); + pthread_mutex_unlock (&priv_mutex); + time_str[strcspn(time_str, "\n")] = '\x00'; + fprintf(fp, "[%s][%s][%s] ", loglevelstr[l], modulestr[m], time_str); va_start (args, format); vfprintf(fp, format, args); va_end (args); } void fini_logwrite (void) { - if (fp != NULL) + pthread_mutex_lock (&priv_mutex); + if (fp != NULL) { fclose(fp); + fp = NULL; + } + pthread_mutex_unlock (&priv_mutex); + + /* Harmless statement */ + pthread_mutex_init (&priv_mutex, NULL); }