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:
Iterable<E>,Collection<E>,Queue<E>,MessagePassingQueue<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, p46Fields inherited from class rx.internal.util.unsafe.SpscArrayQueueConsumerField
C_INDEX_OFFSET, consumerIndexFields inherited from class rx.internal.util.unsafe.SpscArrayQueueL2Pad
p20, p21, p22, p23, p24, p25, p26Fields inherited from class rx.internal.util.unsafe.SpscArrayQueueProducerFields
P_INDEX_OFFSET, producerIndex, producerLookAheadFields inherited from class rx.internal.util.unsafe.SpscArrayQueueL1Pad
p10, p11, p12, p13, p14, p15, p16Fields inherited from class rx.internal.util.unsafe.SpscArrayQueueColdField
lookAheadStepFields inherited from class rx.internal.util.unsafe.ConcurrentCircularArrayQueue
buffer, BUFFER_PAD, mask, SPARSE_SHIFT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanisEmpty()This method's accuracy is subject to concurrent modifications happening as the observation is carried out.private longprivate longbooleanCalled from a producer thread subject to the restrictions appropriate to the implementation and according to theQueue.offer(Object)interface.peek()Called from the consumer thread subject to the restrictions appropriate to the implementation and according to theQueue.peek()interface.poll()Called from the consumer thread subject to the restrictions appropriate to the implementation and according to theQueue.poll()interface.intsize()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 voidsoConsumerIndex(long v) private voidsoProducerIndex(long v) Methods inherited from class rx.internal.util.unsafe.ConcurrentCircularArrayQueue
calcElementOffset, calcElementOffset, clear, iterator, lpElement, lpElement, lvElement, lvElement, soElement, soElement, spElement, spElementMethods inherited from class java.util.AbstractQueue
add, addAll, element, removeMethods inherited from class java.util.AbstractCollection
contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
contains, containsAll, equals, hashCode, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Constructor Details
-
SpscArrayQueue
public SpscArrayQueue(int capacity)
-
-
Method Details
-
offer
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.
- Parameters:
e-- Returns:
- true if element was inserted into the queue, false iff full
-
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
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:MessagePassingQueueThis 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:
sizein interfaceCollection<E>- Specified by:
sizein interfaceMessagePassingQueue<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- number of messages in the queue, between 0 and queue capacity or
Integer.MAX_VALUEif not bounded
-
isEmpty
public boolean isEmpty()Description copied from interface:MessagePassingQueueThis method's accuracy is subject to concurrent modifications happening as the observation is carried out.- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceMessagePassingQueue<E>- Overrides:
isEmptyin classAbstractCollection<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()
-