From c2d0a4a80fc5c8e1755523b0709e68f973a3d5d3 Mon Sep 17 00:00:00 2001 From: NRK Date: Sun, 8 Oct 2023 02:58:27 +0600 Subject: [PATCH] terminal-unix: avoid data race + simplify tio_orig and tio_orig_set are being touched inside of signal handler which might be invoked from another thread - which makes this a data race. there's no real reason to set tio_orig inside of do_activate_getch2() which is registered as a signal handler. just set it once, in terminal_init(), before any signal handlers come in play. this also allows removing the tio_orig_set variable completely. --- osdep/terminal-unix.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/osdep/terminal-unix.c b/osdep/terminal-unix.c index 0ad3729eda..8e96af3928 100644 --- a/osdep/terminal-unix.c +++ b/osdep/terminal-unix.c @@ -51,8 +51,7 @@ // buffer all keypresses until ENTER is pressed. #define INPUT_TIMEOUT 1000 -static volatile struct termios tio_orig; -static volatile int tio_orig_set; +static struct termios tio_orig; static int tty_in = -1, tty_out = -1; @@ -300,11 +299,6 @@ static void do_activate_getch2(void) struct termios tio_new; tcgetattr(tty_in,&tio_new); - if (!tio_orig_set) { - tio_orig = tio_new; - tio_orig_set = 1; - } - tio_new.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */ tio_new.c_cc[VMIN] = 1; tio_new.c_cc[VTIME] = 0; @@ -319,12 +313,7 @@ static void do_deactivate_getch2(void) return; enable_kx(false); - - if (tio_orig_set) { - // once set, it will never be set again - // so we can cast away volatile here - tcsetattr(tty_in, TCSANOW, (const struct termios *) &tio_orig); - } + tcsetattr(tty_in, TCSANOW, &tio_orig); getch2_active = 0; } @@ -552,6 +541,8 @@ void terminal_init(void) tty_out = STDOUT_FILENO; } + tcgetattr(tty_in, &tio_orig); + // handlers to fix terminal settings setsigaction(SIGCONT, continue_sighandler, 0, true); setsigaction(SIGTSTP, stop_sighandler, SA_RESETHAND, false);