SpscRingBuffer

zio.blocks.ringbuffer.SpscRingBuffer
See theSpscRingBuffer companion object
final class SpscRingBuffer[A <: AnyRef](val capacity: Int)

A high-performance, non-blocking SPSC ring buffer for generic reference types.

Uses the FastFlow pattern: the array element's null/non-null state IS the synchronization signal. The producer never reads consumerIndex; the consumer never reads producerIndex. This minimizes cross-core cache traffic.

Optimized for SPSC (Single Producer, Single Consumer). Only one producer thread and one consumer thread should access the buffer concurrently.

Null elements are not permitted. offer(null) throws NullPointerException.

Value parameters

capacity

the buffer capacity, must be a positive power of two

Attributes

Companion
object
Source
SpscRingBuffer.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def drain(consumer: A => Unit, limit: Int): Int

Drains up to limit elements from the buffer, passing each to the given consumer callback.

Drains up to limit elements from the buffer, passing each to the given consumer callback.

Elements are consumed in FIFO order. The consumer index is advanced after each element is passed to the callback, so if the callback throws, all previously consumed elements remain consumed and the buffer is in a consistent state.

Value parameters

consumer

the callback invoked for each drained element

limit

the maximum number of elements to drain; must be non-negative

Attributes

Returns

the number of elements actually drained (0 if the buffer is empty)

Throws
java.lang.IllegalArgumentException

if limit is negative

Note

Must be called from the consumer thread only.

Source
SpscRingBuffer.scala
def fill(supplier: () => A, limit: Int): Int

Fills up to limit slots in the buffer by calling the supplier for each new element.

Fills up to limit slots in the buffer by calling the supplier for each new element.

Elements are inserted in order. The producer index is advanced after each element is stored. If the supplier throws, all previously inserted elements remain in the buffer.

Value parameters

limit

the maximum number of elements to insert; must be non-negative

supplier

the callback invoked to produce each new element; must not return null

Attributes

Returns

the number of elements actually inserted (0 if the buffer is full)

Throws
java.lang.IllegalArgumentException

if limit is negative

java.lang.NullPointerException

if the supplier returns null

Note

Must be called from the producer thread only.

Source
SpscRingBuffer.scala
def isEmpty: Boolean

Returns true if the buffer contains no elements (approximate under concurrency).

Returns true if the buffer contains no elements (approximate under concurrency).

Attributes

Source
SpscRingBuffer.scala
def isFull: Boolean

Returns true if the buffer is full (approximate under concurrency).

Returns true if the buffer is full (approximate under concurrency).

Attributes

Source
SpscRingBuffer.scala
def offer(a: A): Boolean

Tries to insert an element into the buffer without blocking.

Tries to insert an element into the buffer without blocking.

Value parameters

a

the element to insert; must not be null

Attributes

Returns

true if the element was successfully inserted, false if the buffer is full

Throws
java.lang.NullPointerException

if the element is null

Note

Must be called from the producer thread only.

Source
SpscRingBuffer.scala
def size: Int

Returns the number of elements currently in the buffer (approximate under concurrency).

Returns the number of elements currently in the buffer (approximate under concurrency).

Attributes

Source
SpscRingBuffer.scala
def take(): A

Tries to remove an element from the buffer without blocking. Uses FastFlow: reads the array slot directly. Null = empty, non-null = data available.

Tries to remove an element from the buffer without blocking. Uses FastFlow: reads the array slot directly. Null = empty, non-null = data available.

Attributes

Returns

the element, or null if the buffer is empty

Note

Must be called from the consumer thread only.

Source
SpscRingBuffer.scala

Concrete fields

val capacity: Int

Attributes

Source
SpscRingBuffer.scala

Inherited fields

var consumerIndex: Long

Attributes

Inherited from:
SpscConsumerFields (hidden)
Source
SpscRingBuffer.scala
var p00: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p01: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p02: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p03: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p04: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p05: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p06: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p07: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p08: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p09: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p0a: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p0b: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p0c: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p0d: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p0e: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p0f: Long

Attributes

Inherited from:
SpscPad0 (hidden)
Source
SpscRingBuffer.scala
var p10: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p11: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p12: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p13: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p14: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p15: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p16: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p17: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p18: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p19: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p1a: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p1b: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p1c: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p1d: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p1e: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p1f: Long

Attributes

Inherited from:
SpscPad1 (hidden)
Source
SpscRingBuffer.scala
var p20: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p21: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p22: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p23: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p24: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p25: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p26: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p27: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p28: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p29: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p2a: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p2b: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p2c: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p2d: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p2e: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var p2f: Long

Attributes

Inherited from:
SpscPad2 (hidden)
Source
SpscRingBuffer.scala
var producerIndex: Long

Attributes

Inherited from:
SpscProducerFields (hidden)
Source
SpscRingBuffer.scala
var producerLimit: Long

Attributes

Inherited from:
SpscProducerFields (hidden)
Source
SpscRingBuffer.scala