Added lockfree bounded MPMC queue (Vyukov/Strauss-style) with unique_ptr wrapper, GTest suite, and sanitizer builds - #11
Merged
Conversation
Removed MPSC test execution and added unbounded and bounded MPMC tests.
Contributor
Author
|
Build is failing because, This queue required 128 bit CAS Operations. |
rviz190606
pushed a commit
to rviz190606/ThreadsafeQueueLib
that referenced
this pull request
Jun 5, 2026
Did Requested Changes 23 March, 2026
Collaborator
|
Asked AI and suggested to try this. Try and then lmk if this works:
|
Contributor
Author
|
Added -mcx16 Flag. Now works on Github workflows. |
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
Adds a new lockfree bounded MPMC queue (tsfqueue::impl::lockfree_mpmc_bounded<T, N>)
under include/lockfree_mpmc_bounded/, a unique_ptr wrapper around it, a 9-test
GTest suite, and TSAN/ASAN/UBSAN build options.
A bounded, lock-free, multi-producer / multi-consumer ring buffer in the spirit
of Vyukov's MPMC bounded queue + Erez Strauss's packed-entry optimization.
Each slot is a single 64-bit
entrypacking both {value, index}, so the slot'sstate can be CAS'd atomically in one shot - no per-slot mutex, no separate
sequence counter word.
Algorithm in brief:
Two atomic counters: head_index (consumer cursor) and tail_index (producer cursor).
Each slot stores {value, index} packed into a single atomic word.
Push:
then CAS-bump tail_index from t to t+1 (helping rule).
help-bump tail_index and retry.
on API).
Pop:
{empty, h+N} (mark it free for the next lap), then bump head_index to h+1.
Single-word slot CAS + lap-encoded indices give wait-free progress per operation
and no ABA risk (the lap stride is N, so indices advance monotonically modulo
the full index_type range).
N must be a power of two so & (N-1) replaces % N.
lockfree_mpmc_bounded_unique_ptr<T, N> wraps the raw queue so callers can move
heap-owned objects across threads safely. Internally it stores the released raw
pointer as uint64_t. push releases on success, pop reconstructs a
std::unique_ptr via reset, and the destructor drains any remaining entries
so nothing leaks on queue teardown.
9 GTest cases across correctness, linearizability, memory safety, and lifecycle:
All 9 pass under: normal build, TSAN, and ASAN.
A short test-running cheat sheet is included at
tests/guide_to_run_tests/TEST_MPMC_BOUNDED_LOCKFREE.md.
New CMake options (TSAN and ASAN are mutually exclusive):
cmake -DENABLE_TSAN=ON .. # ThreadSanitizer (data-race detection)
cmake -DENABLE_ASAN=ON .. # AddressSanitizer (UAF, OOB, leaks)
cmake -DENABLE_UBSAN=ON .. # UndefinedBehaviorSanitizer
Tests auto-scale iteration counts down under TSAN (via TSFQUEUE_TSAN_BUILD)
so the suite still finishes in seconds.
.github/workflows/tests.yaml:
Removed the MPSC step. tests/test_mpsc.cpp is currently empty (no code), so
there is no test_mpsc binary to run - keeping the step would fail CI. Removed
for now so the workflow stays green; can be re-added when an MPSC test file
is actually written.
Replaced the now-stale ./test_mpmc invocation with the two new binaries
(./test_mpmc_unbounded_blocking, ./test_mpmc_bounded_lockfree) and updated
the changed-file detection to match the new test filenames.