Top Description Fields Constructors Methods
io.netty.util.internal.shaded.org.jctools.queues

public Class MpscArrayQueue<E>

Additional top-level classes in compilation unit: MpscArrayQueueL1Pad, MpscArrayQueueProducerIndexField, MpscArrayQueueMidPad, MpscArrayQueueProducerLimitField, MpscArrayQueueL2Pad, MpscArrayQueueConsumerIndexField, MpscArrayQueueL3Pad.

extends MpscArrayQueueL3Pad<E>
Class Inheritance
Static Imports
io.netty.util.internal.shaded.org.jctools.util.UnsafeAccess.UNSAFE, .UnsafeAccess.fieldOffset, io.netty.util.internal.shaded.org.jctools.util.UnsafeRefArrayAccess.*

A Multi-Producer-Single-Consumer queue based on a io.netty.util.internal.shaded.org.jctools.queues.ConcurrentCircularArrayQueue. This implies that any thread may call the offer method, but only a single thread may call poll/peek for correctness to maintained.
This implementation follows patterns documented on the package level for False Sharing protection.
This implementation is using the Fast Flow method for polling from the queue (with minor change to correctly publish the index) and an extension of the Leslie Lamport concurrent queue algorithm (originated by Martin Thompson) on the producer side.

Field Summary

Inherited from io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueueL3Pad:
b000b001b002b003b004b005b006b007b010b011b012b013b014b015b016b017b020b021b022b023b024b025b026b027b030b031b032b033b034b035b036b037b040b041b042b043b044b045b046b047b050b051b052b053b054b055b056b057b060b061b062b063b064b065b066b067b070b071b072b073b074b075b076b077b100b101b102b103b104b105b106b107b110b111b112b113b114b115b116b117b120b121b122b123b124b125b126b127b130b131b132b133b134b135b136b137b140b141b142b143b144b145b146b147b150b151b152b153b154b155b156b157b160b161b162b163b164b165b166b167b170b171b172b173b174b175b176b177

Constructor Summary

AccessConstructor and Description
public
MpscArrayQueue(final int capacity)

Method Summary

Modifier and TypeMethod and Description
public int
drain(final MessagePassingQueue.Consumer<E> c, final int limit)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Remove up to limit elements from the queue and hand to consume.
public int
drain(MessagePassingQueue.Consumer<E> c)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Remove all available item from the queue and hand to consume.
public void
public final int

Returns:

1 if next element cannot be filled, -1 if CAS failed, 0 if successful
failFastOffer
(final E
new element, not null
e
)

A wait free alternative to offer which fails on CAS failure.

public int
fill(MessagePassingQueue.Supplier<E> s, int limit)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Stuff the queue with up to limit elements from the supplier.
public int
fill(MessagePassingQueue.Supplier<E> s)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Stuff the queue with elements from the supplier.
public void
public boolean
offer(final E
not null, will throw NPE if it is
e
)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.offer, java.util.Queue.offer.

Called from a producer thread subject to the restrictions appropriate to the implementation and according to the Queue#offer(Object) interface.
public boolean

Returns:

true if the offer is successful, false if queue size exceeds threshold
offerIfBelowThreshold
(final E
the object to offer onto the queue, not null
e
,
int
the maximum allowable size
threshold
)

offer} if size() is less than threshold.

public E
peek()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.peek, java.util.Queue.peek.

Called from the consumer thread subject to the restrictions appropriate to the implementation and according to the Queue#peek() interface.
public E
poll()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.poll, java.util.Queue.poll.

Called from the consumer thread subject to the restrictions appropriate to the implementation and according to the Queue#poll() interface.
public boolean
relaxedOffer(E
not null, will throw NPE if it is
e
)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedOffer.

Called from a producer thread subject to the restrictions appropriate to the implementation.
public E
relaxedPeek()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedPeek.

Called from the consumer thread subject to the restrictions appropriate to the implementation.
public E
relaxedPoll()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedPoll.

Called from the consumer thread subject to the restrictions appropriate to the implementation.

Constructor Detail

MpscArrayQueueback to summary
public MpscArrayQueue(final int capacity)

Method Detail

drainback to summary
public int drain(final MessagePassingQueue.Consumer<E> c, final int limit)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Remove up to limit elements from the queue and hand to consume. This should be semantically similar to:

M m;
  int i = 0;
  for(;i < limit && (m = relaxedPoll()) != null; i++){
    c.accept(m);
  }
  return i;

There's no strong commitment to the queue being empty at the end of a drain. Called from a consumer thread subject to the restrictions appropriate to the implementation.

Warning

Explicit assumptions are made with regards to Consumer#accept make sure you have read and understood these before using this method.

Returns:int

the number of polled elements

Annotations
@Override
drainback to summary
public int drain(MessagePassingQueue.Consumer<E> c)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Remove all available item from the queue and hand to consume. This should be semantically similar to:

M m;
while((m = relaxedPoll()) != null){
c.accept(m);
}
There's no strong commitment to the queue being empty at the end of a drain. Called from a consumer thread subject to the restrictions appropriate to the implementation.

Warning

Explicit assumptions are made with regards to Consumer#accept make sure you have read and understood these before using this method.

Returns:int

the number of polled elements

