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.
This commit is contained in:
NRK 2023-10-08 02:58:27 +06:00 committed by sfan5
parent 1abe908e31
commit c2d0a4a80f
1 changed files with 4 additions and 13 deletions

View File

@ -51,8 +51,7 @@
// buffer all keypresses until ENTER is pressed. // buffer all keypresses until ENTER is pressed.
#define INPUT_TIMEOUT 1000 #define INPUT_TIMEOUT 1000
static volatile struct termios tio_orig; static struct termios tio_orig;
static volatile int tio_orig_set;
static int tty_in = -1, tty_out = -1; static int tty_in = -1, tty_out = -1;
@ -300,11 +299,6 @@ static void do_activate_getch2(void)
struct termios tio_new; struct termios tio_new;
tcgetattr(tty_in,&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_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
tio_new.c_cc[VMIN] = 1; tio_new.c_cc[VMIN] = 1;
tio_new.c_cc[VTIME] = 0; tio_new.c_cc[VTIME] = 0;
@ -319,12 +313,7 @@ static void do_deactivate_getch2(void)
return; return;
enable_kx(false); enable_kx(false);
tcsetattr(tty_in, TCSANOW, &tio_orig);
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);
}
getch2_active = 0; getch2_active = 0;
} }
@ -552,6 +541,8 @@ void terminal_init(void)
tty_out = STDOUT_FILENO; tty_out = STDOUT_FILENO;
} }
tcgetattr(tty_in, &tio_orig);
// handlers to fix terminal settings // handlers to fix terminal settings
setsigaction(SIGCONT, continue_sighandler, 0, true); setsigaction(SIGCONT, continue_sighandler, 0, true);
setsigaction(SIGTSTP, stop_sighandler, SA_RESETHAND, false); setsigaction(SIGTSTP, stop_sighandler, SA_RESETHAND, false);