Skip to content

typestates/segment_state

segment_state defines the segment-level lifecycle typestate machine used by the unbounded Queue's linked-segment ring. It encodes the allowed segment transitions (allocated → linked → drained → retired) so that the queue body's segment-advance and reclamation paths are statically checked.

Downstream code does not normally interact with segment_state directly; it is documented here to make the umbrella's segment lifecycle contract visible.

See also

segment_state

Segment-state predicate for the unbounded MPSC arm.

Companion to the two slot-state predicate families in slot_state.nim, covering the MPSC committed-segment predicate (the linked-segment destructor walk).

Substrate: Segment[T, ccMulti, ccSingle, S].committed: array[S, Atomic[bool]] — the committed-flag overlay on the MPSC arm (declared in queue.nim on the (ccMulti × ccSingle) shape).

Current call sites: NONE in v0.1.0. The first consumer is the segment destructor walk, which walks committed[i] to drop any values committed by the producer but not yet read by the consumer when the segment is reclaimed.

Why a sibling module rather than a section in slot_state.nim? Orthogonality rationale: three substrates → three predicate families. The MPSC committed[i] is a segment-overlay flag with different lifetime semantics from the per-slot seq counters used by Families A and B. Keeping it lexically distinct makes accidental cross-substrate reuse impossible.

Import note: this module imports queue.nim (and is itself NOT imported by queue.nim in v0.1.0), so there is no cycle.

slotIsCommittedAndUnread inline

proc slotIsCommittedAndUnread(seg: var Segment[T, ccMulti, ccSingle, S]; slot: int; prevConsumerIdx: int): bool

Returns true when slot is at or past the consumer's

last-read index AND the producer has flipped its committed[slot] flag to true. The relaxed load suffices because the destructor walk runs only when the segment is being reclaimed — all producers and the consumer have already quiesced by EBR/manual reclamation guarantees, so no in-flight publish can race the walk.

Parameters
  • seg (var Segment[T, ccMulti, ccSingle, S])
  • slot (int)
  • prevConsumerIdx (int)
Returns

bool