Skip to content

Commit 4e675d1

Browse files
committed
feat: implement true lock-free bounded queues with committed flags
Replace ordered commit pattern (blocking while loops) with per-slot committed flags for lock-free progress guarantees: - Mupsic (MPSC): Add committed flags array, producers CAS reservedTail and mark committed without blocking. Consumer checks committed before reading. Uses head/tail distance for fullness to prevent wraparound. - Sipmuc (SPMC): Add committed flags array, consumers CAS reservedHead and check committed before reading. Producer marks committed after writing. Uses committed flags for fullness check. - Mupmuc (MPMC): Add MupmucProducer type that uses reservedHead for fullness check instead of head. This prevents livelock when consumers complete out-of-order causing head to lag behind reservedHead. Key changes: - committed: array[N, Atomic[bool]] per-slot flags signal data readiness - Producers set committed[slot]=true after writing - Consumers check committed[slot] before reading, clear after consuming - Multi-consumer fullness uses reservedHead not head (avoids livelock) - All threaded tests pass under high contention This fixes race conditions where: 1. Ordered wait caused obstruction-free (not lock-free) behavior 2. Wraparound allowed producers to overwrite uncommitted slots 3. Stale head caused producers to think queue was full
1 parent d2a9e53 commit 4e675d1

27 files changed

Lines changed: 1455 additions & 1136 deletions

examples/audio_buffer.nim

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# lockfreequeues
2-
# © Copyright 2020 Elijah Shaw-Rutschman
3-
#
4-
# See the file "LICENSE", included in this distribution for details about the
5-
# copyright.
6-
71
## Audio Buffer Example
82
##
93
## Demonstrates using a bounded Sipsic (SPSC) queue for real-time audio processing.

examples/event_collector.nim

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# lockfreequeues
2-
# © Copyright 2020 Elijah Shaw-Rutschman
3-
#
4-
# See the file "LICENSE", included in this distribution for details about the
5-
# copyright.
6-
71
## Event Collector Example
82
##
93
## Demonstrates using an unbounded Mupsic (MPSC) queue for collecting events

examples/job_scheduler.nim

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# lockfreequeues
2-
# © Copyright 2020 Elijah Shaw-Rutschman
3-
#
4-
# See the file "LICENSE", included in this distribution for details about the
5-
# copyright.
6-
71
## Job Scheduler Example
82
##
93
## Demonstrates using an unbounded Mupmuc (MPMC) queue for a dynamic job

examples/mupmuc.nim

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# lockfreequeues
2-
# © Copyright 2020 Elijah Shaw-Rutschman
3-
#
4-
# See the file "LICENSE", included in this distribution for details about the
5-
# copyright.
6-
71
## Example usage of Mupmuc, a multi-producer, multi-consumer (MPMC) bounded queue.
82

93
import options

examples/mupsic.nim

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# lockfreequeues
2-
# © Copyright 2020 Elijah Shaw-Rutschman
3-
#
4-
# See the file "LICENSE", included in this distribution for details about the
5-
# copyright.
6-
71
## Example usage of Mupsic, a multi-producer, single-consumer (MPSC) bounded queue.
82

93
import options

examples/sipsic.nim

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# lockfreequeues
2-
# © Copyright 2020 Elijah Shaw-Rutschman
3-
#
4-
# See the file "LICENSE", included in this distribution for details about the
5-
# copyright.
6-
71
## Example usage of Sipsic, a single-producer, single-consumer (SPSC) bounded queue.
82

93
import options

examples/task_fanout.nim

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# lockfreequeues
2-
# © Copyright 2020 Elijah Shaw-Rutschman
3-
#
4-
# See the file "LICENSE", included in this distribution for details about the
5-
# copyright.
6-
71
## Task Fan-Out Example
82
##
93
## Demonstrates using a bounded Sipmuc (SPMC) queue for distributing work

mkdocs.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ plugins:
4848
docstring_style: rst
4949
show_source: true
5050
show_signature: true
51-
heading_level: 2
51+
heading_level: 3 # h3 so procs nest under h2 API section in TOC
52+
source_url: https://github.com/elijahr/lockfreequeues
53+
# source_ref auto-detected from git branch; set explicitly for releases
5254

5355
markdown_extensions:
5456
- pymdownx.highlight:
@@ -63,6 +65,7 @@ markdown_extensions:
6365
- md_in_html
6466
- toc:
6567
permalink: true
68+
toc_depth: 4 # Include h2, h3, h4 in right-side TOC
6669

6770
nav:
6871
- Home: index.md
@@ -85,7 +88,9 @@ nav:
8588

8689
watch:
8790
- docs
88-
- src # Reload when Nim source files change
91+
- docs/api
92+
- src
93+
- src/lockfreequeues
8994

9095
extra:
9196
version:

0 commit comments

Comments
 (0)