beginWrite method

Pointer<Uint8>? beginWrite()

The slot to fill: any that is neither the newest complete one nor the one a reader is holding.

With three slots at most two are excluded, so this always finds one and the writer never waits on a reader. The null return exists only so a mis-sized buffer fails visibly rather than corrupting; it is unreachable at slotCount 3.

Implementation

Pointer<Uint8>? beginWrite() {
  final ready = _control[_readyWord];
  final reading = _control[_readingWord];
  for (var slot = 0; slot < slotCount; slot++) {
    if (slot == ready || slot == reading) continue;
    _writeSlot = slot;
    return _slots[slot];
  }
  return null;
}