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

public abstract class AbstractNodeQueue<T> extends AtomicReference<AbstractNodeQueue.Node<T>>
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:
  • Constructor Details

    • AbstractNodeQueue

      protected AbstractNodeQueue()
  • Method Details

    • peekNode

      protected final AbstractNodeQueue.Node<T> 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

      public final T 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

      public final void add(T value)
      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

      public final void addNode(AbstractNodeQueue.Node<T> n)
      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

      public final T 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

      public final AbstractNodeQueue.Node<T> 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