Package rx.internal.util.unsafe
Class SpscArrayQueue<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractQueue<E>
-
- rx.internal.util.unsafe.ConcurrentCircularArrayQueueL0Pad<E>
-
- rx.internal.util.unsafe.ConcurrentCircularArrayQueue<E>
-
- rx.internal.util.unsafe.SpscArrayQueueColdField<E>
-
- rx.internal.util.unsafe.SpscArrayQueueL1Pad<E>
-
- rx.internal.util.unsafe.SpscArrayQueueProducerFields<E>
-
- rx.internal.util.unsafe.SpscArrayQueueL2Pad<E>
-
- rx.internal.util.unsafe.SpscArrayQueueConsumerField<E>
-
- rx.internal.util.unsafe.SpscArrayQueueL3Pad<E>
-
- rx.internal.util.unsafe.SpscArrayQueue<E>
-
- Type Parameters:
E
-
- All Implemented Interfaces:
java.lang.Iterable<E>
,java.util.Collection<E>
,java.util.Queue<E>
,MessagePassingQueue<E>
@SuppressAnimalSniffer public final class SpscArrayQueue<E> extends SpscArrayQueueL3Pad<E>
A Single-Producer-Single-Consumer queue backed by a pre-allocated buffer.This implementation is a mashup of the Fast Flow algorithm with an optimization of the offer method taken from the BQueue algorithm (a variation on Fast Flow), and adjusted to comply with Queue.offer semantics with regards to capacity.
For convenience the relevant papers are available in the resources folder:
2010 - Pisa - SPSC Queues on Shared Cache Multi-Core Systems.pdf
2012 - Junchang- BQueue- Efficient and Practical Queuing.pdf
This implementation is wait free.
-
-
Field Summary
-
Fields inherited from class rx.internal.util.unsafe.SpscArrayQueueL3Pad
p30, p31, p32, p33, p34, p35, p36, p37, p40, p41, p42, p43, p44, p45, p46
-
Fields inherited from class rx.internal.util.unsafe.SpscArrayQueueConsumerField
C_INDEX_OFFSET, consumerIndex
-
Fields inherited from class rx.internal.util.unsafe.SpscArrayQueueL2Pad
p20, p21, p22, p23, p24, p25, p26
-
Fields inherited from class rx.internal.util.unsafe.SpscArrayQueueProducerFields
P_INDEX_OFFSET, producerIndex, producerLookAhead
-
Fields inherited from class rx.internal.util.unsafe.SpscArrayQueueL1Pad
p10, p11, p12, p13, p14, p15, p16
-
Fields inherited from class rx.internal.util.unsafe.SpscArrayQueueColdField
lookAheadStep
-
Fields inherited from class rx.internal.util.unsafe.ConcurrentCircularArrayQueue
buffer, BUFFER_PAD, mask, SPARSE_SHIFT
-
-
Constructor Summary
Constructors Constructor Description SpscArrayQueue(int capacity)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
isEmpty()
This method's accuracy is subject to concurrent modifications happening as the observation is carried out.private long
lvConsumerIndex()
private long
lvProducerIndex()
boolean
offer(E e)
Called from a producer thread subject to the restrictions appropriate to the implementation and according to theQueue.offer(Object)
interface.E
peek()
Called from the consumer thread subject to the restrictions appropriate to the implementation and according to theQueue.peek()
interface.E
poll()
Called from the consumer thread subject to the restrictions appropriate to the implementation and according to theQueue.poll()
interface.int
size()
This method's accuracy is subject to concurrent modifications happening as the size is estimated and as such is a best effort rather than absolute value.private void
soConsumerIndex(long v)
private void
soProducerIndex(long v)
-
Methods inherited from class rx.internal.util.unsafe.ConcurrentCircularArrayQueue
calcElementOffset, calcElementOffset, clear, iterator, lpElement, lpElement, lvElement, lvElement, soElement, soElement, spElement, spElement
-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
-
-
-
Method Detail
-
offer
public boolean offer(E e)
Called from a producer thread subject to the restrictions appropriate to the implementation and according to theQueue.offer(Object)
interface.This implementation is correct for single producer thread use only.
- Returns:
- true if element was inserted into the queue, false iff full
-
poll
public E poll()
Called from the consumer thread subject to the restrictions appropriate to the implementation and according to theQueue.poll()
interface.This implementation is correct for single consumer thread use only.
- Returns:
- a message from the queue if one is available, null iff empty
-
peek
public E peek()
Called from the consumer thread subject to the restrictions appropriate to the implementation and according to theQueue.peek()
interface.This implementation is correct for single consumer thread use only.
- Returns:
- a message from the queue if one is available, null iff empty
-
size
public int size()
Description copied from interface:MessagePassingQueue
This method's accuracy is subject to concurrent modifications happening as the size is estimated and as such is a best effort rather than absolute value. For some implementations this method may be O(n) rather than O(1).- Specified by:
size
in interfacejava.util.Collection<E>
- Specified by:
size
in interfaceMessagePassingQueue<E>
- Specified by:
size
in classjava.util.AbstractCollection<E>
- Returns:
- number of messages in the queue, between 0 and queue capacity or
Integer.MAX_VALUE
if not bounded
-
isEmpty
public boolean isEmpty()
Description copied from interface:MessagePassingQueue
This method's accuracy is subject to concurrent modifications happening as the observation is carried out.- Specified by:
isEmpty
in interfacejava.util.Collection<E>
- Specified by:
isEmpty
in interfaceMessagePassingQueue<E>
- Overrides:
isEmpty
in classjava.util.AbstractCollection<E>
- Returns:
- true if empty, false otherwise
-
soProducerIndex
private void soProducerIndex(long v)
-
soConsumerIndex
private void soConsumerIndex(long v)
-
lvProducerIndex
private long lvProducerIndex()
-
lvConsumerIndex
private long lvConsumerIndex()
-
-