Package org.apache.pekko.dispatch
Class AbstractNodeQueue<T>
java.lang.Object
java.util.concurrent.atomic.AtomicReference<AbstractNodeQueue.Node<T>>
org.apache.pekko.dispatch.AbstractNodeQueue<T>
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
NodeMessageQueue
Lock-free MPSC linked queue implementation based on Dmitriy Vyukov's non-intrusive MPSC queue:
https://www.1024cores.net/home/lock-free-algorithms/queues/non-intrusive-mpsc-node-based-queue
This queue could be wait-free (i.e. without the spinning loops in peekNode and pollNode) if
it were permitted to return null while the queue is not quite empty anymore but the enqueued
element is not yet visible. This would break actor scheduling, though.
- See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidAdd an element to the head of the queue.final voidAdd an element to the head of the queue, providing the queue node to be used.final intcount()This method returns an upper bound on the queue size at the time it starts executing.final booleanisEmpty()Query the queue whether it is empty right now.final Tpeek()Query the queue tail for the next element without dequeuing it.protected final AbstractNodeQueue.Node<T>peekNode()Query the queue tail for the next element without dequeuing it.final Tpoll()Pull one item from the queue’s tail if there is one.final AbstractNodeQueue.Node<T>pollNode()Pull one item from the queue, returning it within a queue node.Methods inherited from class java.util.concurrent.atomic.AtomicReference
accumulateAndGet, compareAndExchange, compareAndExchangeAcquire, compareAndExchangeRelease, compareAndSet, get, getAcquire, getAndAccumulate, getAndSet, getAndUpdate, getOpaque, getPlain, lazySet, set, setOpaque, setPlain, setRelease, toString, updateAndGet, weakCompareAndSet, weakCompareAndSetAcquire, weakCompareAndSetPlain, weakCompareAndSetRelease, weakCompareAndSetVolatile
-
Constructor Details
-
AbstractNodeQueue
protected AbstractNodeQueue()
-
-
Method Details
-
peekNode
Query the queue tail for the next element without dequeuing it. Use this method only from the consumer thread! !!! There is a copy of this code in pollNode() !!!- Returns:
- queue node with element inside if there was one, or null if there was none
-
peek
Query the queue tail for the next element without dequeuing it. Use this method only from the consumer thread!- Returns:
- element if there was one, or null if there was none
-
add
Add an element to the head of the queue. This method can be used from any thread.- Parameters:
value- the element to be added; must not be null
-
addNode
Add an element to the head of the queue, providing the queue node to be used. This method can be used from any thread.- Parameters:
n- the node containing the element to be added; both must not be null
-
isEmpty
public final boolean isEmpty()Query the queue whether it is empty right now. This method can be used from any thread.- Returns:
- true if queue was empty at some point in the past
-
count
public final int count()This method returns an upper bound on the queue size at the time it starts executing. It may spuriously return smaller values (including zero) if the consumer pulls items out concurrently. This method can be used from any thread.- Returns:
- an upper bound on queue length at some time in the past
-
poll
Pull one item from the queue’s tail if there is one. Use this method only from the consumer thread!- Returns:
- element if there was one, or null if there was none
-
pollNode
Pull one item from the queue, returning it within a queue node. Use this method only from the consumer thread!- Returns:
- queue node with element inside if there was one, or null if there was none
-