Removed boilerplate, added final optmization

This commit is contained in:
Viktor Klang 2010-10-25 00:38:48 +02:00
parent a630caecf3
commit b075b80db9

View file

@ -62,9 +62,10 @@ object MessageDispatcher {
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a> * @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/ */
trait MessageDispatcher extends MailboxFactory with Logging { trait MessageDispatcher extends MailboxFactory with Logging {
import MessageDispatcher._
protected val uuids = new ConcurrentSkipListSet[Uuid] protected val uuids = new ConcurrentSkipListSet[Uuid]
protected val guard = new ReentrantGuard protected val guard = new ReentrantGuard
private val shutdownSchedule = new AtomicInteger(MessageDispatcher.UNSCHEDULED) private var shutdownSchedule = UNSCHEDULED //This can be non-volatile since it is protected by guard withGuard
protected val active = new Switch(false) protected val active = new Switch(false)
/** /**
@ -99,13 +100,13 @@ trait MessageDispatcher extends MailboxFactory with Logging {
if (uuids remove actorRef.uuid) { if (uuids remove actorRef.uuid) {
actorRef.mailbox = null actorRef.mailbox = null
if (uuids.isEmpty){ if (uuids.isEmpty){
shutdownSchedule.get() match { shutdownSchedule match {
case MessageDispatcher.UNSCHEDULED => case UNSCHEDULED =>
shutdownSchedule.set(MessageDispatcher.SCHEDULED) shutdownSchedule = SCHEDULED
Scheduler.scheduleOnce(shutdownAction, timeoutMs, TimeUnit.MILLISECONDS) Scheduler.scheduleOnce(shutdownAction, timeoutMs, TimeUnit.MILLISECONDS)
case MessageDispatcher.SCHEDULED => case SCHEDULED =>
shutdownSchedule.set(MessageDispatcher.RESCHEDULED) shutdownSchedule = RESCHEDULED
case MessageDispatcher.RESCHEDULED => //Already marked for reschedule case RESCHEDULED => //Already marked for reschedule
} }
} }
} }
@ -128,18 +129,18 @@ trait MessageDispatcher extends MailboxFactory with Logging {
private val shutdownAction = new Runnable { private val shutdownAction = new Runnable {
def run = guard withGuard { def run = guard withGuard {
shutdownSchedule.get() match { shutdownSchedule match {
case MessageDispatcher.RESCHEDULED => case RESCHEDULED =>
shutdownSchedule.set(MessageDispatcher.SCHEDULED) shutdownSchedule = SCHEDULED
Scheduler.scheduleOnce(this, timeoutMs, TimeUnit.MILLISECONDS) Scheduler.scheduleOnce(this, timeoutMs, TimeUnit.MILLISECONDS)
case MessageDispatcher.SCHEDULED => case SCHEDULED =>
if (uuids.isEmpty()) { if (uuids.isEmpty()) {
active switchOff { active switchOff {
shutdown // shut down in the dispatcher's references is zero shutdown // shut down in the dispatcher's references is zero
} }
} }
shutdownSchedule.set(MessageDispatcher.UNSCHEDULED) shutdownSchedule = UNSCHEDULED
case MessageDispatcher.UNSCHEDULED => //Do nothing case UNSCHEDULED => //Do nothing
} }
} }
} }