From 6e95519accff23b120b3b5b46fec32e55395f9d6 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 16 May 2026 23:52:28 +0300 Subject: [PATCH] Kernel: Make TTY write lock a mutex instead of a spinlock The mutex could not be locked while processing ANSI DGR in VTTY --- kernel/include/kernel/Terminal/TTY.h | 2 +- kernel/kernel/Terminal/TTY.cpp | 10 ++++------ kernel/kernel/Terminal/VirtualTTY.cpp | 18 +++++++++--------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/kernel/include/kernel/Terminal/TTY.h b/kernel/include/kernel/Terminal/TTY.h index 42d23332..74943048 100644 --- a/kernel/include/kernel/Terminal/TTY.h +++ b/kernel/include/kernel/Terminal/TTY.h @@ -105,7 +105,7 @@ namespace Kernel protected: Mutex m_mutex; - RecursiveSpinLock m_write_lock; + Mutex m_write_lock; ThreadBlocker m_write_blocker; }; diff --git a/kernel/kernel/Terminal/TTY.cpp b/kernel/kernel/Terminal/TTY.cpp index a4d89068..00b3ee49 100644 --- a/kernel/kernel/Terminal/TTY.cpp +++ b/kernel/kernel/Terminal/TTY.cpp @@ -424,7 +424,7 @@ namespace Kernel const auto termios = get_termios(); - SpinLockGuard _1(m_write_lock); + LockGuard _(m_write_lock); if (termios.c_oflag & OPOST) { if ((termios.c_oflag & ONLCR) && ch == NL) @@ -471,15 +471,13 @@ namespace Kernel BAN::ErrorOr TTY::write_impl(off_t, BAN::ConstByteSpan buffer) { - SpinLockGuard write_guard(m_write_lock); + LockGuard write_guard(m_write_lock); while (!can_write()) { if (master_has_closed()) return BAN::Error::from_errno(EIO); - - SpinLockGuardAsMutex smutex(write_guard); - TRY(Thread::current().block_or_eintr_indefinite(m_write_blocker, &smutex)); + TRY(Thread::current().block_or_eintr_indefinite(m_write_blocker, &m_write_lock)); } size_t written = 0; @@ -497,7 +495,7 @@ namespace Kernel void TTY::putchar_current(uint8_t ch) { ASSERT(s_tty); - SpinLockGuard _(s_tty->m_write_lock); + LockGuard _(s_tty->m_write_lock); s_tty->putchar(ch); s_tty->after_write(); } diff --git a/kernel/kernel/Terminal/VirtualTTY.cpp b/kernel/kernel/Terminal/VirtualTTY.cpp index 713c29b7..a5999f97 100644 --- a/kernel/kernel/Terminal/VirtualTTY.cpp +++ b/kernel/kernel/Terminal/VirtualTTY.cpp @@ -61,7 +61,7 @@ namespace Kernel void VirtualTTY::clear() { - SpinLockGuard _(m_write_lock); + LockGuard _(m_write_lock); for (uint32_t i = 0; i < m_width * m_height; i++) m_buffer[i] = { .foreground = m_foreground, .background = m_background, .codepoint = ' ' }; m_terminal_driver->clear(m_background); @@ -73,7 +73,7 @@ namespace Kernel return BAN::Error::from_errno(EINVAL); { - SpinLockGuard _(m_write_lock); + LockGuard _(m_write_lock); TRY(m_terminal_driver->set_font(BAN::move(font))); @@ -110,7 +110,7 @@ namespace Kernel void VirtualTTY::reset_ansi() { - ASSERT(m_write_lock.current_processor_has_lock()); + ASSERT(m_write_lock.is_locked_by_current_thread()); m_ansi_state = { .nums = { -1, -1, -1, -1, -1 }, .index = 0, @@ -121,7 +121,7 @@ namespace Kernel void VirtualTTY::handle_ansi_csi_color(uint8_t value) { - ASSERT(m_write_lock.current_processor_has_lock()); + ASSERT(m_write_lock.is_locked_by_current_thread()); switch (value) { case 0: @@ -201,7 +201,7 @@ namespace Kernel void VirtualTTY::handle_ansi_csi(uint8_t ch) { - ASSERT(m_write_lock.current_processor_has_lock()); + ASSERT(m_write_lock.is_locked_by_current_thread()); switch (ch) { case '0': case '1': case '2': case '3': case '4': @@ -446,7 +446,7 @@ namespace Kernel void VirtualTTY::render_from_buffer(uint32_t x, uint32_t y) { - ASSERT(m_write_lock.current_processor_has_lock()); + ASSERT(m_write_lock.is_locked_by_current_thread()); ASSERT(x < m_width && y < m_height); const auto& cell = m_buffer[y * m_width + x]; m_terminal_driver->putchar_at(cell.codepoint, x, y, cell.foreground, cell.background); @@ -454,7 +454,7 @@ namespace Kernel void VirtualTTY::putchar_at(uint32_t codepoint, uint32_t x, uint32_t y) { - ASSERT(m_write_lock.current_processor_has_lock()); + ASSERT(m_write_lock.is_locked_by_current_thread()); ASSERT(x < m_width && y < m_height); auto& cell = m_buffer[y * m_width + x]; cell.codepoint = codepoint; @@ -488,7 +488,7 @@ namespace Kernel void VirtualTTY::putcodepoint(uint32_t codepoint) { - ASSERT(m_write_lock.current_processor_has_lock()); + ASSERT(m_write_lock.is_locked_by_current_thread()); switch (codepoint) { @@ -534,7 +534,7 @@ namespace Kernel bool VirtualTTY::putchar_impl(uint8_t ch) { - ASSERT(m_write_lock.current_processor_has_lock()); + ASSERT(m_write_lock.is_locked_by_current_thread()); uint32_t codepoint = ch;