feat(userland): add stlxstd C++ threading library with futex-based sync primitives#124
Merged
FlareCoding merged 6 commits intomasterfrom Apr 3, 2026
Merged
Conversation
Futex wrappers for SYS_FUTEX_WAIT/WAKE/WAKE_ALL syscalls. Three-state mutex (Drepper algorithm) with atomic fast path. Sequence-counter condition variable. Generation-counter reusable barrier. Co-authored-by: Albert Slepak <FlareCoding@users.noreply.github.com>
…ar, barrier stlxstd::thread - RAII thread with mmap'd stack, join/detach support stlxstd::mutex - wraps stlx_mutex_t with lock/unlock/try_lock stlxstd::lock_guard / unique_lock - RAII lock management stlxstd::condition_variable - wraps stlx_cond_t with wait/notify stlxstd::barrier - wraps stlx_barrier_t with arrive_and_wait Type-erased thread callable via function pointer dispatch (no RTTI). Co-authored-by: Albert Slepak <FlareCoding@users.noreply.github.com>
…headers synctest validates stlxstd:: primitives: mutex stress (8 threads), condvar producer/consumer, barrier sync, thread join/detach, and multi-mutex independence. Added extern "C" guards to all libstlx C headers (futex.h, mutex.h, cond.h, barrier.h, proc.h) so C++ code links correctly against the C implementations. Co-authored-by: Albert Slepak <FlareCoding@users.noreply.github.com>
New user threads had tls_base=0, causing a null dereference on any FS-relative access (musl's errno, malloc internals, etc.). Since threads share the creator's address space, inheriting the TLS base gives them a valid FS segment for musl's thread-local storage. Co-authored-by: Albert Slepak <FlareCoding@users.noreply.github.com>
1. Barrier: eliminated split-update race between count reset and generation advance. Count now grows monotonically; last thread per round is identified by count % total == 0. No reset needed. 2. Thread: added destroy function pointer to thread_context so the callable's destructor runs before free(). Prevents resource leaks when lambdas capture RAII types by value. Co-authored-by: Albert Slepak <FlareCoding@users.noreply.github.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit c103a30. Configure here.
1. Thread: decay Fn so passing an lvalue callable stores a copy, not a dangling reference. Matches std::thread behavior. 2. Barrier: use uint64_t for monotonic count to prevent uint32 wrap from causing premature barrier release. Co-authored-by: Albert Slepak <FlareCoding@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Builds the userland synchronization stack on top of the kernel futex primitive (merged in #123). Three layers:
C Layer (libstlx extensions)
stlx_futex_wait/wake/wake_all— thin syscall wrappers for SYS_FUTEX_WAIT/WAKE/WAKE_ALLstlx_mutex_t— three-state mutex (Drepper algorithm): atomic CAS fast path, futex slow pathstlx_cond_t— sequence-counter condition variablestlx_barrier_t— generation-counter reusable barrierC++ Layer (new libstlxcxx)
stlxstd::thread— RAII thread with mmap'd stack, join/detach, type-erased callable supportstlxstd::mutex— wrapsstlx_mutex_tstlxstd::lock_guard<M>/unique_lock<M>— RAII lock managementstlxstd::condition_variable— wrapsstlx_cond_twith predicated waitstlxstd::barrier— wrapsstlx_barrier_tTest App
synctest— validates all primitives: mutex stress (8 threads × 10K iterations), condvar producer/consumer, barrier synchronization, thread join/detach, multi-mutex independenceKernel Fix
create_user_threadnow inherits the creator's TLS base instead of setting it to 0. Without this, any%fs-relative access (musl errno, malloc, etc.) in a child thread would null-deref.Design Decisions
stlxstd::threaddetach intentionally leaks the stack (freed at process exit) — can't munmap a stack you're running onAPP_LIBS := stlxcxxin their Makefileextern "C"guards for C++ linkageTesting