← board

Threadsafe heap — optimize + cross-target (M5)

Invariant

Threading is opt-in, off by default; the single-threaded self-build stays byte-identical. No libc — built on [[feature-pal-thread-primitives]] (syscalls only). Milestones land in any order under [[meta-multithreading]].

Scope

Today --threadsafe = a coarse global lock-prefix on every alloc/free + refcount, x86-64 only. Make it fast + portable:

Acceptance

Update — thread-safe heap VALIDATED under contention (2026-06-30)

test_thread_heap (lib/rtl + TThread): 4 threads x 12000 GetMem/FreeMem of 128B, each fills its block with a thread-unique tag and reads it back. WITH --threadsafe: 0 errors across runs (the existing x86-64 lock-prefixed spinlock around PXXAlloc/ PXXFree holds). WITHOUT --threadsafe: SIGSEGV every run — proving threaded allocation genuinely requires the flag (the M5 contract). In make test-threads (compiled --threadsafe). The optimisation part of M5 (per-thread arenas / lock-free fast path) is still open; correctness is now demonstrated + gated.