From: Tony Cook Date: Sat, 27 Oct 2012 02:18:10 +0000 (+1100) Subject: handle the race between writing the head and tail of an entry between threads X-Git-Tag: v0.94~2^2~11 X-Git-Url: http://git.imager.perl.org/imager.git/commitdiff_plain/cd45c8109b81c44b28103cc1a821e3eaab4758ca handle the race between writing the head and tail of an entry between threads in some cases the head and tails from different threads would interleave --- diff --git a/log.c b/log.c index ebcb9a0d..62b8eab8 100644 --- a/log.c +++ b/log.c @@ -13,6 +13,8 @@ #define LOG_DATE_FORMAT "%Y/%m/%d %H:%M:%S" +static i_mutex_t log_mutex; + static void im_vloog(pIMCTX, int level, const char *fmt, va_list ap); @@ -24,6 +26,10 @@ int im_init_log(pIMCTX, const char* name,int level) { i_clear_error(); + if (!log_mutex) { + log_mutex = i_mutex_new(); + } + if (aIMCTX->lg_file) { if (aIMCTX->own_log) fclose(aIMCTX->lg_file); @@ -95,6 +101,8 @@ im_vloog(pIMCTX, int level, const char *fmt, va_list ap) { if (!aIMCTX->lg_file || level > aIMCTX->log_level) return; + + i_mutex_lock(log_mutex); timi = time(NULL); str_tm = localtime(&timi); @@ -103,6 +111,8 @@ im_vloog(pIMCTX, int level, const char *fmt, va_list ap) { aIMCTX->filename, aIMCTX->line, level); vfprintf(aIMCTX->lg_file, fmt, ap); fflush(aIMCTX->lg_file); + + i_mutex_unlock(log_mutex); } void