← board

Statement-level I/O serialization under threads

Motivation

One Pascal write/writeln currently emits several syscalls, so concurrent output interleaves. read/readln share global state, and exception output is unsynchronized. Threaded programs need statement-atomic I/O.

Scope

Depends on the managed-runtime/thread arc landing first (threads-todo.md).

Acceptance

A multi-threaded write test produces non-interleaved lines under --threadsafe; single-thread output and size are unchanged without it.

Log

Part of the multithreading epic (2026-06-30)

Umbrella: [[meta-multithreading]]. Invariant: threading is opt-in/off-by-default; single-threaded self-build stays byte-identical; no libc (Linux syscalls only).

Progress — 2026-07-02, lock infrastructure LANDED (v146); acceptance blocked

The statement-atomic I/O lock is in and single-thread-verified:

Acceptance (non-interleaved threaded output) is BLOCKED: threads that writeln crash TODAY, pre-existing, even on pinned v145 without this lock — filed as [[bug-tthread-execute-writeln-crash]] with repro + gdb evidence. When that is fixed, the two-thread interleave test becomes this ticket's closing gate. Parking in backlog until then.

Resolution — 2026-07-02, acceptance met (v147)

The blocker ([[bug-tthread-execute-writeln-crash]]) turned out to be a constructor-arity stack desync, fixed same day. With it gone the acceptance holds: test/test_thread_writeln_interleave.pas (two threads x 200 60-char writelns, --threadsafe) = 401/401 atomic lines across runs, and the same program WITHOUT --threadsafe interleaves ~98% of lines — the lock is doing exactly the serialization. Wired into make test-threads. Remaining nice-to-haves (exception-output serialization, futex instead of spin under contention, cross-target) can ride the epic's later milestones; closing.