BlockingMpmcQueue

zio.blocks.streams.queues.BlockingMpmcQueue
final class BlockingMpmcQueue[A <: AnyRef](capacity: Int)

A blocking MPMC (multi-producer, multi-consumer) queue.

Backed by MpmcRingBuffer for the fast path; uses LockSupport.parkNanos for blocking when the buffer is full or empty.

Multiple threads may call offer and take concurrently.

==Single-Waiter Design==

Only one producer and one consumer can be fast-path woken at a time. The queue maintains a single producerWaiter and consumerWaiter volatile slot. When a thread blocks, it registers itself in the appropriate slot and calls LockSupport.parkNanos(this, 1000L) (1 microsecond timeout). When the opposite side has data/space, it unparks the registered waiter. Other waiting threads wake up within 1 microsecond via the timeout and retry.

This design is virtual-thread friendly: parkNanos does not OS-block on virtual threads, allowing bounded re-checks without risk of permanent parking.

==Happens-Before Guarantee==

The pattern producerWaiter = Thread.currentThread() (volatile write) followed by if (closed) return false (volatile read) ensures that a close() call setting closed = true is always visible before the thread parks. This prevents the race where a thread parks after close() has already been called.

==close() Behavior==

close() unparks only the registered producerWaiter and consumerWaiter. Other waiting threads wake up within 1 microsecond via parkNanos timeout and detect the closed flag on retry. This is intentional and documented.

Null elements are not permitted.

Value parameters

capacity

The logical capacity. Rounded up to the next power of two >= 2 internally.

Attributes

Source
BlockingMpmcQueue.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def close(): Unit

Closes the queue, unblocking any waiting thread on either side.

Closes the queue, unblocking any waiting thread on either side.

Attributes

Source
BlockingMpmcQueue.scala
def isClosed: Boolean

Whether the queue has been closed.

Whether the queue has been closed.

Attributes

Source
BlockingMpmcQueue.scala
def isEmpty: Boolean

Whether the ring buffer contains no elements.

Whether the ring buffer contains no elements.

Attributes

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

Offers an element to the queue, blocking until space is available or the queue is closed. Returns true on success, false if the queue was closed.

Offers an element to the queue, blocking until space is available or the queue is closed. Returns true on success, false if the queue was closed.

Attributes

Source
BlockingMpmcQueue.scala
def take(): A

Takes an element from the queue, blocking until one is available or the queue is closed. Returns the element, or null if closed and empty.

Takes an element from the queue, blocking until one is available or the queue is closed. Returns the element, or null if closed and empty.

Attributes

Source
BlockingMpmcQueue.scala