Skip to content

Commit

Permalink
Refactor Unsafe related stuff in scala.concurrent.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandar Prokopec authored and Aleksandar Prokopec committed May 1, 2012
1 parent 187817b commit b3ced61
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
23 changes: 21 additions & 2 deletions src/library/scala/concurrent/impl/AbstractPromise.java
Expand Up @@ -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");
}
12 changes: 1 addition & 11 deletions src/library/scala/concurrent/impl/Promise.scala
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Expand Up @@ -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;
Expand Down

0 comments on commit b3ced61

Please sign in to comment.