diff options
| author | Berke Güzel <wenekar1@gmail.com> | 2026-10-15 12:37:10 +0300 |
|---|---|---|
| committer | Berke Güzel <wenekar1@gmail.com> | 2026-10-15 12:37:10 +0300 |
| commit | e3d8fe4854a7ef7ff0637688cda9e65feb680122 (patch) | |
| tree | 56bc226a49b7a706e078923434cb9fb05bab6e21 /src/internal.h | |
| parent | 5f03033b0e211f9ed13e7a1351f9a8f9011ab665 (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'src/internal.h')
| -rw-r--r-- | src/internal.h | 18 |
1 files changed, 4 insertions, 14 deletions
diff --git a/src/internal.h b/src/internal.h index 8c02c91..d67e67e 100644 --- a/src/internal.h +++ b/src/internal.h @@ -108,7 +108,8 @@ struct resize_scratch { struct term { - _Atomic uint64_t seq; + /* The writer section. A waiting thread sleeps on it. */ + term_mutex write_lock; _Atomic uint64_t writer_tid; uint32_t write_depth; int cols; @@ -375,18 +376,7 @@ static inline void term_write_begin(struct term *t) t->write_depth++; return; } - uint64_t s; - for (;;) { - s = atomic_load_explicit(&t->seq, memory_order_relaxed); - if (s & 1) { - term_cpu_relax(); - continue; - } - if (atomic_compare_exchange_weak_explicit(&t->seq, &s, s + 1, - memory_order_acquire, - memory_order_relaxed)) - break; - } + term_mutex_lock(&t->write_lock); atomic_store_explicit(&t->writer_tid, self, memory_order_relaxed); t->write_depth = 1; } @@ -396,7 +386,7 @@ static inline void term_write_end(struct term *t) if (!t) return; if (--t->write_depth == 0) { atomic_store_explicit(&t->writer_tid, 0, memory_order_relaxed); - atomic_fetch_add_explicit(&t->seq, 1, memory_order_release); + term_mutex_unlock(&t->write_lock); } } |
