Package rx.internal.util.atomic
Class SpscUnboundedAtomicArrayQueue<T>
java.lang.Object
rx.internal.util.atomic.SpscUnboundedAtomicArrayQueue<T>
- Type Parameters:
T- the value type held by this queue
- All Implemented Interfaces:
Iterable<T>,Collection<T>,Queue<T>
A single-producer single-consumer queue with unbounded capacity.
The implementation uses fixed, power-of-2 arrays to store elements and turns into a linked-list like structure if the production overshoots the consumption.
Note that the minimum capacity of the 'islands' are 8 due to how the look-ahead optimization works.
The implementation uses field updaters and thus should be platform-safe.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) AtomicReferenceArray<Object> (package private) final AtomicLong(package private) intprivate static final Object(package private) static final int(package private) AtomicReferenceArray<Object> (package private) final AtomicLong(package private) long(package private) int(package private) int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends T> c) private voidadjustLookAheadStep(int capacity) private static intcalcDirectOffset(int index) private static intcalcWrappedOffset(long index, int mask) voidclear()booleanbooleancontainsAll(Collection<?> c) element()booleanisEmpty()iterator()private longprivate longprivate longprivate static <E> ObjectlvElement(AtomicReferenceArray<Object> buffer, int offset) private AtomicReferenceArray<Object> lvNext(AtomicReferenceArray<Object> curr) private longprivate TnewBufferPeek(AtomicReferenceArray<Object> nextBuffer, long index, int mask) private TnewBufferPoll(AtomicReferenceArray<Object> nextBuffer, long index, int mask) booleanpeek()poll()remove()booleanbooleanremoveAll(Collection<?> c) private voidresize(AtomicReferenceArray<Object> oldBuffer, long currIndex, int offset, T e, long mask) booleanretainAll(Collection<?> c) intsize()private voidsoConsumerIndex(long v) private static voidsoElement(AtomicReferenceArray<Object> buffer, int offset, Object e) private voidsoNext(AtomicReferenceArray<Object> curr, AtomicReferenceArray<Object> next) private voidsoProducerIndex(long v) Object[]toArray()<E> E[]toArray(E[] a) private booleanwriteToQueue(AtomicReferenceArray<Object> buffer, T e, long index, int offset) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
MAX_LOOK_AHEAD_STEP
static final int MAX_LOOK_AHEAD_STEP -
producerIndex
-
producerLookAheadStep
int producerLookAheadStep -
producerLookAhead
long producerLookAhead -
producerMask
int producerMask -
producerBuffer
AtomicReferenceArray<Object> producerBuffer -
consumerMask
int consumerMask -
consumerBuffer
AtomicReferenceArray<Object> consumerBuffer -
consumerIndex
-
HAS_NEXT
-
-
Constructor Details
-
SpscUnboundedAtomicArrayQueue
public SpscUnboundedAtomicArrayQueue(int bufferSize)
-
-
Method Details
-
offer
This implementation is correct for single producer thread use only.
-
writeToQueue
-
resize
private void resize(AtomicReferenceArray<Object> oldBuffer, long currIndex, int offset, T e, long mask) -
soNext
-
lvNext
-
poll
This implementation is correct for single consumer thread use only.
-
newBufferPoll
-
peek
This implementation is correct for single consumer thread use only.
-
clear
public void clear()- Specified by:
clearin interfaceCollection<T>
-
newBufferPeek
-
size
public int size()- Specified by:
sizein interfaceCollection<T>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<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
-
lvElement
-
iterator
-
contains
- Specified by:
containsin interfaceCollection<T>
-
toArray
- Specified by:
toArrayin interfaceCollection<T>
-
toArray
public <E> E[] toArray(E[] a) - Specified by:
toArrayin interfaceCollection<T>
-
remove
- Specified by:
removein interfaceCollection<T>
-
containsAll
- Specified by:
containsAllin interfaceCollection<T>
-
addAll
- Specified by:
addAllin interfaceCollection<T>
-
removeAll
- Specified by:
removeAllin interfaceCollection<T>
-
retainAll
- Specified by:
retainAllin interfaceCollection<T>
-
add
-
remove
-
element
-