#2023 - Switching from ARFU to Unsafe for the DefaultPromise

This commit is contained in:
Viktor Klang 2012-04-26 02:12:59 +02:00
parent 0f18259027
commit fbb3356fb0
2 changed files with 26 additions and 14 deletions

View file

@ -4,10 +4,31 @@
package akka.dispatch;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import akka.util.Unsafe;
abstract class AbstractPromise {
private volatile Object _ref = DefaultPromise.EmptyPending();
protected final static AtomicReferenceFieldUpdater<AbstractPromise, Object> updater =
AtomicReferenceFieldUpdater.newUpdater(AbstractPromise.class, Object.class, "_ref");
private volatile Object _ref;
{
if (!updateState(null, DefaultPromise.EmptyPending()))
throw new ExceptionInInitializerError(new IllegalStateException("AbstractPromise initial value not null!"));
}
final static long _refOffset;
static {
try {
_refOffset = Unsafe.instance.objectFieldOffset(AbstractPromise.class.getDeclaredField("_ref"));
} catch(Throwable t){
throw new ExceptionInInitializerError(t);
}
}
protected final boolean updateState(Object oldState, Object newState) {
return Unsafe.instance.compareAndSwapObject(this, _refOffset, oldState, newState);
}
protected final Object getState() {
return Unsafe.instance.getObjectVolatile(this, _refOffset);
}
}