From e3d8fe4854a7ef7ff0637688cda9e65feb680122 Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Thu, 15 Oct 2026 12:37:10 +0300 Subject: Replace the writer spin lock with a blocking mutex term_write_begin spun on an atomic sequence word. A waiter burned CPU, and with no priority inheritance a SCHED_FIFO waiter could starve a preempted holder on the same CPU. The writer section is now a pthread mutex with PTHREAD_PRIO_INHERIT where the platform has it, and an SRWLOCK on Windows. Re-entry on the owning thread still only counts the depth. The platform wrapper (init, lock, unlock, destroy) is in compat.h. term_new initializes the mutex, and term_free destroys it. Meson links the thread library into the core, term_dep, and term_internal_dep. The README, term.h, and the amalgamation note say that POSIX needs -pthread. Tests: the feed and snapshot thread test now checks the final screen and history against a serial reference. A new test checks that term_lock nests and that another thread waits for the last term_unlock. Co-Authored-By: Claude Haiku 5.5 --- include/term/term.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'include/term') diff --git a/include/term/term.h b/include/term/term.h index dc41114..b9f3843 100644 --- a/include/term/term.h +++ b/include/term/term.h @@ -17,8 +17,9 @@ * * Threads. Any thread can call any function. Each call that reads or changes * a terminal enters its writer section, so calls on one terminal run one at a - * time. A snapshot belongs to one thread at a time. term_free() is the last - * call on a terminal. + * time. A thread that waits for the section sleeps. On POSIX the section is a + * mutex with priority inheritance where the platform has it. A snapshot + * belongs to one thread at a time. term_free() is the last call on a terminal. */ #include @@ -169,9 +170,10 @@ TERM_API struct term *term_new(const struct term_options *opt); TERM_API void term_free(struct term *t); /* Hold the writer section across several calls, so that no other thread - * changes the terminal between them. The section is a spin lock: keep it - * short. The thread that holds it can call every function, and it can enter - * the section again. */ + * changes the terminal between them. The section is a mutex, and a waiting + * thread sleeps. Keep it short. The thread that holds it can call every + * function, and it can enter the section again. Each term_lock() needs one + * term_unlock(). */ TERM_API void term_lock(struct term *t); TERM_API void term_unlock(struct term *t); -- cgit v1.2.3