Top Description Fields Constructors Methods
io.reactivex.internal.queue

public final Class SpscLinkedArrayQueue<T>

extends Object
implements SimplePlainQueue<T>
Class Inheritance
All Implemented Interfaces
io.reactivex.internal.fuseable.SimplePlainQueue, io.reactivex.internal.fuseable.SimpleQueue
Type Parameters
<T>
the contained value type
Imports
java.util.concurrent.atomic.*, io.reactivex.annotations.Nullable, io.reactivex.internal.fuseable.SimplePlainQueue, io.reactivex.internal.util.Pow2

A single-producer single-consumer array-backed queue which can allocate new arrays in case the consumer is slower than the producer.

Field Summary

Modifier and TypeField and Description
pack-priv AtomicReferenceArray<Object>
pack-priv final AtomicLong
pack-priv final int
private static final Object
pack-priv static final int
pack-priv AtomicReferenceArray<Object>
pack-priv final AtomicLong
pack-priv long
pack-priv int
pack-priv final int

Constructor Summary

AccessConstructor and Description
public
SpscLinkedArrayQueue(final int bufferSize)

Method Summary

Modifier and TypeMethod and Description
private void
adjustLookAheadStep(int capacity)

private static int
calcDirectOffset(int index)

private static int
calcWrappedOffset(long index, int mask)

public void
clear()

Implements io.reactivex.internal.fuseable.SimpleQueue.clear.

Removes all enqueued items from this queue.
public boolean
isEmpty()

Implements io.reactivex.internal.fuseable.SimpleQueue.isEmpty.

Returns true if the queue is empty.
private long
private long
private long
private static <E> Object
lvElement(AtomicReferenceArray<Object> buffer, int offset)

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

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

public boolean
offer(final T
the value to enqueue, not null
e
)

Implements io.reactivex.internal.fuseable.SimpleQueue.offer.

Atomically enqueue a single.
public boolean

Returns:

true if the queue accepted the two new values
offer
(T
the first value, not null
first
,
T
the second value, not null
second
)

Implements io.reactivex.internal.fuseable.SimpleQueue.offer.

Offer two elements at the same time.
public T
peek()

public T
poll()

Implements io.reactivex.internal.fuseable.SimplePlainQueue.poll.

Tries to dequeue a value (non-null) or returns null if the queue is empty.
private void
resize(final AtomicReferenceArray<Object> oldBuffer, final long currIndex, final int offset, final T e, final long mask)

public int
size()

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

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

consumerBufferback to summary
pack-priv AtomicReferenceArray<Object> consumerBuffer
consumerIndexback to summary
pack-priv final AtomicLong consumerIndex
consumerMaskback to summary
pack-priv final int consumerMask
HAS_NEXTback to summary
private static final Object HAS_NEXT
MAX_LOOK_AHEAD_STEPback to summary
pack-priv static final int MAX_LOOK_AHEAD_STEP
producerBufferback to summary
pack-priv AtomicReferenceArray<Object> producerBuffer
producerIndexback to summary
pack-priv final AtomicLong producerIndex
producerLookAheadback to summary
pack-priv long producerLookAhead
producerLookAheadStepback to summary
pack-priv int producerLookAheadStep
producerMaskback to summary
pack-priv final int producerMask

Constructor Detail

SpscLinkedArrayQueueback to summary
public SpscLinkedArrayQueue(final int bufferSize)

Method Detail

adjustLookAheadStepback to summary
private void adjustLookAheadStep(int capacity)
calcDirectOffsetback to summary
private static int calcDirectOffset(int index)
calcWrappedOffsetback to summary
private static int calcWrappedOffset(long index, int mask)
clearback to summary
public void clear()

Implements io.reactivex.internal.fuseable.SimpleQueue.clear.

Doc from io.reactivex.internal.fuseable.SimpleQueue.clear.

Removes all enqueued items from this queue.

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Implements io.reactivex.internal.fuseable.SimpleQueue.isEmpty.

Doc from io.reactivex.internal.fuseable.SimpleQueue.isEmpty.

Returns true if the queue is empty.

Note however that due to potential fused functions in poll() it is possible this method returns false but then poll() returns null because the fused function swallowed the available item(s).

Returns:boolean

true if the queue is empty

Annotations
@Override
lpConsumerIndexback to summary
private long lpConsumerIndex()
lpProducerIndexback to summary
private long lpProducerIndex()
lvConsumerIndexback to summary
private long lvConsumerIndex()
lvElementback to summary
private static <E> Object lvElement(AtomicReferenceArray<Object> buffer, int offset)
lvProducerIndexback to summary
private long lvProducerIndex()
newBufferPeekback to summary
private T newBufferPeek(AtomicReferenceArray<Object> nextBuffer, final long index, final int mask)
Annotations
@SuppressWarnings:unchecked
newBufferPollback to summary
private T newBufferPoll(AtomicReferenceArray<Object> nextBuffer, final long index, final int mask)
Annotations
@SuppressWarnings:unchecked
offerback to summary
public boolean offer(final T e)

Implements io.reactivex.internal.fuseable.SimpleQueue.offer.

Doc from io.reactivex.internal.fuseable.SimpleQueue.offer.

Atomically enqueue a single.

This implementation is correct for single producer thread use only.

Parameters
e:T

the value to enqueue, not null

Returns:boolean

true if successful, false if the value was not enqueued likely due to reaching the queue capacity)

Annotations
@Override
offerback to summary
public boolean offer(T first, T second)

Implements io.reactivex.internal.fuseable.SimpleQueue.offer.

Offer two elements at the same time.

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

Parameters
first:T

the first value, not null

second:T

the second value, not null

Returns:boolean

true if the queue accepted the two new values

Annotations
@Override
peekback to summary
public T peek()
Annotations
@SuppressWarnings:unchecked
pollback to summary
public T poll()

Implements io.reactivex.internal.fuseable.SimplePlainQueue.poll.

Doc from io.reactivex.internal.fuseable.SimpleQueue.poll.

Tries to dequeue a value (non-null) or returns null if the queue is empty.

If the producer uses offer(Object, Object) and when polling in pairs, if the first poll() returns a non-null item, the second poll() is guaranteed to return a non-null item as well.

This implementation is correct for single consumer thread use only.

Returns:T

the item or null to indicate an empty queue

Annotations
@Nullable
@SuppressWarnings:unchecked
@Override
resizeback to summary
private void resize(final AtomicReferenceArray<Object> oldBuffer, final long currIndex, final int offset, final T e, final long mask)
sizeback to summary
public int size()
soConsumerIndexback to summary
private void soConsumerIndex(long v)
soElementback to summary
private static void soElement(AtomicReferenceArray<Object> buffer, int offset, Object e)
soNextback to summary
private void soNext(AtomicReferenceArray<Object> curr, AtomicReferenceArray<Object> next)
soProducerIndexback to summary
private void soProducerIndex(long v)
writeToQueueback to summary
private boolean writeToQueue(final AtomicReferenceArray<Object> buffer, final T e, final long index, final int offset)