diff --git a/cmdline/io.c b/cmdline/io.c index 07dfe1e6..02485e8c 100644 --- a/cmdline/io.c +++ b/cmdline/io.c @@ -26,69 +26,6 @@ #error Pthread library is required for InputOutput! #endif -/** - * Implementation note about conditional variables. - * - * In release code the conditional variables are signaled outside the mutex, - * to reduce the number of context switches. - * - * Instead, when testing with helgrind and drd, they are signaled inside the mutex - * to help such tools to see the dependency between the signal and the wait. - * We use the CHECKER define to detect such case. - * - * Here some interesting discussion: - * - * Condvars: signal with mutex locked or not? - * http://www.domaigne.com/blog/computing/condvars-signal-with-mutex-locked-or-not/ - * - * Calling pthread_cond_signal without locking mutex - * http://stackoverflow.com/questions/4544234/calling-pthread-cond-signal-without-locking-mutex/4544494#4544494 - */ - -/* with checkers use the safe signaling */ -#ifdef CHECKER -#define USE_SAFE_SIGNAL 1 -#endif - -/* in Windows also use the safe signaling */ -#ifdef _WIN32 -#define USE_SAFE_SIGNAL 1 -#endif - -static void pthread_cond_signal_and_unlock(pthread_cond_t* cond, pthread_mutex_t* mutex) -{ -#ifndef USE_SAFE_SIGNAL - /* without the thread checker unlock before signaling, */ - /* this reduces the number of context switches */ - pthread_mutex_unlock(mutex); -#endif - - pthread_cond_signal(cond); - -#ifdef USE_SAFE_SIGNAL - /* with the thread checker unlock after signaling */ - /* to make explicit the condition and mutex relation */ - pthread_mutex_unlock(mutex); -#endif -} - -static void pthread_cond_broadcast_and_unlock(pthread_cond_t* cond, pthread_mutex_t* mutex) -{ -#ifndef USE_SAFE_SIGNAL - /* without the thread checker unlock before signaling, */ - /* this reduces the number of context switches */ - pthread_mutex_unlock(mutex); -#endif - - pthread_cond_broadcast(cond); - -#ifdef USE_SAFE_SIGNAL - /* with the thread checker unlock after signaling */ - /* to make explicit the condition and mutex relation */ - pthread_mutex_unlock(mutex); -#endif -} - void io_init(struct snapraid_io* io, struct snapraid_state* state, unsigned io_cache, unsigned buffer_max, void (*data_reader)(struct snapraid_worker*, struct snapraid_task*), @@ -759,7 +696,6 @@ static void* io_reader_thread(void* arg) return 0; } - static void* io_writer_thread(void* arg) { struct snapraid_worker* worker = arg; diff --git a/cmdline/snapraid.c b/cmdline/snapraid.c index f3a6028d..f773164b 100644 --- a/cmdline/snapraid.c +++ b/cmdline/snapraid.c @@ -282,6 +282,7 @@ void config(char* conf, size_t conf_size, const char* argv0) #define OPT_TEST_FORCE_PARITY_UPDATE 289 #define OPT_TEST_IO_CACHE 290 #define OPT_TEST_IO_STATS 291 +#define OPT_TEST_COND_SIGNAL_OUTSIDE 292 #if HAVE_GETOPT_LONG struct option long_options[] = { @@ -415,6 +416,9 @@ struct option long_options[] = { /* Print IO stats */ { "test-io-stats", 0, 0, OPT_TEST_IO_STATS }, + /* Signal condition variable outside the mutex */ + { "test-cond-signal-oustide", 0, 0, OPT_TEST_COND_SIGNAL_OUTSIDE }, + { 0, 0, 0, 0 } }; #endif @@ -809,6 +813,9 @@ int main(int argc, char* argv[]) case OPT_TEST_IO_STATS : opt.force_stats = 1; break; + case OPT_TEST_COND_SIGNAL_OUTSIDE : + pthread_cond_signal_outside = 1; + break; default : /* LCOV_EXCL_START */ log_fatal("Unknown option '%c'\n", (char)c); diff --git a/cmdline/support.c b/cmdline/support.c index fd7388f6..dd75356a 100644 --- a/cmdline/support.c +++ b/cmdline/support.c @@ -1110,3 +1110,71 @@ int smartctl_flush(FILE* f, const char* file, const char* name) return 0; } +/****************************************************************************/ +/* thread */ + +#if HAVE_PTHREAD +/** + * Implementation note about conditional variables. + * + * The conditional variables can be signaled inside or ouside the mutex, + * what is better it's debatable but in general doing that ouside the mutex, + * reduces the number of context switches. + * + * But when when testing with helgrind and drd, this disallows such tools to + * to see the dependency between the signal and the wait. + * + * To avoid it we signal everything inside the mutex. And we do this in both + * test mode (with CHERCKER defined) and release mode (CHECKER not defined), + * to be on the safe side and avoid any difference in beaviour between test and + * release. + * + * Here some interesting discussion: + * + * Condvars: signal with mutex locked or not? + * http://www.domaigne.com/blog/computing/condvars-signal-with-mutex-locked-or-not/ + * + * Calling pthread_cond_signal without locking mutex + * http://stackoverflow.com/questions/4544234/calling-pthread-cond-signal-without-locking-mutex/4544494#4544494 + */ + +/** + * Control when to signal the condition variables. + */ +int pthread_cond_signal_outside = 0; + +void pthread_cond_signal_and_unlock(pthread_cond_t* cond, pthread_mutex_t* mutex) +{ + if (pthread_cond_signal_outside) { + /* without the thread checker unlock before signaling, */ + /* this reduces the number of context switches */ + pthread_mutex_unlock(mutex); + } + + pthread_cond_signal(cond); + + if (!pthread_cond_signal_outside) { + /* with the thread checker unlock after signaling */ + /* to make explicit the condition and mutex relation */ + pthread_mutex_unlock(mutex); + } +} + +void pthread_cond_broadcast_and_unlock(pthread_cond_t* cond, pthread_mutex_t* mutex) +{ + if (pthread_cond_signal_outside) { + /* without the thread checker unlock before signaling, */ + /* this reduces the number of context switches */ + pthread_mutex_unlock(mutex); + } + + pthread_cond_broadcast(cond); + + if (!pthread_cond_signal_outside) { + /* with the thread checker unlock after signaling */ + /* to make explicit the condition and mutex relation */ + pthread_mutex_unlock(mutex); + } +} +#endif + diff --git a/cmdline/support.h b/cmdline/support.h index 8bcbe0d5..ad1a04fe 100644 --- a/cmdline/support.h +++ b/cmdline/support.h @@ -360,5 +360,25 @@ int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* sm */ int smartctl_flush(FILE* f, const char* file, const char* name); +/****************************************************************************/ +/* thread */ + +#if HAVE_PTHREAD +/** + * Control when to signal the condition variables. + * + * Default is inside the mutex. + * + * Ensure to change that before starting any thread. + */ +int pthread_cond_signal_outside; + +/** + * Signal and unlock condition variables. + */ +void pthread_cond_signal_and_unlock(pthread_cond_t* cond, pthread_mutex_t* mutex); +void pthread_cond_broadcast_and_unlock(pthread_cond_t* cond, pthread_mutex_t* mutex); +#endif + #endif