From 4b10a4ca64f3d96a33a7f7badbf2a74e1c176fc4 Mon Sep 17 00:00:00 2001 From: Philipp Haller Date: Tue, 25 May 2010 14:58:42 +0000 Subject: Addresses see #3470 by adding a method Reactor.... Addresses see #3470 by adding a method Reactor.restart. Review by rompf. --- src/actors/scala/actors/Actor.scala | 30 ++++++++++++++---------------- src/actors/scala/actors/Reactor.scala | 31 +++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/actors/scala/actors/Actor.scala b/src/actors/scala/actors/Actor.scala index 988e7dda6c..8caa624f67 100644 --- a/src/actors/scala/actors/Actor.scala +++ b/src/actors/scala/actors/Actor.scala @@ -622,24 +622,22 @@ trait Actor extends AbstractActor with ReplyReactor with ActorCanReply with Inpu _state == Actor.State.Terminated } - override def start(): Actor = synchronized { - if (_state == Actor.State.New) { - _state = Actor.State.Runnable - - // Reset various flags. - // - // Note that we do *not* reset `trapExit`. The reason is that - // users should be able to set the field in the constructor - // and before `act` is called. - exitReason = 'normal - shouldExit = false + // guarded by this + private[actors] override def dostart() { + // Reset various flags. + // + // Note that we do *not* reset `trapExit`. The reason is that + // users should be able to set the field in the constructor + // and before `act` is called. + exitReason = 'normal + shouldExit = false - scheduler newActor this - scheduler execute (new Reaction(this)) + super.dostart() + } - this - } else - this + override def start(): Actor = synchronized { + super.start() + this } override def getState: Actor.State.Value = synchronized { diff --git a/src/actors/scala/actors/Reactor.scala b/src/actors/scala/actors/Reactor.scala index b5b9080c27..1c1dfdbd7a 100644 --- a/src/actors/scala/actors/Reactor.scala +++ b/src/actors/scala/actors/Reactor.scala @@ -215,17 +215,32 @@ trait Reactor[Msg >: Null] extends OutputChannel[Msg] with Combinators { scheduler executeFromActor makeReaction(null, handler, msg) } + // guarded by this + private[actors] def dostart() { + _state = Actor.State.Runnable + scheduler newActor this + scheduler execute makeReaction(() => act(), null, null) + } + /** - * Starts this $actor. + * Starts this $actor. This method is idempotent. */ def start(): Reactor[Msg] = synchronized { - if (_state == Actor.State.New) { - _state = Actor.State.Runnable - scheduler newActor this - scheduler execute makeReaction(() => act()) - this - } else - this + if (_state == Actor.State.New) + dostart() + this + } + + /** + * Restarts this $actor. + * + * @throws java.lang.IllegalStateException if the $actor is not in state `Actor.State.Terminated` + */ + def restart(): Unit = synchronized { + if (_state == Actor.State.Terminated) + dostart() + else + throw new IllegalStateException("restart only in state "+Actor.State.Terminated) } /** Returns the execution state of this $actor. -- cgit v1.2.3