nx_chan.nx
buildroot/runtime/nx_chan.nx
about
nx_chan.nx -- Vyukov MPMC bounded lock-free queue (i64 messages).
Multi-Producer Multi-Consumer queue based on Dmitry Vyukov's
2010 design ("Bounded MPMC queue", 1024cores.net). Each cell
carries a sequence number; producer sees `seq == enq_pos` to
claim a slot, consumer sees `seq == deq_pos + 1` to drain.
CAS bumps the position counter. Wait-free under no contention,
lock-free under contention.
Properties:
* MPMC: any number of producers + any number of consumers
* Bounded: capacity rounded up to power of 2 (mask-wrap)
* Lock-free: no spin locks, no futex waits in fast path
* FIFO order globally
Compose-against: [[atomic_intrinsics_real_amo]] for CAS+FAA;
[[thread_clone_native_trampoline]] for the worker threads;
[[nx_hw_dynamic_probes]] for sizing producer/consumer count.
Predecessor nx_channel.nx remains for SPSC code (smaller cell
footprint, no per-cell seq); use nx_chan.nx whenever multiple
threads can produce or consume.
Reference: Vyukov D., "Bounded MPMC queue", 2010
(independent rederive from algorithm description; no code copied).
dependencies 3 imports · 3 importers
imports: nx_syscalls.nxnx_atom.nxnx_thread.nx
imported by: nx_chan_mpmc_test.nxnx_pipeline.nxnx_thread_pool.nx
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| 40 | struct NxChanCell |
| 47 | struct NxChan |
consts
| 45 | const NX_CHAN_CELL_BYTES: i64 = 16 |
| 54 | const NX_CHAN_HEADER_BYTES: i64 = 64 |
functions
| 57 | func _nx_chan_cell(c: *NxChan, idx: i64) -> *NxChanCell |
| 65 | func nx_chan_new(cap_hint: i64) -> *NxChan |
| 101 | func nx_chan_try_send(c: *NxChan, v: i64) -> i64 |
| 133 | func nx_chan_try_recv(c: *NxChan, out: *i64) -> i64 called by 5: nx_chan_recvmainconsumer_mainnx_pipeline_try_recv_nx_pool_worker_main calls 1: _nx_chan_cell |
| 166 | func nx_chan_send(c: *NxChan, v: i64) -> i64 |
| 176 | func nx_chan_recv(c: *NxChan) -> i64 |
| 188 | func nx_chan_len(c: *NxChan) -> i64 |
| 198 | func main() -> i64 |