Skip to content

feat(userland): add stlxstd C++ threading library with futex-based sync primitives#124

Merged
FlareCoding merged 6 commits intomasterfrom
cursor/llama-cpp-kernel-context-0b94
Apr 3, 2026
Merged

feat(userland): add stlxstd C++ threading library with futex-based sync primitives#124
FlareCoding merged 6 commits intomasterfrom
cursor/llama-cpp-kernel-context-0b94

Conversation

@FlareCoding
Copy link
Copy Markdown
Owner

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_ALL
  • stlx_mutex_t — three-state mutex (Drepper algorithm): atomic CAS fast path, futex slow path
  • stlx_cond_t — sequence-counter condition variable
  • stlx_barrier_t — generation-counter reusable barrier

C++ Layer (new libstlxcxx)

  • stlxstd::thread — RAII thread with mmap'd stack, join/detach, type-erased callable support
  • stlxstd::mutex — wraps stlx_mutex_t
  • stlxstd::lock_guard<M> / unique_lock<M> — RAII lock management
  • stlxstd::condition_variable — wraps stlx_cond_t with predicated wait
  • stlxstd::barrier — wraps stlx_barrier_t

Test App

  • synctest — validates all primitives: mutex stress (8 threads × 10K iterations), condvar producer/consumer, barrier synchronization, thread join/detach, multi-mutex independence

Kernel Fix

  • TLS inheritance for user threadscreate_user_thread now 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

  • Uncontended mutex lock/unlock is a single atomic CAS — zero syscalls
  • stlxstd::thread detach intentionally leaks the stack (freed at process exit) — can't munmap a stack you're running on
  • Thread callable dispatch uses function pointer (no virtual/RTTI dependency)
  • Apps opt into libstlxcxx via APP_LIBS := stlxcxx in their Makefile
  • All libstlx C headers now have extern "C" guards for C++ linkage

Testing

  • All 509 kernel unit tests pass
  • Both x86_64 and aarch64 userland builds succeed (40/40 apps)
  • synctest passes all 6 tests on Stellux x86_64
Open in Web Open in Cursor 

cursoragent and others added 4 commits April 3, 2026 21:45
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>
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ 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>
@FlareCoding FlareCoding merged commit c682e45 into master Apr 3, 2026
15 checks passed
@FlareCoding FlareCoding deleted the cursor/llama-cpp-kernel-context-0b94 branch April 3, 2026 23:17
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.

2 participants