Annotations
@Override
drainback to summary
public void drain(MessagePassingQueue.Consumer<E> c, MessagePassingQueue.WaitStrategy w, MessagePassingQueue.ExitCondition exit)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Remove elements from the queue and hand to consume forever. Semantically similar to:

 int idleCounter = 0;
 while (exit.keepRunning()) {
     E e = relaxedPoll();
     if(e==null){
         idleCounter = wait.idle(idleCounter);
         continue;
     }
     idleCounter = 0;
     c.accept(e);
 }

Called from a consumer thread subject to the restrictions appropriate to the implementation.

Warning

Explicit assumptions are made with regards to Consumer#accept make sure you have read and understood these before using this method.

Annotations
@Override
failFastOfferback to summary
public final int failFastOffer(final E e)

A wait free alternative to offer which fails on CAS failure.

Parameters
e:E

new element, not null

Returns:int

1 if next element cannot be filled, -1 if CAS failed, 0 if successful

fillback to summary
public int fill(MessagePassingQueue.Supplier<E> s, int limit)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Stuff the queue with up to limit elements from the supplier. Semantically similar to:

for(int i=0; i < limit && relaxedOffer(s.get()); i++);

There's no strong commitment to the queue being full at the end of a fill. Called from a producer thread subject to the restrictions appropriate to the implementation

Warning

Explicit assumptions are made with regards to Supplier#get make sure you have read and understood these before using this method.

Returns:int

the number of offered elements

Annotations
@Override
fillback to summary
public int fill(MessagePassingQueue.Supplier<E> s)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Stuff the queue with elements from the supplier. Semantically similar to:

while(relaxedOffer(s.get());
There's no strong commitment to the queue being full at the end of a fill. Called from a producer thread subject to the restrictions appropriate to the implementation.

Unbounded queues will fill up the queue with a fixed amount rather than fill up to oblivion

Warning

Explicit assumptions are made with regards to Supplier#get make sure you have read and understood these before using this method.

Returns:int

the number of offered elements

Annotations
@Override
fillback to summary
public void fill(MessagePassingQueue.Supplier<E> s, MessagePassingQueue.WaitStrategy wait, MessagePassingQueue.ExitCondition exit)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Stuff the queue with elements from the supplier forever. Semantically similar to:


 int idleCounter = 0;
 while (exit.keepRunning()) {
     E e = s.get();
     while (!relaxedOffer(e)) {
         idleCounter = wait.idle(idleCounter);
         continue;
     }
     idleCounter = 0;
 }

Called from a producer thread subject to the restrictions appropriate to the implementation. The main difference being that implementors MUST assure room in the queue is available BEFORE calling Supplier#get

Warning

Explicit assumptions are made with regards to Supplier#get make sure you have read and understood these before using this method.

Annotations
@Override
offerback to summary
public boolean offer(final E e)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.offer, java.util.Queue.offer.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.offer.

Called from a producer thread subject to the restrictions appropriate to the implementation and according to the Queue#offer(Object) interface.

Implementation Notes


Lock free offer using a single CAS. As class name suggests access is permitted to many threads concurrently.

Parameters
e:E

not null, will throw NPE if it is

Returns:boolean

true if element was inserted into the queue, false iff full

Annotations
@Override
See Also
java.util.Queue#offer, io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue#offer
offerIfBelowThresholdback to summary
public boolean offerIfBelowThreshold(final E e, int threshold)

offer} if size() is less than threshold.

Parameters
e:E

the object to offer onto the queue, not null

threshold:int

the maximum allowable size

Returns:boolean

true if the offer is successful, false if queue size exceeds threshold

Since
1.0.1
peekback to summary
public E peek()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.peek, java.util.Queue.peek.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.peek.

Called from the consumer thread subject to the restrictions appropriate to the implementation and according to the Queue#peek() interface.

Implementation Notes


Lock free peek using ordered loads. As class name suggests access is limited to a single thread.

Returns:E

a message from the queue if one is available, null iff empty

Annotations
@Override
See Also
java.util.Queue#poll, io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue#poll
pollback to summary
public E poll()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.poll, java.util.Queue.poll.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.poll.

Called from the consumer thread subject to the restrictions appropriate to the implementation and according to the Queue#poll() interface.

Implementation Notes


Lock free poll using ordered loads/stores. As class name suggests access is limited to a single thread.

Returns:E

a message from the queue if one is available, null iff empty

Annotations
@Override
See Also
java.util.Queue#poll, io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue#poll
relaxedOfferback to summary
public boolean relaxedOffer(E e)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedOffer.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedOffer.

Called from a producer thread subject to the restrictions appropriate to the implementation. As opposed to Queue#offer(Object) this method may return false without the queue being full.

Parameters
e:E

not null, will throw NPE if it is

Returns:boolean

true if element was inserted into the queue, false if unable to offer

Annotations
@Override
relaxedPeekback to summary
public E relaxedPeek()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedPeek.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedPeek.

Called from the consumer thread subject to the restrictions appropriate to the implementation. As opposed to Queue#peek() this method may return null without the queue being empty.

Returns:E

a message from the queue if one is available, null if unable to peek

Annotations
@Override
relaxedPollback to summary
public E relaxedPoll()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedPoll.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedPoll.

Called from the consumer thread subject to the restrictions appropriate to the implementation. As opposed to Queue#poll() this method may return null without the queue being empty.

Returns:E

a message from the queue if one is available, null if unable to poll

Annotations
@Override