Top Description Interfaces Classes
module io.netty.common

Package io.netty.util.internal.shaded.org.jctools.queues


This package aims to fill a gap in current JDK implementations in offering lock free (wait free where possible) queues for inter-thread message passing with finer grained guarantees and an emphasis on performance.
At the time of writing the only lock free queue available in the JDK is java.util.concurrent.ConcurrentLinkedQueue which is an unbounded multi-producer, multi-consumer queue which is further encumbered by the need to implement the full range of java.util.Queue methods. In this package we offer a range of implementations:
  1. Bounded/Unbounded SPSC queues - Serving the Single Producer Single Consumer use case.
  2. Bounded/Unbounded MPSC queues - The Multi Producer Single Consumer case also has a multi-lane implementation on offer which trades the FIFO ordering(re-ordering is not limited) for reduced contention and increased throughput under contention.
  3. Bounded SPMC/MPMC queues


Limited Queue methods support:
The queues implement a subset of the java.util.Queue interface which is documented under the io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue interface. In particular java.util.Queue#iterator() is usually not supported and dependent methods from java.util.AbstractQueue are also not supported such as:

  1. java.util.Queue#remove(Object)
  2. java.util.Queue#removeAll(java.util.Collection)
  3. java.util.Queue#removeIf(java.util.function.Predicate)
  4. java.util.Queue#contains(Object)
  5. java.util.Queue#containsAll(java.util.Collection)
A few queues do support a limited form of iteration. This support is documented in the Javadoc of the relevant queues.


Memory layout controls and False Sharing:
The classes in this package use what is considered at the moment the most reliable method of controlling class field layout, namely inheritance. The method is described in this post which also covers why other methods are currently suspect.
Note that we attempt to tackle both active (write/write) and passive(read/write) false sharing case:

  1. Hot counters (or write locations) are padded.
  2. Read-Only shared fields are padded.
  3. Array edges are NOT padded (though doing so is entirely legitimate).


Use of sun.misc.Unsafe:
A choice is made in this library to utilize sun.misc.Unsafe for performance reasons. In this package we have two use cases:

  1. The queue counters in the queues are all inlined (i.e. are primitive fields of the queue classes). To allow lazySet/CAS semantics to these fields we could use java.util.concurrent.atomic.AtomicLongFieldUpdater but choose not to for performance reasons. On newer OpenJDKs where AFU is made more performant the difference is small.
  2. We use Unsafe to gain volatile/lazySet access to array elements. We could use java.util.concurrent.atomic.AtomicReferenceArray but choose not to for performance reasons(extra reference chase and redundant boundary checks).
Both use cases should be made obsolete by VarHandles at some point.


Avoiding redundant loads of fields:
Because a volatile load will force any following field access to reload the field value an effort is made to cache field values in local variables where possible and expose interfaces which allow the code to capitalize on such caching. As a convention the local variable name will be the field name and will be final.


Method naming conventions:
The following convention is followed in method naming to highlight volatile/ordered/plain access to fields:

  1. lpFoo/spFoo: these will be plain load or stores to the field. No memory ordering is needed or expected.
  2. soFoo: this is an ordered stored to the field (like java.util.concurrent.atomic.AtomicInteger#lazySet(int)). Implies an ordering of stores (StoreStore barrier before the store).
  3. lv/svFoo: these are volatile load/store. A store implies a StoreLoad barrier, a load implies LoadLoad barrier before and LoadStore after.
  4. casFoo: compare and swap the field. StoreLoad if successful. See java.util.concurrent.atomic.AtomicInteger#compareAndSet(int, int)
  5. xchgFoo: atomically get and set the field. Effectively a StoreLoad. See java.util.concurrent.atomic.AtomicInteger#getAndSet(int)
  6. xaddFoo: atomically get and add to the field. Effectively a StoreLoad. See java.util.concurrent.atomic.AtomicInteger#getAndAdd(int)
It is generally expected that a volatile load signals the acquire of a field previously released by a non-plain store.
Author
nitsanw

Interface Summary

Modifier and TypeInterface and Description
public interface
MessagePassingQueue<
the event/message type
T
>

Message passing queues are intended for concurrent method passing.

public interface
QueueProgressIndicators

This interface is provided for monitoring purposes only and is only available on queues where it is easy to provide it.

public interface
SupportsIterator

Tagging interface to help testing

Class Summary

Modifier and TypeClass and Description
pack-priv abstract class
BaseLinkedQueue<E>

A base data structure for concurrent linked queues.

pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
BaseMpscLinkedArrayQueue<E>

An MPSC array queue which starts at initialCapacity and grows to maxCapacity in linked chunks of the initial size.

pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
ConcurrentCircularArrayQueue<E>

Common functionality for array backed queues.

pack-priv abstract class
pack-priv abstract class
public class
IndexedQueueSizeUtil

A note to maintainers on index assumptions: in a single threaded world it would seem intuitive to assume:

producerIndex >= consumerIndex
pack-priv class
LinkedArrayQueueUtil

This is used for method substitution in the LinkedArray classes code generation.

pack-priv class
public class
public class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
public class
MpmcUnboundedXaddArrayQueue<E>

An MPMC array queue which grows unbounded in linked chunks.

pack-priv class
public class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
public class
MpscBlockingConsumerArrayQueue<E>

This is a partial implementation of the java.util.concurrent.BlockingQueue on the consumer side only on top of the mechanics described in BaseMpscLinkedArrayQueue, but with the reservation bit used for blocking rather than resizing in this instance.

pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
public class
MpscChunkedArrayQueue<E>

An MPSC array queue which starts at initialCapacity and grows to maxCapacity in linked chunks of the initial size.

pack-priv abstract class
public class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
MpscCompoundQueueL0Pad<E>

Use a set number of parallel MPSC queues to diffuse the contention on tail.

pack-priv abstract class
public class
MpscGrowableArrayQueue<E>

An MPSC array queue which starts at initialCapacity and grows to maxCapacity in linked chunks, doubling theirs size every time until the full blown backing array is used.

public class
MpscLinkedQueue<E>

This is a Java port of the MPSC algorithm as presented on 1024 Cores by D.

public class
MpscUnboundedArrayQueue<E>

An MPSC array queue which starts at initialCapacity and grows indefinitely in linked chunks of the initial size.

public class
MpscUnboundedXaddArrayQueue<E>

An MPSC array queue which grows unbounded in linked chunks.

pack-priv class
pack-priv abstract class
MpUnboundedXaddArrayQueue<R extends MpUnboundedXaddChunk<R, E>, E>

Common infrastructure for the XADD queues.

pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv class
public class
QueueFactory

Deprecated
The queue factory produces java.util.Queue instances based on a best fit to the ConcurrentQueueSpec.
public class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
public class
SpscArrayQueue<E>

A Single-Producer-Single-Consumer queue backed by a pre-allocated buffer.

pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
pack-priv abstract class
public class
SpscChunkedArrayQueue<E>

An SPSC array queue which starts at initialCapacity and grows to maxCapacity in linked chunks of the initial size.

public class
SpscGrowableArrayQueue<E>

An SPSC array queue which starts at initialCapacity and grows to maxCapacity in linked chunks, doubling theirs size every time until the full blown backing array is used.

public class
SpscLinkedQueue<E>

This is a weakened version of the MPSC algorithm as presented on 1024 Cores by D.

public class
SpscUnboundedArrayQueue<E>

An SPSC array queue which starts at initialCapacity and grows indefinitely in linked chunks of the initial size.