Files
NX2navi/runtime/logq.h
chrg6 5da5a48a90 Write the log from a thread of its own, so a stalled write no longer holds the thread that logged.
Every log line was two writes on the thread that logged it, to stderr and to the log file. Such a write can stall for
tens or hundreds of milliseconds (a busy disk, a virus scanner, a console or a pipe that is slow to take it), and the
render thread logs too: a frame it spent in a write was a hitch with nothing else to show for it.

In a game run the lines now go into a queue that a log thread writes out in order (runtime/logq.c). A fatal error,
the crash report and the end of the process write the queue out on their own thread first and write their own lines
directly, so nothing is lost to an exit. A thread that finds the queue's lock held for long, or the queue full, writes
its line itself. NX2NAVI_LOG_ASYNC=0 writes every line where it is logged, as before.

Every write is timed in both modes: a "log: ... writes of the log took over 2 ms" line counts the slow ones, gives
the longest and says whose time they took.

tests/logq_check.py checks order and completeness with eight threads, a slow destination, a stopped destination with
the queue full, and the synchronous flush.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-10 07:30:44 -05:00

11 lines
730 B
C

// Log writes on a thread of their own (logq.c). main.c owns the log's destinations and hands every line here.
#pragma once
#include <stddef.h>
typedef void (*LogqSink)(const char *p, size_t n); // writes the bytes to the log's destinations; may block
void logq_init(LogqSink sink); // before the first logq_write; lines are written by the thread that logs them
int logq_async(void); // from now on the log thread writes them; 0 when it could not be started
void logq_write(const char *p, size_t n); // queues the bytes, or writes them when the queue is off, full or busy
void logq_sync(void); // writes what is queued, on the calling thread, and turns the queue off