io.netty.util.internal.shaded.org.jctools.queues.atomic.SpscLinkedAtomicQueue Maven / Gradle / Ivy
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.netty.util.internal.shaded.org.jctools.queues.atomic;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue;
import io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.Supplier;
import io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueueUtil;
import io.netty.util.internal.shaded.org.jctools.queues.QueueProgressIndicators;
import io.netty.util.internal.shaded.org.jctools.queues.IndexedQueueSizeUtil;
import static io.netty.util.internal.shaded.org.jctools.queues.atomic.LinkedAtomicArrayQueueUtil.*;
import java.util.concurrent.atomic.AtomicReferenceArray;
import io.netty.util.internal.shaded.org.jctools.queues.MpmcArrayQueue;
/**
* NOTE: This class was automatically generated by io.netty.util.internal.shaded.org.jctools.queues.atomic.JavaParsingAtomicLinkedQueueGenerator
* which can found in the jctools-build module. The original source file is SpscLinkedQueue.java.
* This is a weakened version of the MPSC algorithm as presented
* on
* 1024 Cores by D. Vyukov. The original has been adapted to Java and it's quirks with regards to memory
* model and layout:
*
* - Use inheritance to ensure no false sharing occurs between producer/consumer node reference fields.
*
- As this is an SPSC we have no need for XCHG, an ordered store is enough.
*
* The queue is initialized with a stub node which is set to both the producer and consumer node references.
* From this point follow the notes on offer/poll.
*
* @param
* @author nitsanw
*/
public class SpscLinkedAtomicQueue extends BaseLinkedAtomicQueue {
public SpscLinkedAtomicQueue() {
LinkedQueueAtomicNode node = newNode();
spProducerNode(node);
spConsumerNode(node);
// this ensures correct construction: StoreStore
node.soNext(null);
}
/**
* {@inheritDoc}
*
* IMPLEMENTATION NOTES:
* Offer is allowed from a SINGLE thread.
* Offer allocates a new node (holding the offered value) and:
*
* - Sets that node as the producerNode.next
*
- Sets the new node as the producerNode
*
* From this follows that producerNode.next is always null and for all other nodes node.next is not null.
*
* @see MessagePassingQueue#offer(Object)
* @see java.util.Queue#offer(java.lang.Object)
*/
@Override
public boolean offer(final E e) {
if (null == e) {
throw new NullPointerException();
}
final LinkedQueueAtomicNode nextNode = newNode(e);
lpProducerNode().soNext(nextNode);
spProducerNode(nextNode);
return true;
}
/**
* {@inheritDoc}
*
* 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 empty.
*
- If it is not null set it as the consumer node and return it's now evacuated value.
*
* This means the consumerNode.value is always null, which is also the starting point for the queue.
* Because null values are not allowed to be offered this is the only node with it's value set to null at
* any one time.
*/
@Override
public E poll() {
return relaxedPoll();
}
@Override
public E peek() {
return relaxedPeek();
}
@Override
public int fill(Supplier s) {
// result is a long because we want to have a safepoint check at regular intervals
long result = 0;
do {
fill(s, 4096);
result += 4096;
} while (result <= Integer.MAX_VALUE - 4096);
return (int) result;
}
@Override
public int fill(Supplier s, int limit) {
if (limit == 0) {
return 0;
}
LinkedQueueAtomicNode tail = newNode(s.get());
final LinkedQueueAtomicNode head = tail;
for (int i = 1; i < limit; i++) {
final LinkedQueueAtomicNode temp = newNode(s.get());
tail.soNext(temp);
tail = temp;
}
final LinkedQueueAtomicNode oldPNode = lpProducerNode();
oldPNode.soNext(head);
spProducerNode(tail);
return limit;
}
@Override
public void fill(Supplier s, WaitStrategy wait, ExitCondition exit) {
LinkedQueueAtomicNode chaserNode = producerNode;
while (exit.keepRunning()) {
for (int i = 0; i < 4096; i++) {
final LinkedQueueAtomicNode nextNode = newNode(s.get());
chaserNode.soNext(nextNode);
chaserNode = nextNode;
this.producerNode = chaserNode;
}
}
}
}