Class MpscLinkedQueue<T>
- Type Parameters:
T
- the contained value type
- All Implemented Interfaces:
SimplePlainQueue<T>
,SimpleQueue<T>
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicReference
<MpscLinkedQueue.LinkedQueueNode<T>> private final AtomicReference
<MpscLinkedQueue.LinkedQueueNode<T>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all enqueued items from this queue.boolean
isEmpty()
Returns true if the queue is empty.(package private) MpscLinkedQueue.LinkedQueueNode
<T> (package private) MpscLinkedQueue.LinkedQueueNode
<T> (package private) MpscLinkedQueue.LinkedQueueNode
<T> boolean
Atomically enqueue a single value.boolean
Atomically enqueue two values.poll()
Tries to dequeue a value (non-null) or returns null if the queue is empty.(package private) void
(package private) MpscLinkedQueue.LinkedQueueNode
<T>
-
Field Details
-
producerNode
-
consumerNode
-
-
Constructor Details
-
MpscLinkedQueue
public MpscLinkedQueue()
-
-
Method Details
-
offer
Atomically enqueue a single value.
IMPLEMENTATION NOTES:
Offer is allowed from multiple threads.
Offer allocates a new node and:- Swaps it atomically with current producer node (only one producer 'wins')
- Sets the new node as the node following from the swapped producer node
- Specified by:
offer
in interfaceSimpleQueue<T>
- Parameters:
e
- the value to enqueue, not null- Returns:
- true if successful, false if the value was not enqueued likely due to reaching the queue capacity)
- See Also:
-
poll
Tries to dequeue a value (non-null) or returns null if the queue is empty.If the producer uses
SimpleQueue.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.
IMPLEMENTATION NOTES:
Poll is allowed from a SINGLE thread.
Poll reads the next node from the consumerNode and:- If it is null, the queue is assumed empty (though it might not be).
- If it is not null set it as the consumer node and return it's now evacuated value.
- Specified by:
poll
in interfaceSimplePlainQueue<T>
- Specified by:
poll
in interfaceSimpleQueue<T>
- Returns:
- the item or null to indicate an empty queue
- See Also:
-
offer
Description copied from interface:SimpleQueue
Atomically enqueue two values.- Specified by:
offer
in interfaceSimpleQueue<T>
- Parameters:
v1
- the first value to enqueue, not nullv2
- the second value to enqueue, not null- Returns:
- true if successful, false if the value was not enqueued likely due to reaching the queue capacity)
-
clear
public void clear()Description copied from interface:SimpleQueue
Removes all enqueued items from this queue.- Specified by:
clear
in interfaceSimpleQueue<T>
-
lvProducerNode
MpscLinkedQueue.LinkedQueueNode<T> lvProducerNode() -
xchgProducerNode
-
lvConsumerNode
MpscLinkedQueue.LinkedQueueNode<T> lvConsumerNode() -
lpConsumerNode
MpscLinkedQueue.LinkedQueueNode<T> lpConsumerNode() -
spConsumerNode
-
isEmpty
public boolean isEmpty()Returns true if the queue is empty.Note however that due to potential fused functions in
SimpleQueue.poll()
it is possible this method returns false but then poll() returns null because the fused function swallowed the available item(s).
IMPLEMENTATION NOTES:
Queue is empty when producerNode is the same as consumerNode. An alternative implementation would be to observe the producerNode.value is null, which also means an empty queue because only the consumerNode.value is allowed to be null.- Specified by:
isEmpty
in interfaceSimpleQueue<T>
- Returns:
- true if the queue is empty
-