diff options
-rw-r--r-- | src/library/scala/concurrent/Actor.scala | 50 | ||||
-rw-r--r-- | src/library/scala/concurrent/NameServer.scala | 48 | ||||
-rw-r--r-- | src/library/scala/concurrent/Pid.scala | 50 | ||||
-rw-r--r-- | src/library/scala/concurrent/Process.scala | 95 |
4 files changed, 243 insertions, 0 deletions
diff --git a/src/library/scala/concurrent/Actor.scala b/src/library/scala/concurrent/Actor.scala new file mode 100644 index 0000000000..f8947d6a6b --- /dev/null +++ b/src/library/scala/concurrent/Actor.scala @@ -0,0 +1,50 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2006, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: Actor.scala 9235 2006-11-13 14:59:18 +0000 (Mon, 13 Nov 2006) mihaylov $ + + +package scala.concurrent + + +import java.lang.Thread + +/** + * The class <code>Actor</code> ... + * + * @author Martin Odersky + * @version 1.0 + * @deprecated use scala.actors package instead + */ +[deprecated] +abstract class Actor extends Thread { + private val in = new MailBox + + def send(msg: in.Message) = + in.send(msg) + + def receive[a](f: PartialFunction[in.Message, a]): a = + if (currentThread == this) in.receive(f) + else throw new IllegalArgumentException("receive called not on own process") + + def receiveWithin[a](msec: long)(f: PartialFunction[in.Message, a]): a = + if (currentThread == this) in.receiveWithin(msec)(f) + else throw new IllegalArgumentException("receiveWithin called not on own process") + + private var pid: Pid = null + + def self = { + if (pid eq null) pid = new Pid(this) + pid + } + + def self_= (p: Pid) = pid = p +} + + + diff --git a/src/library/scala/concurrent/NameServer.scala b/src/library/scala/concurrent/NameServer.scala new file mode 100644 index 0000000000..309382b502 --- /dev/null +++ b/src/library/scala/concurrent/NameServer.scala @@ -0,0 +1,48 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2006, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: NameServer.scala 9110 2006-11-01 16:03:28 +0000 (Wed, 01 Nov 2006) mihaylov $ + + +package scala.concurrent + + +/** + * @author Erik Stenman + * @version 1.0, 01/10/2003 + * @deprecated use scala.actors package instead + */ +[deprecated] +object NameServer { + + val names = new scala.collection.mutable.HashMap[Symbol, Process] + + /** + * @param name ... + * @param proc ... + */ + def register(name: Symbol, proc: Process) = { + if (names contains name) throw new IllegalArgumentException("Name:" + name + " already registred") + names += name -> proc + } + + def unregister(name: Symbol) = + if (names contains name) names -= name + else throw new IllegalArgumentException("Name:" + name + " not registred") + + /** + * @param name ... + * @return ... + */ + def whereis(name: Symbol): Option[Process] = + names.get(name) + + def send(name: Symbol, msg: MailBox#Message) = + names(name).send(msg) + +} diff --git a/src/library/scala/concurrent/Pid.scala b/src/library/scala/concurrent/Pid.scala new file mode 100644 index 0000000000..2bd9082d31 --- /dev/null +++ b/src/library/scala/concurrent/Pid.scala @@ -0,0 +1,50 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2006, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: Pid.scala 7955 2006-06-22 13:55:30 +0000 (Thu, 22 Jun 2006) michelou $ + + +package scala.concurrent + +/** + * The class <code>Pid</code> provides process identifiers + * to thread-based actors. + * + * @author Philipp Haller + * @version 1.0 + * @deprecated use scala.actors package instead + */ +[deprecated] +class Pid(actor: Actor) { + private var target = actor + + def !(msg: MailBox#Message) = target send msg + + def spawn(body: Actor => Unit): Pid = { + val a = new Actor { + override def run: Unit = body(this) + } + a.start + a.self + } + + def spawnReceive(cases: PartialFunction[MailBox#Message, Unit]) = { + val a = new Actor { + override def run: Unit = receive(cases) + } + a.start + a.self + } + + override def hashCode() = target.hashCode() + + override def equals(that: Any) = + this.hashCode() == that.hashCode() + + override def toString() = "Pid(" + target + ")" +} diff --git a/src/library/scala/concurrent/Process.scala b/src/library/scala/concurrent/Process.scala new file mode 100644 index 0000000000..e1a663ba74 --- /dev/null +++ b/src/library/scala/concurrent/Process.scala @@ -0,0 +1,95 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: Process.scala 10086 2007-02-21 19:10:41 +0000 (Wed, 21 Feb 2007) odersky $ + + +package scala.concurrent + + +/** This object ... + * + * @author Erik Stenman + * @version 1.0, 01/10/2003 + * @deprecated use scala.actors package instead + */ +[deprecated] +object Process { + + def spawn(body: => Unit): Process = { + val p = new Process(body) + p.start() + p + } + + def spawn_link(body: => Unit): Process = + self.spawn_link(body) + + def send(p: Process, msg: MailBox#Message) = + p.send(msg) + + def receive[a](f: PartialFunction[MailBox#Message, a]): a = + self.receive(f) + + def receiveWithin[a](msec: long)(f: PartialFunction[MailBox#Message, a]): a = + self.receiveWithin(msec)(f) + + /** + * @return the self process + * @throws Predef.UnsupportedOperationException if self called outside a process. + */ + def self: Process = + if (currentThread.isInstanceOf[Process]) + currentThread.asInstanceOf[Process] + else + throw new UnsupportedOperationException("Self called outside a process") + + def exit(p: Process, reason: AnyRef) = + p.exit(reason) + +} + +class Process(body: => Unit) extends Actor() { + private var exitReason: AnyRef = null + private var links: List[Process] = Nil + + override def run() = + try { + body + } + catch { + case _: java.lang.InterruptedException => + signal(exitReason) + case (exitSignal) => + signal(exitSignal) + } + + private def signal(s: MailBox#Message) = + links.foreach { p: Process => p.send(('EXIT, this, s)) } + + def !(msg: MailBox#Message) = + send(msg) + + def link(p: Process) = + links = p::links + + def spawn_link(body: => Unit) = { + val p = new Process(body) + p.link(this) + p.start() + p + } + + //def self = this + + def exit(reason: AnyRef): Unit = { + exitReason = reason + interrupt() + } + +} |