MpscRingBuffer

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

A high-performance, non-blocking Multi-Producer Single-Consumer (MPSC) ring buffer for generic reference types.

Multiple threads may call offer concurrently, but only a single consumer thread may call take. This is enforced by contract — no runtime check is performed for the single-consumer invariant.

The algorithm follows the JCTools MpscArrayQueue design:

  • Producers claim a slot via CAS on producerIndex, then write the element with release semantics.
  • The consumer reads elements using acquire semantics. A null slot indicates either an empty queue or a producer that has claimed a slot but has not yet written the element (the "FastFlow" pattern). In the latter case, take returns null rather than spinning, following relaxedPoll semantics.
  • A cached producerLimit avoids reading consumerIndex on every offer, reducing cross-core traffic.

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
MpscRingBuffer.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. Uses relaxed poll semantics: stops at the first null slot (either empty or producer mid-write).

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
MpscRingBuffer.scala
def isEmpty: Boolean

Returns true if the buffer appears to contain no elements (approximate under concurrency).

Returns true if the buffer appears to contain no elements (approximate under concurrency).

Attributes

Source
MpscRingBuffer.scala
def isFull: Boolean

Returns true if the buffer appears to be full (approximate under concurrency).

Returns true if the buffer appears to be full (approximate under concurrency).

Attributes

Source
MpscRingBuffer.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.

Any thread may call this method concurrently. The implementation uses a CAS loop to claim a slot in the circular buffer, then writes the element with release semantics.

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

Source
MpscRingBuffer.scala
def size: Int

Returns the approximate number of elements currently in the buffer.

Returns the approximate number of elements currently in the buffer.

Under concurrent access this value is a snapshot and may be stale by the time the caller observes it.

Attributes

Source
MpscRingBuffer.scala
def take(): A

Tries to remove an element from the buffer without blocking.

Tries to remove an element from the buffer without blocking.

Uses the FastFlow pattern: reads the array slot directly with acquire semantics. A null value means either the buffer is empty or a producer has claimed the slot but has not yet written the element. In both cases, null is returned (relaxed poll semantics).

Attributes

Returns

the element, or null if the buffer is empty (or a producer is mid-write)

Note

Must be called from the consumer thread only.

Source
MpscRingBuffer.scala

Concrete fields

val capacity: Int

Attributes

Source
MpscRingBuffer.scala

Inherited fields

var consumerIndex: Long

Attributes

Inherited from:
MpscConsumerFields (hidden)
Source
MpscRingBuffer.scala
var p00: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p01: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p02: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p03: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p04: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p05: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p06: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p07: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p08: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p09: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p0a: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p0b: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p0c: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p0d: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p0e: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p0f: Long

Attributes

Inherited from:
MpscPad0 (hidden)
Source
MpscRingBuffer.scala
var p10: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p11: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p12: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p13: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p14: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p15: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p16: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p17: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p18: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p19: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p1a: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p1b: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p1c: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p1d: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p1e: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p1f: Long

Attributes

Inherited from:
MpscPad1 (hidden)
Source
MpscRingBuffer.scala
var p20: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p21: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p22: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p23: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p24: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p25: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p26: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p27: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p28: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p29: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p2a: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p2b: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p2c: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p2d: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p2e: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p2f: Long

Attributes

Inherited from:
MpscPad2 (hidden)
Source
MpscRingBuffer.scala
var p30: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p31: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p32: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p33: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p34: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p35: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p36: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p37: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p38: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p39: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p3a: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p3b: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p3c: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p3d: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p3e: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var p3f: Long

Attributes

Inherited from:
MpscPad3 (hidden)
Source
MpscRingBuffer.scala
var producerIndex: Long

Attributes

Inherited from:
MpscProducerFields (hidden)
Source
MpscRingBuffer.scala
var producerLimit: Long

Attributes

Inherited from:
MpscProducerLimitFields (hidden)
Source
MpscRingBuffer.scala