Top Description Fields Constructors Methods
rx.internal.util.atomic

public final Class SpscAtomicArrayQueue<E>

extends AtomicReferenceArrayQueue<E>
Class Inheritance
Imports
java.util.concurrent.atomic.*

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

Modifier and TypeField and Description
pack-priv final AtomicLong
pack-priv final int
private static final Integer
pack-priv final AtomicLong
pack-priv long
Inherited from rx.internal.util.atomic.AtomicReferenceArrayQueue:
buffermask

Constructor Summary

AccessConstructor and Description
public
SpscAtomicArrayQueue(int capacity)

Method Summary

Modifier and TypeMethod and Description
public boolean
isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Collection.isEmpty.

Returns true if this collection contains no elements.
private long
private long
public boolean
offer(E
the element to add
e
)

Implements java.util.Queue.offer.

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
public E
peek()

Implements java.util.Queue.peek.

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
public E
poll()

Implements java.util.Queue.poll.

Retrieves and removes the head of this queue, or returns null if this queue is empty.
public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Collection.size.

Returns the number of elements in this collection.
private void
soConsumerIndex(long newIndex)

private void
soProducerIndex(long newIndex)

Inherited from rx.internal.util.atomic.AtomicReferenceArrayQueue:
calcElementOffsetcalcElementOffsetcleariteratorlpElementlpElementlvElementlvElementsoElementsoElementspElementspElementsvElement

Field Detail

consumerIndexback to summary
pack-priv final AtomicLong consumerIndex
lookAheadStepback to summary
pack-priv final int lookAheadStep
MAX_LOOK_AHEAD_STEPback to summary
private static final Integer MAX_LOOK_AHEAD_STEP
producerIndexback to summary
pack-priv final AtomicLong producerIndex
producerLookAheadback to summary
pack-priv long producerLookAhead

Constructor Detail

SpscAtomicArrayQueueback to summary
public SpscAtomicArrayQueue(int capacity)

Method Detail

isEmptyback to summary
public boolean isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Collection.isEmpty.

Doc from java.util.Collection.isEmpty.

Returns true if this collection contains no elements.

Returns:boolean

true if this collection contains no elements

Annotations
@Override
lvConsumerIndexback to summary
private long lvConsumerIndex()
lvProducerIndexback to summary
private long lvProducerIndex()
offerback to summary
public boolean offer(E e)

Implements java.util.Queue.offer.

Doc from java.util.Queue.offer.

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add, which can fail to insert an element only by throwing an exception.

Parameters
e:E

the element to add

Returns:boolean

true if the element was added to this queue, else false

Annotations
@Override
peekback to summary
public E peek()

Implements java.util.Queue.peek.

Doc from java.util.Queue.peek.

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

Returns:E

the head of this queue, or null if this queue is empty

Annotations
@Override
pollback to summary
public E poll()

Implements java.util.Queue.poll.

Doc from java.util.Queue.poll.

Retrieves and removes the head of this queue, or returns null if this queue is empty.

Returns:E

the head of this queue, or null if this queue is empty

Annotations
@Override
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Collection.size.

Doc from java.util.Collection.size.

Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of elements in this collection

Annotations
@Override
soConsumerIndexback to summary
private void soConsumerIndex(long newIndex)
soProducerIndexback to summary
private void soProducerIndex(long newIndex)