| author | wenzelm | 
| Tue, 21 Sep 2010 22:01:27 +0200 | |
| changeset 39579 | 0d5a32c1bf11 | 
| parent 39577 | 51bcd6003984 | 
| child 39586 | ea8f3ea13a95 | 
| permissions | -rw-r--r-- | 
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 1 | /* Title: Pure/Concurrent/simple_thread.scala | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 2 | Author: Makarius | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 3 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 4 | Simplified thread operations. | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 5 | */ | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 6 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 7 | package isabelle | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 8 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 9 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 10 | import java.lang.Thread | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 11 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 12 | import scala.actors.Actor | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 13 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 14 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 15 | object Simple_Thread | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 16 | {
 | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 17 | /* plain thread */ | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 18 | |
| 39577 | 19 | def fork(name: String = "", daemon: Boolean = false)(body: => Unit): Thread = | 
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 20 |   {
 | 
| 39577 | 21 | val thread = | 
| 22 |       if (name == null || name == "") new Thread() { override def run = body }
 | |
| 23 |       else new Thread(name) { override def run = body }
 | |
| 38638 | 24 | thread.setDaemon(daemon) | 
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 25 | thread.start | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 26 | thread | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 27 | } | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 28 | |
| 39579 | 29 | def fork(body: => Unit): Thread = fork()(body) | 
| 30 | ||
| 31 | ||
| 32 | /* future result via thread */ | |
| 39577 | 33 | |
| 34 | def future[A](name: String = "", daemon: Boolean = false)(body: => A): Future[A] = | |
| 35 |   {
 | |
| 36 | val result = Future.promise[A] | |
| 37 |     fork(name, daemon) { result.fulfill_result(Exn.capture(body)) }
 | |
| 38 | result | |
| 39 | } | |
| 40 | ||
| 41 | def future[A](body: => A): Future[A] = future()(body) | |
| 42 | ||
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 43 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 44 | /* thread as actor */ | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 45 | |
| 39572 | 46 | def actor(name: String, daemon: Boolean = false)(body: => Unit): (Thread, Actor) = | 
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 47 |   {
 | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 48 | val actor = Future.promise[Actor] | 
| 38638 | 49 |     val thread = fork(name, daemon) { actor.fulfill(Actor.self); body }
 | 
| 39572 | 50 | (thread, actor.join) | 
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 51 | } | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 52 | } | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 53 |