Permalink
Browse files

Refactor Unsafe related stuff in scala.concurrent.

  • Loading branch information...
Aleksandar Prokopec Aleksandar Prokopec
Aleksandar Prokopec authored and Aleksandar Prokopec committed May 1, 2012
1 parent 187817b commit b3ced6162cefa7dd71d394af65295644d1714748
@@ -9,13 +9,32 @@
package scala.concurrent.impl;
import scala.concurrent.util.Unsafe;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
abstract class AbstractPromise {
private volatile Object _ref;
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 _ref;
}
protected final static AtomicReferenceFieldUpdater<AbstractPromise, Object> updater =
AtomicReferenceFieldUpdater.newUpdater(AbstractPromise.class, Object.class, "_ref");
AtomicReferenceFieldUpdater.newUpdater(AbstractPromise.class, Object.class, "_ref");
}
@@ -11,7 +11,6 @@ package scala.concurrent.impl
import java.util.concurrent.TimeUnit.{ NANOSECONDS, MILLISECONDS }
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater
import scala.concurrent.{Awaitable, ExecutionContext, resolveEither, resolver, blocking, CanAwait, TimeoutException}
//import scala.util.continuations._
import scala.concurrent.util.Duration
@@ -77,7 +76,7 @@ object Promise {
/** Default promise implementation.
*/
class DefaultPromise[T](implicit val executor: ExecutionContext) extends AbstractPromise with Promise[T] { self =>
updater.set(this, Nil) // Start at "No callbacks" //FIXME switch to Unsafe instead of ARFU
updateState(null, Nil) // Start at "No callbacks" //FIXME switch to Unsafe instead of ARFU
protected final def tryAwait(atMost: Duration): Boolean = {
@tailrec
@@ -124,15 +123,6 @@ object Promise {
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 {
@@ -6,22 +6,25 @@
** |/ **
\* */
package scala.concurrent.util;
package scala.concurrent;
import java.lang.reflect.Field;
final class Unsafe {
public final class Unsafe {
public final static sun.misc.Unsafe instance;
static {
try {
sun.misc.Unsafe found = null;
for(Field field : sun.misc.Unsafe.class.getDeclaredFields()) {
if (field.getType() == sun.misc.Unsafe.class) {
field.setAccessible(true);
found = (sun.misc.Unsafe) field.get(null);
break;
}
if (field.getType() == sun.misc.Unsafe.class) {
field.setAccessible(true);
found = (sun.misc.Unsafe) field.get(null);
break;
}
}
if (found == null) throw new IllegalStateException("Can't find instance of sun.misc.Unsafe");
else instance = found;

0 comments on commit b3ced61

Please sign in to comment.