io.netty.util.internal.shaded.org.jctools.queues.atomic.SpscAtomicArrayQueue 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.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicLongArray;
/**
* NOTE: This class was automatically generated by io.netty.util.internal.shaded.org.jctools.queues.atomic.JavaParsingAtomicArrayQueueGenerator
* which can found in the jctools-build module. The original source file is SpscArrayQueue.java.
*/
abstract class SpscAtomicArrayQueueColdField extends AtomicReferenceArrayQueue {
public static final int MAX_LOOK_AHEAD_STEP = Integer.getInteger("jctools.spsc.max.lookahead.step", 4096);
protected final int lookAheadStep;
public SpscAtomicArrayQueueColdField(int capacity) {
super(capacity);
lookAheadStep = Math.min(capacity() / 4, MAX_LOOK_AHEAD_STEP);
}
}
/**
* NOTE: This class was automatically generated by io.netty.util.internal.shaded.org.jctools.queues.atomic.JavaParsingAtomicArrayQueueGenerator
* which can found in the jctools-build module. The original source file is SpscArrayQueue.java.
*/
abstract class SpscAtomicArrayQueueL1Pad extends SpscAtomicArrayQueueColdField {
long p01, p02, p03, p04, p05, p06, p07;
long p10, p11, p12, p13, p14, p15, p16, p17;
public SpscAtomicArrayQueueL1Pad(int capacity) {
super(capacity);
}
}
/**
* NOTE: This class was automatically generated by io.netty.util.internal.shaded.org.jctools.queues.atomic.JavaParsingAtomicArrayQueueGenerator
* which can found in the jctools-build module. The original source file is SpscArrayQueue.java.
*/
abstract class SpscAtomicArrayQueueProducerIndexFields extends SpscAtomicArrayQueueL1Pad {
private static final AtomicLongFieldUpdater P_INDEX_UPDATER = AtomicLongFieldUpdater.newUpdater(SpscAtomicArrayQueueProducerIndexFields.class, "producerIndex");
protected volatile long producerIndex;
protected long producerLimit;
public SpscAtomicArrayQueueProducerIndexFields(int capacity) {
super(capacity);
}
@Override
public final long lvProducerIndex() {
return producerIndex;
}
protected final void soProducerIndex(final long newValue) {
P_INDEX_UPDATER.lazySet(this, newValue);
}
}
/**
* NOTE: This class was automatically generated by io.netty.util.internal.shaded.org.jctools.queues.atomic.JavaParsingAtomicArrayQueueGenerator
* which can found in the jctools-build module. The original source file is SpscArrayQueue.java.
*/
abstract class SpscAtomicArrayQueueL2Pad extends SpscAtomicArrayQueueProducerIndexFields {
long p01, p02, p03, p04, p05, p06, p07;
long p10, p11, p12, p13, p14, p15, p16, p17;
public SpscAtomicArrayQueueL2Pad(int capacity) {
super(capacity);
}
}
/**
* NOTE: This class was automatically generated by io.netty.util.internal.shaded.org.jctools.queues.atomic.JavaParsingAtomicArrayQueueGenerator
* which can found in the jctools-build module. The original source file is SpscArrayQueue.java.
*/
abstract class SpscAtomicArrayQueueConsumerIndexField extends SpscAtomicArrayQueueL2Pad {
private static final AtomicLongFieldUpdater C_INDEX_UPDATER = AtomicLongFieldUpdater.newUpdater(SpscAtomicArrayQueueConsumerIndexField.class, "consumerIndex");
protected volatile long consumerIndex;
public SpscAtomicArrayQueueConsumerIndexField(int capacity) {
super(capacity);
}
public final long lvConsumerIndex() {
return consumerIndex;
}
protected final void soConsumerIndex(final long newValue) {
C_INDEX_UPDATER.lazySet(this, newValue);
}
}
/**
* NOTE: This class was automatically generated by io.netty.util.internal.shaded.org.jctools.queues.atomic.JavaParsingAtomicArrayQueueGenerator
* which can found in the jctools-build module. The original source file is SpscArrayQueue.java.
*/
abstract class SpscAtomicArrayQueueL3Pad extends SpscAtomicArrayQueueConsumerIndexField {
long p01, p02, p03, p04, p05, p06, p07;
long p10, p11, p12, p13, p14, p15, p16, p17;
public SpscAtomicArrayQueueL3Pad(int capacity) {
super(capacity);
}
}
/**
* NOTE: This class was automatically generated by io.netty.util.internal.shaded.org.jctools.queues.atomic.JavaParsingAtomicArrayQueueGenerator
* which can found in the jctools-build module. The original source file is SpscArrayQueue.java.
* 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.
*
* @param
* @author nitsanw
*/
public class SpscAtomicArrayQueue extends SpscAtomicArrayQueueL3Pad {
public SpscAtomicArrayQueue(final int capacity) {
super(Math.max(capacity, 4));
}
/**
* {@inheritDoc}
*
* This implementation is correct for single producer thread use only.
*/
@Override
public boolean offer(final E e) {
if (null == e) {
throw new NullPointerException();
}
// local load of field to avoid repeated loads after volatile reads
final AtomicReferenceArray buffer = this.buffer;
final int mask = this.mask;
final long producerIndex = this.producerIndex;
if (producerIndex >= producerLimit && !offerSlowPath(buffer, mask, producerIndex)) {
return false;
}
final int offset = calcElementOffset(producerIndex, mask);
// StoreStore
soElement(buffer, offset, e);
// ordered store -> atomic and ordered for size()
soProducerIndex(producerIndex + 1);
return true;
}
private boolean offerSlowPath(final AtomicReferenceArray buffer, final int mask, final long producerIndex) {
final int lookAheadStep = this.lookAheadStep;
if (null == lvElement(buffer, calcElementOffset(producerIndex + lookAheadStep, mask))) {
// LoadLoad
producerLimit = producerIndex + lookAheadStep;
} else {
final int offset = calcElementOffset(producerIndex, mask);
if (null != lvElement(buffer, offset)) {
return false;
}
}
return true;
}
/**
* {@inheritDoc}
*
* This implementation is correct for single consumer thread use only.
*/
@Override
public E poll() {
final long consumerIndex = this.consumerIndex;
final int offset = calcElementOffset(consumerIndex);
// local load of field to avoid repeated loads after volatile reads
final AtomicReferenceArray buffer = this.buffer;
// LoadLoad
final E e = lvElement(buffer, offset);
if (null == e) {
return null;
}
// StoreStore
soElement(buffer, offset, null);
// ordered store -> atomic and ordered for size()
soConsumerIndex(consumerIndex + 1);
return e;
}
/**
* {@inheritDoc}
*
* This implementation is correct for single consumer thread use only.
*/
@Override
public E peek() {
return lvElement(buffer, calcElementOffset(consumerIndex));
}
@Override
public boolean relaxedOffer(final E message) {
return offer(message);
}
@Override
public E relaxedPoll() {
return poll();
}
@Override
public E relaxedPeek() {
return peek();
}
@Override
public int drain(final Consumer c) {
return drain(c, capacity());
}
@Override
public int fill(final Supplier s) {
return fill(s, capacity());
}
@Override
public int drain(final Consumer c, final int limit) {
final AtomicReferenceArray buffer = this.buffer;
final int mask = this.mask;
final long consumerIndex = this.consumerIndex;
for (int i = 0; i < limit; i++) {
final long index = consumerIndex + i;
final int offset = calcElementOffset(index, mask);
// LoadLoad
final E e = lvElement(buffer, offset);
if (null == e) {
return i;
}
// StoreStore
soElement(buffer, offset, null);
// ordered store -> atomic and ordered for size()
soConsumerIndex(index + 1);
c.accept(e);
}
return limit;
}
@Override
public int fill(final Supplier s, final int limit) {
final AtomicReferenceArray buffer = this.buffer;
final int mask = this.mask;
final int lookAheadStep = this.lookAheadStep;
final long producerIndex = this.producerIndex;
for (int i = 0; i < limit; i++) {
final long index = producerIndex + i;
final int lookAheadElementOffset = calcElementOffset(index + lookAheadStep, mask);
if (null == lvElement(buffer, lookAheadElementOffset)) {
// LoadLoad
int lookAheadLimit = Math.min(lookAheadStep, limit - i);
for (int j = 0; j < lookAheadLimit; j++) {
final int offset = calcElementOffset(index + j, mask);
// StoreStore
soElement(buffer, offset, s.get());
// ordered store -> atomic and ordered for size()
soProducerIndex(index + j + 1);
}
i += lookAheadLimit - 1;
} else {
final int offset = calcElementOffset(index, mask);
if (null != lvElement(buffer, offset)) {
return i;
}
// StoreStore
soElement(buffer, offset, s.get());
// ordered store -> atomic and ordered for size()
soProducerIndex(index + 1);
}
}
return limit;
}
@Override
public void drain(final Consumer c, final WaitStrategy w, final ExitCondition exit) {
final AtomicReferenceArray buffer = this.buffer;
final int mask = this.mask;
long consumerIndex = this.consumerIndex;
int counter = 0;
while (exit.keepRunning()) {
for (int i = 0; i < 4096; i++) {
final int offset = calcElementOffset(consumerIndex, mask);
// LoadLoad
final E e = lvElement(buffer, offset);
if (null == e) {
counter = w.idle(counter);
continue;
}
consumerIndex++;
counter = 0;
// StoreStore
soElement(buffer, offset, null);
// ordered store -> atomic and ordered for size()
soConsumerIndex(consumerIndex);
c.accept(e);
}
}
}
@Override
public void fill(final Supplier s, final WaitStrategy w, final ExitCondition e) {
final AtomicReferenceArray buffer = this.buffer;
final int mask = this.mask;
final int lookAheadStep = this.lookAheadStep;
long producerIndex = this.producerIndex;
int counter = 0;
while (e.keepRunning()) {
final int lookAheadElementOffset = calcElementOffset(producerIndex + lookAheadStep, mask);
if (null == lvElement(buffer, lookAheadElementOffset)) {
// LoadLoad
for (int j = 0; j < lookAheadStep; j++) {
final int offset = calcElementOffset(producerIndex, mask);
producerIndex++;
// StoreStore
soElement(buffer, offset, s.get());
// ordered store -> atomic and ordered for size()
soProducerIndex(producerIndex);
}
} else {
final int offset = calcElementOffset(producerIndex, mask);
if (null != lvElement(buffer, offset)) {
// LoadLoad
counter = w.idle(counter);
continue;
}
producerIndex++;
counter = 0;
// StoreStore
soElement(buffer, offset, s.get());
// ordered store -> atomic and ordered for size()
soProducerIndex(producerIndex);
}
}
}
}