#2023 - Switching from ARFU to Unsafe for the DefaultPromise
This commit is contained in:
parent
0f18259027
commit
fbb3356fb0
2 changed files with 26 additions and 14 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ import akka.event.Logging.LogEventException
|
|||
import akka.event.Logging.Debug
|
||||
import java.util.concurrent.TimeUnit.NANOSECONDS
|
||||
import java.util.concurrent.{ ExecutionException, Callable, TimeoutException }
|
||||
import java.util.concurrent.atomic.{ AtomicInteger, AtomicReferenceFieldUpdater }
|
||||
import java.util.concurrent.atomic.{ AtomicInteger }
|
||||
import akka.pattern.AskTimeoutException
|
||||
import scala.util.DynamicVariable
|
||||
import scala.runtime.BoxedUnit
|
||||
|
|
@ -887,15 +887,6 @@ class DefaultPromise[T](implicit val executor: ExecutionContext) extends Abstrac
|
|||
case _ ⇒ false
|
||||
}
|
||||
|
||||
@inline
|
||||
private[this] final def updater = AbstractPromise.updater.asInstanceOf[AtomicReferenceFieldUpdater[AbstractPromise, AnyRef]]
|
||||
|
||||
@inline
|
||||
protected final def updateState(oldState: AnyRef, newState: AnyRef): Boolean = updater.compareAndSet(this, oldState, newState)
|
||||
|
||||
@inline
|
||||
protected final def getState: AnyRef = updater.get(this)
|
||||
|
||||
def tryComplete(value: Either[Throwable, T]): Boolean = {
|
||||
val callbacks: List[Either[Throwable, T] ⇒ Unit] = {
|
||||
try {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue