From 55bb29356f0c818dce8b3febc9ebd9026509a075 Mon Sep 17 00:00:00 2001 From: Matt Birkholz Date: Sat, 20 Dec 2014 08:44:05 -0700 Subject: [PATCH] smp: Serialize the outf functions. --- src/microcode/outf.c | 47 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/microcode/outf.c b/src/microcode/outf.c index 1a5cc5a9e..a5849f9f5 100644 --- a/src/microcode/outf.c +++ b/src/microcode/outf.c @@ -40,8 +40,12 @@ USA. Use outf where you would normally think of using fprintf and outf_flush where you would normally use fflush. */ -#include "config.h" +#include "scheme.h" +#include "ossmp.h" #include "outf.h" +#ifdef ENABLE_SMP +#include +#endif #ifdef __WIN32__ # include @@ -245,48 +249,89 @@ outf_flush_fatal (void) #ifndef OUTF_VARIANTS_DEFINED +#ifdef ENABLE_SMP +static pthread_mutex_t stderr_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER; +#define LOCK(which) mutex_lock (which) +#define UNLOCK(which) mutex_unlock (which) + +static void +mutex_lock (pthread_mutex_t *m) +{ + int err = pthread_mutex_lock (m); + if (err) + outf_error_line (";%d outf mutex lock failed: %d", self->id, err); +} + +static void +mutex_unlock (pthread_mutex_t *m) +{ + int err = pthread_mutex_unlock (m); + if (err) + outf_error_line (";%d outf mutex unlock failed: %d", self->id, err); +} + +#else +#define LOCK(which) +#define UNLOCK(which) +#endif + void voutf_console (const char * format, va_list args) { + LOCK (&stdout_mutex); vfprintf (stdout, format, args); + UNLOCK (&stdout_mutex); } void outf_flush_console (void) { + LOCK(&stdout_mutex); fflush (stdout); + UNLOCK(&stdout_mutex); } void voutf_error (const char * format, va_list args) { + LOCK(&stderr_mutex); vfprintf (stderr, format, args); + UNLOCK(&stderr_mutex); } void outf_flush_error (void) { + LOCK(&stderr_mutex); fflush (stderr); + UNLOCK(&stderr_mutex); } void voutf_error_line (const char * format, va_list args) { + LOCK(&stderr_mutex); vfprintf (stderr, format, args); fputc ('\n', stderr); fflush (stderr); + UNLOCK(&stderr_mutex); } void voutf_fatal (const char * format, va_list args) { + LOCK(&stderr_mutex); vfprintf (stderr, format, args); + UNLOCK(&stderr_mutex); } void outf_flush_fatal (void) { + LOCK(&stderr_mutex); fflush (stderr); + UNLOCK(&stderr_mutex); } #endif /* not OUTF_VARIANTS_DEFINED */ -- 2.25.1