diff options
author | 2023-08-30 16:38:25 -0700 | |
---|---|---|
committer | 2023-08-30 16:38:25 -0700 | |
commit | 04215e2f3a8c11d9e2b5f336eee40e6f7ebf11c9 (patch) | |
tree | b22d77fe97584c3dad1c94a78c0c22ea5cf81558 /src/bun.js | |
parent | c19714aff7ae5170e905be8a2e3c1301b0158433 (diff) | |
download | bun-04215e2f3a8c11d9e2b5f336eee40e6f7ebf11c9.tar.gz bun-04215e2f3a8c11d9e2b5f336eee40e6f7ebf11c9.tar.zst bun-04215e2f3a8c11d9e2b5f336eee40e6f7ebf11c9.zip |
reset tty at exit (#4419)
Diffstat (limited to 'src/bun.js')
-rw-r--r-- | src/bun.js/bindings/wtf-bindings.cpp | 75 |
1 files changed, 45 insertions, 30 deletions
diff --git a/src/bun.js/bindings/wtf-bindings.cpp b/src/bun.js/bindings/wtf-bindings.cpp index 6fd10721a..d05fb255b 100644 --- a/src/bun.js/bindings/wtf-bindings.cpp +++ b/src/bun.js/bindings/wtf-bindings.cpp @@ -2,8 +2,8 @@ #include "wtf/StackTrace.h" #include "wtf/dtoa.h" -#include "wtf/Lock.h" -#include "termios.h" +#include <termios.h> +#include <stdatomic.h> extern "C" double WTF__parseDouble(const LChar* string, size_t length, size_t* position) { @@ -17,7 +17,8 @@ extern "C" void WTF__copyLCharsFromUCharSource(LChar* destination, const UChar* static int orig_termios_fd = -1; static struct termios orig_termios; -static WTF::Lock orig_termios_lock; +static _Atomic int orig_termios_spinlock; +static std::once_flag reset_once_flag; static int current_tty_mode = 0; static struct termios orig_tty_termios; @@ -36,6 +37,26 @@ int uv__tcsetattr(int fd, int how, const struct termios* term) return 0; } +extern "C" int uv_tty_reset_mode(void) +{ + int saved_errno; + int err; + + saved_errno = errno; + + if (atomic_exchange(&orig_termios_spinlock, 1)) + return 16; // UV_EBUSY; /* In uv_tty_set_mode(). */ + + err = 0; + if (orig_termios_fd != -1) + err = uv__tcsetattr(orig_termios_fd, TCSANOW, &orig_termios); + + atomic_store(&orig_termios_spinlock, 0); + errno = saved_errno; + + return err; +} + static void uv__tty_make_raw(struct termios* tio) { assert(tio != NULL); @@ -91,15 +112,17 @@ Bun__ttySetMode(int fd, int mode) if (rc == -1) return errno; - { - /* This is used for uv_tty_reset_mode() */ - LockHolder locker(orig_termios_lock); + /* This is used for uv_tty_reset_mode() */ + do { + expected = 0; + } while (!atomic_compare_exchange_strong(&orig_termios_spinlock, &expected, 1)); - if (orig_termios_fd == -1) { - orig_termios = orig_termios; - orig_termios_fd = fd; - } + if (orig_termios_fd == -1) { + orig_termios = orig_tty_termios; + orig_termios_fd = fd; } + + atomic_store(&orig_termios_spinlock, 0); } tmp = orig_tty_termios; @@ -113,9 +136,21 @@ Bun__ttySetMode(int fd, int mode) tmp.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); tmp.c_cc[VMIN] = 1; tmp.c_cc[VTIME] = 0; + + std::call_once(reset_once_flag, [] { + atexit([] { + uv_tty_reset_mode(); + }); + }); break; case 2: // io uv__tty_make_raw(&tmp); + + std::call_once(reset_once_flag, [] { + atexit([] { + uv_tty_reset_mode(); + }); + }); break; } @@ -127,26 +162,6 @@ Bun__ttySetMode(int fd, int mode) return rc; } -int uv_tty_reset_mode(void) -{ - int saved_errno; - int err; - - saved_errno = errno; - - if (orig_termios_lock.tryLock()) - return 16; // UV_EBUSY; /* In uv_tty_set_mode(). */ - - err = 0; - if (orig_termios_fd != -1) - err = uv__tcsetattr(orig_termios_fd, TCSANOW, &orig_termios); - - orig_termios_lock.unlock(); - errno = saved_errno; - - return err; -} - extern "C" void Bun__crashReportWrite(void* ctx, const char* message, size_t length); extern "C" void Bun__crashReportDumpStackTrace(void* ctx) { |