From 1f83b1f07ce20c675b3df1c06e80b2f64e924554 Mon Sep 17 00:00:00 2001 From: Philipp Haller Date: Thu, 22 Jan 2009 19:04:51 +0000 Subject: Whenever an actor is created it inherits the sc... Whenever an actor is created it inherits the scheduler from the creating actor. Prepares for resolution of #1405. --- src/actors/scala/actors/Actor.scala | 58 +++++++++++++++++++------------- src/actors/scala/actors/ActorProxy.scala | 4 +-- 2 files changed, 36 insertions(+), 26 deletions(-) (limited to 'src/actors') diff --git a/src/actors/scala/actors/Actor.scala b/src/actors/scala/actors/Actor.scala index 84c9d497a0..967e5c758b 100644 --- a/src/actors/scala/actors/Actor.scala +++ b/src/actors/scala/actors/Actor.scala @@ -37,13 +37,21 @@ object Actor { * * @return returns the currently executing actor. */ - def self: Actor = { - var a = tl.get.asInstanceOf[Actor] - if (null eq a) { - a = new ActorProxy(currentThread) - tl.set(a) - } - a + def self: Actor = self(Scheduler) + + private[actors] def self(sched: IScheduler): Actor = { + val s = tl.get + if (s eq null) { + val r = new ActorProxy(currentThread, sched) + tl.set(r) + r + } else + s + } + + private def parentScheduler: IScheduler = { + val s = tl.get + if (s eq null) Scheduler else s.scheduler } /** @@ -55,9 +63,9 @@ object Actor { * even if its ActorProxy has died for some reason. */ def resetProxy { - val a = tl.get.asInstanceOf[Actor] + val a = tl.get if ((null ne a) && a.isInstanceOf[ActorProxy]) - tl.set(new ActorProxy(currentThread)) + tl.set(new ActorProxy(currentThread, parentScheduler)) } /** @@ -90,11 +98,12 @@ object Actor { * @return the newly created actor. Note that it is automatically started. */ def actor(body: => Unit): Actor = { - val actor = new Actor { + val a = new Actor { def act() = body + override final val scheduler: IScheduler = parentScheduler } - actor.start() - actor + a.start() + a } /** @@ -125,6 +134,7 @@ object Actor { def act() { Responder.run(body) } + override final val scheduler: IScheduler = parentScheduler } a.start() a @@ -369,7 +379,7 @@ trait Actor extends AbstractActor { protected val mailbox = new MessageQueue private var sessions: List[OutputChannel[Any]] = Nil - protected def scheduler: IScheduler = + protected[actors] def scheduler: IScheduler = Scheduler /** @@ -579,7 +589,7 @@ trait Actor extends AbstractActor { * Sends msg to this actor (asynchronous). */ def !(msg: Any) { - send(msg, Actor.self) + send(msg, Actor.self(scheduler)) } /** @@ -597,7 +607,7 @@ trait Actor extends AbstractActor { * @return the reply */ def !?(msg: Any): Any = { - val replyCh = Actor.self.freshReplyChannel + val replyCh = Actor.self(scheduler).freshReplyChannel send(msg, replyCh) replyCh.receive { case x => x @@ -614,7 +624,7 @@ trait Actor extends AbstractActor { * Some(x) where x is the reply */ def !?(msec: Long, msg: Any): Option[Any] = { - val replyCh = Actor.self.freshReplyChannel + val replyCh = Actor.self(scheduler).freshReplyChannel send(msg, replyCh) replyCh.receiveWithin(msec) { case TIMEOUT => None @@ -627,7 +637,7 @@ trait Actor extends AbstractActor { * returns a future representing the reply value. */ def !!(msg: Any): Future[Any] = { - val ftch = new Channel[Any](Actor.self) + val ftch = new Channel[Any](Actor.self(scheduler)) send(msg, ftch) new Future[Any](ftch) { def apply() = @@ -658,7 +668,7 @@ trait Actor extends AbstractActor { * precise type for the reply value. */ def !![A](msg: Any, f: PartialFunction[Any, A]): Future[A] = { - val ftch = new Channel[Any](Actor.self) + val ftch = new Channel[Any](Actor.self(scheduler)) send(msg, ftch) new Future[A](ftch) { def apply() = @@ -789,7 +799,7 @@ trait Actor extends AbstractActor { } private def seq[a, b](first: => a, next: => b): Unit = { - val s = Actor.self + val s = Actor.self(scheduler) val killNext = s.kill s.kill = () => { s.kill = killNext @@ -825,12 +835,14 @@ trait Actor extends AbstractActor { * Links self to actor defined by body. */ def link(body: => Unit): Actor = { - val actor = new Actor { + assert(Actor.self == this, "link called on actor different from self") + val a = new Actor { def act() = body + override final val scheduler: IScheduler = Actor.this.scheduler } - link(actor) - actor.start() - actor + link(a) + a.start() + a } private[actors] def linkTo(to: AbstractActor) = synchronized { diff --git a/src/actors/scala/actors/ActorProxy.scala b/src/actors/scala/actors/ActorProxy.scala index d53db78a74..221368c6aa 100644 --- a/src/actors/scala/actors/ActorProxy.scala +++ b/src/actors/scala/actors/ActorProxy.scala @@ -8,10 +8,8 @@ // $Id$ - package scala.actors - import java.lang.Thread /** @@ -21,7 +19,7 @@ import java.lang.Thread * @version 0.9.8 * @author Philipp Haller */ -private[actors] class ActorProxy(t: Thread) extends Actor { +private[actors] class ActorProxy(t: Thread, override final val scheduler: IScheduler) extends Actor { def act() {} -- cgit v1.2.3