Class SpscLinkedArrayQueue<T>

java.lang.Object
rx.internal.util.atomic.SpscLinkedArrayQueue<T>
Type Parameters:
T - the element type, not null
All Implemented Interfaces:
Iterable<T>, Collection<T>, Queue<T>

public final class SpscLinkedArrayQueue<T> extends Object implements Queue<T>
A single-producer single-consumer array-backed queue which can allocate new arrays in case the consumer is slower than the producer.
  • Field Details

    • MAX_LOOK_AHEAD_STEP

      static final int MAX_LOOK_AHEAD_STEP
    • producerIndex

      final AtomicLong producerIndex
    • producerLookAheadStep

      int producerLookAheadStep
    • producerLookAhead

      long producerLookAhead
    • producerMask

      int producerMask
    • producerBuffer

      AtomicReferenceArray<Object> producerBuffer
    • consumerMask

      int consumerMask
    • consumerBuffer

      AtomicReferenceArray<Object> consumerBuffer
    • consumerIndex

      final AtomicLong consumerIndex
    • HAS_NEXT

      private static final Object HAS_NEXT
  • Constructor Details

    • SpscLinkedArrayQueue

      public SpscLinkedArrayQueue(int bufferSize)
  • Method Details

    • offer

      public boolean offer(T e)

      This implementation is correct for single producer thread use only.

      Specified by:
      offer in interface Queue<T>
    • writeToQueue

      private boolean writeToQueue(AtomicReferenceArray<Object> buffer, T e, long index, int offset)
    • resize

      private void resize(AtomicReferenceArray<Object> oldBuffer, long currIndex, int offset, T e, long mask)
    • soNext

      private void soNext(AtomicReferenceArray<Object> curr, AtomicReferenceArray<Object> next)
    • lvNext

    • poll

      public T poll()

      This implementation is correct for single consumer thread use only.

      Specified by:
      poll in interface Queue<T>
    • newBufferPoll

      private T newBufferPoll(AtomicReferenceArray<Object> nextBuffer, long index, int mask)
    • peek

      public T peek()

      This implementation is correct for single consumer thread use only.

      Specified by:
      peek in interface Queue<T>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<T>
    • newBufferPeek

      private T newBufferPeek(AtomicReferenceArray<Object> nextBuffer, long index, int mask)
    • size

      public int size()
      Specified by:
      size in interface Collection<T>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<T>
    • adjustLookAheadStep

      private void adjustLookAheadStep(int capacity)
    • lvProducerIndex

      private long lvProducerIndex()
    • lvConsumerIndex

      private long lvConsumerIndex()
    • lpProducerIndex

      private long lpProducerIndex()
    • lpConsumerIndex

      private long lpConsumerIndex()
    • soProducerIndex

      private void soProducerIndex(long v)
    • soConsumerIndex

      private void soConsumerIndex(long v)
    • calcWrappedOffset

      private static int calcWrappedOffset(long index, int mask)
    • calcDirectOffset

      private static int calcDirectOffset(int index)
    • soElement

      private static void soElement(AtomicReferenceArray<Object> buffer, int offset, Object e)
    • lvElement

      private static <E> Object lvElement(AtomicReferenceArray<Object> buffer, int offset)
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<T>
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<T>
    • toArray

      public <E> E[] toArray(E[] a)
      Specified by:
      toArray in interface Collection<T>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<T>
    • containsAll

      public boolean containsAll(Collection<?> c)
      Specified by:
      containsAll in interface Collection<T>
    • addAll

      public boolean addAll(Collection<? extends T> c)
      Specified by:
      addAll in interface Collection<T>
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<T>
    • retainAll

      public boolean retainAll(Collection<?> c)
      Specified by:
      retainAll in interface Collection<T>
    • add

      public boolean add(T e)
      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface Queue<T>
    • remove

      public T remove()
      Specified by:
      remove in interface Queue<T>
    • element

      public T element()
      Specified by:
      element in interface Queue<T>
    • offer

      public boolean offer(T first, T second)
      Offer two elements at the same time.

      Don't use the regular offer() with this at all!

      Parameters:
      first -
      second -
      Returns:
      always true