Package rx.internal.util.atomic
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>
A single-producer single-consumer array-backed queue which can allocate new arrays in case the consumer is slower
than the producer.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) AtomicReferenceArray
<Object> (package private) final AtomicLong
(package private) int
private 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 TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends T> c) private void
adjustLookAheadStep
(int capacity) private static int
calcDirectOffset
(int index) private static int
calcWrappedOffset
(long index, int mask) void
clear()
boolean
boolean
containsAll
(Collection<?> c) element()
boolean
isEmpty()
iterator()
private long
private long
private long
private static <E> Object
lvElement
(AtomicReferenceArray<Object> buffer, int offset) private AtomicReferenceArray
<Object> lvNext
(AtomicReferenceArray<Object> curr) private long
private T
newBufferPeek
(AtomicReferenceArray<Object> nextBuffer, long index, int mask) private T
newBufferPoll
(AtomicReferenceArray<Object> nextBuffer, long index, int mask) boolean
boolean
Offer two elements at the same time.peek()
poll()
remove()
boolean
boolean
removeAll
(Collection<?> c) private void
resize
(AtomicReferenceArray<Object> oldBuffer, long currIndex, int offset, T e, long mask) boolean
retainAll
(Collection<?> c) int
size()
private void
soConsumerIndex
(long v) private static void
soElement
(AtomicReferenceArray<Object> buffer, int offset, Object e) private void
soNext
(AtomicReferenceArray<Object> curr, AtomicReferenceArray<Object> next) private void
soProducerIndex
(long v) Object[]
toArray()
<E> E[]
toArray
(E[] a) private boolean
writeToQueue
(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, wait
Methods 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
-
SpscLinkedArrayQueue
public SpscLinkedArrayQueue(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:
clear
in interfaceCollection<T>
-
newBufferPeek
-
size
public int size()- Specified by:
size
in interfaceCollection<T>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in 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:
contains
in interfaceCollection<T>
-
toArray
- Specified by:
toArray
in interfaceCollection<T>
-
toArray
public <E> E[] toArray(E[] a) - Specified by:
toArray
in interfaceCollection<T>
-
remove
- Specified by:
remove
in interfaceCollection<T>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<T>
-
addAll
- Specified by:
addAll
in interfaceCollection<T>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<T>
-
retainAll
- Specified by:
retainAll
in interfaceCollection<T>
-
add
-
remove
-
element
-
offer
Offer two elements at the same time.Don't use the regular offer() with this at all!
- Parameters:
first
-second
-- Returns:
- always true
-