From a6709447e18d166b0a6d101e9e72206f2f8a9988 Mon Sep 17 00:00:00 2001 From: Matt Birkholz Date: Sat, 20 Dec 2014 11:50:06 -0700 Subject: [PATCH] smp: Add mutex LOCK/UNLOCK boilerplate to confshared.h (everywhere). --- src/microcode/confshared.h | 56 ++++++++++++++++++++++++++++++++++++-- src/microcode/outf.c | 6 ++-- src/microcode/prossmp.c | 6 ++-- src/microcode/uxsig.c | 3 +- 4 files changed, 62 insertions(+), 9 deletions(-) diff --git a/src/microcode/confshared.h b/src/microcode/confshared.h index ae091b7f1..2b027d897 100644 --- a/src/microcode/confshared.h +++ b/src/microcode/confshared.h @@ -694,7 +694,59 @@ extern void win32_stack_reset (void); #endif #ifndef ENABLE_SMP -#define __thread -#endif + +# define __thread +# define LOCK() do { } while (false) +# define UNLOCK() do { } while (false) + +#else /* ENABLE_SMP */ + +# include +# ifdef ENABLE_DEBUGGING_TOOLS + +# define MUTEX_INITIALIZER PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP + +extern bool smp_trace_p; + +# define LOCK() do \ +{ \ + int err; \ + if (smp_trace_p && locked_p != false) \ + outf_error_line (";%d already locked!", self->id); \ + err = pthread_mutex_lock (&mutex); \ + assert (err == 0); \ + locked_p = true; \ +} while (false) + +# define UNLOCK() do \ +{ \ + int err; \ + if (smp_trace_p && locked_p != true) \ + outf_error_line (";%d already unlocked!", self->id); \ + err = pthread_mutex_unlock (&mutex); \ + assert (err == 0); \ + locked_p = false; \ +} while (false) + +# else /* !ENABLE_DEBUGGING_TOOLS */ + +# define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER + +# define LOCK() do \ +{ \ + int err = pthread_mutex_lock (&mutex); \ + if (err) \ + error_with_argument (err); \ +} while (false) + +# define UNLOCK() do \ +{ \ + int err = pthread_mutex_unlock (&mutex); \ + if (err) \ + error_with_argument (err); \ +} while (false) + +# endif /* !ENABLE_DEBUGGING_TOOLS */ +#endif /* ENABLE_SMP */ #endif /* SCM_CONFSHARED_H */ diff --git a/src/microcode/outf.c b/src/microcode/outf.c index a5849f9f5..b1321b777 100644 --- a/src/microcode/outf.c +++ b/src/microcode/outf.c @@ -249,9 +249,11 @@ outf_flush_fatal (void) #ifndef OUTF_VARIANTS_DEFINED +#undef LOCK +#undef UNLOCK #ifdef ENABLE_SMP -static pthread_mutex_t stderr_mutex = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t stderr_mutex = MUTEX_INITIALIZER; +static pthread_mutex_t stdout_mutex = MUTEX_INITIALIZER; #define LOCK(which) mutex_lock (which) #define UNLOCK(which) mutex_unlock (which) diff --git a/src/microcode/prossmp.c b/src/microcode/prossmp.c index 52be57ccc..4e70cbf6d 100644 --- a/src/microcode/prossmp.c +++ b/src/microcode/prossmp.c @@ -42,7 +42,7 @@ processor_t *processors; /* The mutex that serializes processor state changes, including the gc_processor variable. */ -pthread_mutex_t state_mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t state_mutex = MUTEX_INITIALIZER; /* The processor running the garbage collector. */ processor_t *gc_processor = NULL; @@ -54,7 +54,7 @@ static pthread_cond_t finished = PTHREAD_COND_INITIALIZER; static pthread_cond_t ready = PTHREAD_COND_INITIALIZER; /* The mutex that serializes thread switches. */ -static pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t thread_mutex = MUTEX_INITIALIZER; /* The current pthread's processor. */ __thread processor_t *self; @@ -65,7 +65,7 @@ extern int saved_stack_size; #ifdef ENABLE_DEBUGGING_TOOLS -static bool smp_trace_p = false; +bool smp_trace_p = false; static void trace (const char * format, ...) diff --git a/src/microcode/uxsig.c b/src/microcode/uxsig.c index 8f061cbf8..ce9b20890 100644 --- a/src/microcode/uxsig.c +++ b/src/microcode/uxsig.c @@ -507,7 +507,6 @@ DEFUN_STD_HANDLER (sighnd_console_resize, }) #ifdef ENABLE_SMP -#include static DEFUN_STD_HANDLER (sighnd_global_gc, @@ -522,7 +521,7 @@ smp_kill_gc (processor_t *p) } static volatile processor_t *next_timer = NULL; -static pthread_mutex_t nt_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t nt_mutex = MUTEX_INITIALIZER; #ifdef ENABLE_DEBUGGING_TOOLS # define LOCK_NT() do \ { \ -- 2.25.1