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 <pthread.h>
+#endif
#ifdef __WIN32__
# include <windows.h>
\f
#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 */