Skip to content

feat(#494): C11 threads.h backend with auto-detection and fallback#503

Merged
justinjoy merged 6 commits intomainfrom
feat/494-c11-threads
Apr 15, 2026
Merged

feat(#494): C11 threads.h backend with auto-detection and fallback#503
justinjoy merged 6 commits intomainfrom
feat/494-c11-threads

Conversation

@justinjoy
Copy link
Copy Markdown
Collaborator

Summary

  • Add wirelog/thread_c11.c implementing C11 <threads.h> API (thrd_create/mtx_*/cnd_*) with trampoline to bridge void*(*)(void*) to int(*)(void*) signature
  • Update wirelog/thread.h with 3-tier #ifdef cascade: WL_HAVE_C11_THREADS > _WIN32 > POSIX
  • Meson auto-detects C11 threads via cc.links() with actual thrd_create call (not just header check)
  • io_adapter.c: Add C11 call_once + mtx_init with error flag propagation for lazy mutex init
  • Migrate test_io_adapter_concurrent.c and test_mem_ledger.c from raw pthread_* to thread.h abstraction
  • POSIX and MSVC backends unchanged; macOS continues to use POSIX fallback

Platform activation matrix

Platform Backend CI coverage
Linux GCC 13+ C11 <threads.h> ubuntu-latest/gcc
Linux Clang 15+ C11 <threads.h> ubuntu-latest/clang
macOS Apple Clang POSIX pthreads (fallback) macos-latest/clang
Windows MSVC MSVC (unchanged) windows-latest/msvc
ARM64 Linux C11 <threads.h> ubuntu-24.04-arm/gcc

Scope

Threading migration only (DoD items 1-2). _GNU_SOURCE/qsort_r/__builtin_* intrinsics deferred to separate issue.

Test plan

  • macOS (POSIX fallback): 135/135 PASS
  • Linux Docker C11 backend: 134/134 PASS
  • Meson detection: cc.links() returns YES on Linux GCC, NO on macOS Apple Clang
  • io_adapter.c call_once path: error flag propagation verified
  • Test files: zero residual pthread_create/pthread_join references

Ralplan consensus

  • Planner: Option A (C11 + fallback) selected; Option B (keep POSIX only) rejected
  • Architect: APPROVE (v2) — call_once + mtx_init is correct C11 pattern
  • Critic: ACCEPT — platform matrix validated, scope limitation sound

Closes #494

Add thread_c11.c implementing C11 thrd_create/mtx_*/cnd_* API with a
trampoline to bridge void*(*)(void*) to int(*)(void*). Update thread.h
with 3-tier ifdef cascade (C11 > Win32 > POSIX). Meson detects C11
support via cc.links() with actual thrd_create call. io_adapter.c uses
call_once + mtx_init for lazy mutex init on C11 path with error flag
propagation. POSIX and MSVC backends unchanged.
Replace direct pthread_create/pthread_join/pthread_t usage in
test_io_adapter_concurrent.c and test_mem_ledger.c with the thread.h
abstraction layer (thread_create/thread_join/thread_t). Enables these
tests to run on the C11 threads backend without modification.
Document C11 <threads.h> backend addition with auto-detection and
POSIX/MSVC fallback under [Unreleased] section.
@justinjoy justinjoy force-pushed the feat/494-c11-threads branch from a2e7e41 to f70f6cd Compare April 14, 2026 10:31
Replace hardcoded thread_posix.c/thread_msvc.c selection in
tests/meson.build and bench/meson.build with wirelog_thread_src
from the parent scope. Fixes type mismatch when C11 threads backend
is active (mtx_t vs pthread_mutex_t under TSan).
TSan does not track happens-before through glibc call_once for
non-atomic variables. Use atomic_store/atomic_load on the init
flag to make the synchronization explicit and TSan-clean.
TSan only intercepts POSIX pthread calls; C11 <threads.h> functions
(thrd_create, mtx_lock) are uninstrumented, causing false SEGV.

Split threads option into native (auto-detect C11/POSIX) and posix
(force pthreads). CI TSan steps now use -Dthreads=posix to ensure
proper instrumentation. Remove disabled option since threading is
always required.
@justinjoy justinjoy merged commit fb56811 into main Apr 15, 2026
7 checks passed
@justinjoy justinjoy deleted the feat/494-c11-threads branch April 15, 2026 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: Migrate from POSIX pthreads to C11 <threads.h>

1 participant