diff --git a/akka-actor/src/main/java/akka/dispatch/AbstractMessageDispatcher.java b/akka-actor/src/main/java/akka/dispatch/AbstractMessageDispatcher.java new file mode 100644 index 0000000000..5d4eee3931 --- /dev/null +++ b/akka-actor/src/main/java/akka/dispatch/AbstractMessageDispatcher.java @@ -0,0 +1,19 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ + +package akka.dispatch; + +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; +import java.util.concurrent.atomic.AtomicLongFieldUpdater; +import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; + +abstract class AbstractMessageDispatcher { + private volatile int _shutdownSchedule; // not initialized because this is faster: 0 == UNSCHEDULED + protected final static AtomicIntegerFieldUpdater shutdownScheduleUpdater = + AtomicIntegerFieldUpdater.newUpdater(AbstractMessageDispatcher.class, "_shutdownSchedule"); + + private volatile long _inhabitants; // not initialized because this is faster + protected final static AtomicLongFieldUpdater inhabitantsUpdater = + AtomicLongFieldUpdater.newUpdater(AbstractMessageDispatcher.class, "_inhabitants"); +} diff --git a/akka-actor/src/main/scala/akka/dispatch/AbstractDispatcher.scala b/akka-actor/src/main/scala/akka/dispatch/AbstractDispatcher.scala index 6cdd6245b0..3e8643c42f 100644 --- a/akka-actor/src/main/scala/akka/dispatch/AbstractDispatcher.scala +++ b/akka-actor/src/main/scala/akka/dispatch/AbstractDispatcher.scala @@ -107,15 +107,14 @@ abstract class MessageDispatcher(val app: ActorSystem) extends AbstractMessageDi /** * Attaches the specified actor instance to this dispatcher */ - final def attach(actor: ActorCell) { - register(actor) - } + final def attach(actor: ActorCell): Unit = register(actor) /** * Detaches the specified actor instance from this dispatcher */ - final def detach(actor: ActorCell) { + final def detach(actor: ActorCell): Unit = try { unregister(actor) + } finally { ifSensibleToDoSoThenScheduleShutdown() } @@ -152,7 +151,7 @@ abstract class MessageDispatcher(val app: ActorSystem) extends AbstractMessageDi () ⇒ if (inhabitantsUpdater.decrementAndGet(this) == 0) ifSensibleToDoSoThenScheduleShutdown() /** - * Don't call this, this calls you. See "attach" for only invocation + * If you override it, you must call it. But only ever once. See "attach" for only invocation */ protected[akka] def register(actor: ActorCell) { inhabitantsUpdater.incrementAndGet(this) @@ -161,7 +160,7 @@ abstract class MessageDispatcher(val app: ActorSystem) extends AbstractMessageDi } /** - * Don't call this, this calls you. See "detach" for the only invocation + * If you override it, you must call it. But only ever once. See "detach" for the only invocation */ protected[akka] def unregister(actor: ActorCell) { inhabitantsUpdater.decrementAndGet(this)