Workaround thread-unsafeness of cli_echo().

This commit is contained in:
Ondrej Zajicek 2014-02-07 13:09:55 +01:00
parent 9ae0f4b78c
commit 4e398e34bf
3 changed files with 13 additions and 2 deletions

View file

@ -39,14 +39,21 @@ static const int rate_limit_count = 5;
#ifdef USE_PTHREADS #ifdef USE_PTHREADS
#include <pthread.h> #include <pthread.h>
static pthread_mutex_t log_mutex; static pthread_mutex_t log_mutex;
static inline void log_lock(void) { pthread_mutex_lock(&log_mutex); } static inline void log_lock(void) { pthread_mutex_lock(&log_mutex); }
static inline void log_unlock(void) { pthread_mutex_unlock(&log_mutex); } static inline void log_unlock(void) { pthread_mutex_unlock(&log_mutex); }
static pthread_t main_thread;
void main_thread_init(void) { main_thread = pthread_self(); }
static int main_thread_self(void) { return pthread_equal(pthread_self(), main_thread); }
#else #else
static inline void log_lock(void) { } static inline void log_lock(void) { }
static inline void log_unlock(void) { } static inline void log_unlock(void) { }
void main_thread_init(void) { }
static int main_thread_self(void) { return 1; }
#endif #endif
@ -128,7 +135,8 @@ log_commit(int class, buffer *buf)
} }
log_unlock(); log_unlock();
/* FIXME: cli_echo is not thread-safe */ /* cli_echo is not thread-safe, so call it just from the main thread */
if (main_thread_self())
cli_echo(class, buf->start); cli_echo(class, buf->start);
buf->pos = buf->start; buf->pos = buf->start;

View file

@ -797,6 +797,8 @@ main(int argc, char **argv)
dup2(0, 2); dup2(0, 2);
} }
main_thread_init();
write_pid_file(); write_pid_file();
signal_init(); signal_init();

View file

@ -67,6 +67,7 @@ void krt_io_init(void);
/* log.c */ /* log.c */
void main_thread_init(void);
void log_init_debug(char *); /* Initialize debug dump to given file (NULL=stderr, ""=off) */ void log_init_debug(char *); /* Initialize debug dump to given file (NULL=stderr, ""=off) */
void log_switch(int debug, list *l, char *); /* Use l=NULL for initial switch */ void log_switch(int debug, list *l, char *); /* Use l=NULL for initial switch */