From 4cde17aea7804d8c42f6d1b1365240bea138a163 Mon Sep 17 00:00:00 2001 From: jsteube Date: Tue, 20 Sep 2016 12:32:39 +0200 Subject: [PATCH] Fix race-condition in thread_keypress() --- include/shared.h | 3 ++- src/shared.c | 13 +++++++++++-- src/terminal.c | 10 +++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/shared.h b/include/shared.h index 86497d127..5a57571ec 100644 --- a/include/shared.h +++ b/include/shared.h @@ -18,6 +18,7 @@ u64 mydivc64 (const u64 dividend, const u64 divisor); void naive_replace (char *s, const u8 key_char, const u8 replace_char); void naive_escape (char *s, size_t s_max, const u8 key_char, const u8 escape_char); -void hc_sleep (const int sec); +void hc_sleep_ms (const int msec); +void hc_sleep (const int sec); #endif // _SHARED_H diff --git a/src/shared.c b/src/shared.c index ce250112f..0b11f52cc 100644 --- a/src/shared.c +++ b/src/shared.c @@ -74,11 +74,20 @@ void naive_escape (char *s, size_t s_max, const u8 key_char, const u8 escape_cha strncpy (s, s_escaped, s_max - 1); } -void hc_sleep (const int sec) +void hc_sleep_ms (const int msec) { #if defined (_WIN) - #define sleep(x) Sleep ((x) * 1000) + Sleep (msec); + #else + usleep (msec * 1000); #endif +} +void hc_sleep (const int sec) +{ + #if defined (_WIN) + Sleep (sec * 1000); + #else sleep (sec); + #endif } diff --git a/src/terminal.c b/src/terminal.c index 1e97719ae..e54ea3f63 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -27,6 +27,7 @@ #include "loopback.h" #include "data.h" #include "status.h" +#include "shared.h" #include "terminal.h" extern hc_global_data_t data; @@ -59,16 +60,19 @@ void clear_prompt () void *thread_keypress (void *p) { opencl_ctx_t *opencl_ctx = data.opencl_ctx; + + while (opencl_ctx->devices_status == STATUS_INIT) hc_sleep_ms (100); + hashconfig_t *hashconfig = data.hashconfig; hashes_t *hashes = data.hashes; uint quiet = data.quiet; - tty_break(); + tty_break (); while (data.shutdown_outer == 0) { - int ch = tty_getchar(); + int ch = tty_getchar (); if (ch == -1) break; @@ -164,7 +168,7 @@ void *thread_keypress (void *p) hc_thread_mutex_unlock (mux_display); } - tty_fix(); + tty_fix (); return (p); }