co.paralleluniverse.strands.queues.SingleConsumerArrayPrimitiveQueue Maven / Gradle / Ivy
/*
* Quasar: lightweight threads and actors for the JVM.
* Copyright (c) 2013-2015, Parallel Universe Software Co. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 3.0
* as published by the Free Software Foundation.
*/
package co.paralleluniverse.strands.queues;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
/**
*
* @author pron
*/
abstract class SingleConsumerArrayPrimitiveQueue extends SingleConsumerArrayQueue {
private volatile Object p001, p002, p003, p004, p005, p006, p007, p008, p009, p010, p011, p012, p013, p014, p015;
volatile long maxReadIndex;
public SingleConsumerArrayPrimitiveQueue(int capacity) {
super(capacity);
}
@Override
long maxReadIndex() {
return maxReadIndex;
}
@Override
void clearValue(int index) {
}
@Override
boolean hasNext(long lind, int iind) {
return lind < maxReadIndex;
}
@SuppressWarnings("empty-statement")
@Override
void awaitValue(long i) {
while (maxReadIndex < i)
;
}
@SuppressWarnings("empty-statement")
final void postEnq(long i) {
if (true) {
while (maxReadIndex != i)
;
maxReadIndex = i + 1;
} else {
while (!compareAndSetMaxReadIndex(i, i + 1))
;
}
}
private static final VarHandle MAX_READ_INDEX;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
MAX_READ_INDEX = l.findVarHandle(SingleConsumerArrayPrimitiveQueue.class, "maxReadIndex", long.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
/**
* CAS maxReadIndex field. Used only by postEnq.
*/
private boolean compareAndSetMaxReadIndex(long expect, long update) {
return MAX_READ_INDEX.compareAndSet(this, expect, update);
}
}