code wiki / (root) / nx_chan.nx

nx_chan.nx

buildroot/runtime/nx_chan.nx

8265 B250 linesdepth 5pulls 6 transitivereach 200 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_atom.nx nx_thread.nx nx_chan.nx nx_chan_mpmc_test.nx nx_pipeline.nx nx_thread_pool.nx

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

main nx_chan_new sys_mmap _nx_chan_cell nx_chan_len nx_chan_try_send _nx_chan_cell ↻ sys_mmap ↻ nx_chan_try_recv _nx_chan_cell ↻

structs

40struct NxChanCell
47struct NxChan

consts

45const NX_CHAN_CELL_BYTES: i64 = 16
54const NX_CHAN_HEADER_BYTES: i64 = 64

functions

57func _nx_chan_cell(c: *NxChan, idx: i64) -> *NxChanCell
65func nx_chan_new(cap_hint: i64) -> *NxChan
101func nx_chan_try_send(c: *NxChan, v: i64) -> i64
called by 2: nx_chan_sendmain calls 1: _nx_chan_cell
133func nx_chan_try_recv(c: *NxChan, out: *i64) -> i64
166func nx_chan_send(c: *NxChan, v: i64) -> i64
176func nx_chan_recv(c: *NxChan) -> i64
188func nx_chan_len(c: *NxChan) -> i64
198func main() -> i64