Files
NX2navi/runtime/logq.h
chrg6 c1951d2b37 Keep a thread's log lines in order when the log queue's lock is taken.
A thread that found the queue's lock taken for 4,000 spins wrote its line itself, ahead of the lines it had queued
earlier. On a busy machine the lock's holder loses the processor inside the copy and the lock stays taken for a time
slice, so one thread's lines reached the log out of order. A line now waits for the lock, and a line that finds no
room, or is larger than the queue, is written after everything queued before it.

tests/logq_check.py failed on this in 8 of 30 runs beside four busy processes and passes 100 of 100 now. It gained a
check that holds the queue's locks itself and fails on the old code every time, and it no longer depends on time: it
waits for the queue to empty, keeps the line about slow writes back except in the step that checks it, and counts the
lines that waited instead of timing the slowest.

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

11 lines
731 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 or has no room
void logq_sync(void); // writes what is queued, on the calling thread, and turns the queue off