| author | blanchet | 
| Fri, 28 Sep 2012 09:12:49 +0200 | |
| changeset 49631 | 9ce0c2cbadb8 | 
| parent 49471 | 97964515a676 | 
| child 56667 | 65e84b0ef974 | 
| permissions | -rw-r--r-- | 
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 1 | /* Title: Pure/Concurrent/simple_thread.scala | 
| 45673 
cd41e3903fbf
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
 wenzelm parents: 
45667diff
changeset | 2 | Module: PIDE | 
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 3 | Author: Makarius | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 4 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 5 | Simplified thread operations. | 
| 
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 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 8 | package isabelle | 
| 
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 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 11 | import java.lang.Thread | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 12 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 13 | import scala.actors.Actor | 
| 
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 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 16 | object Simple_Thread | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 17 | {
 | 
| 49471 
97964515a676
text_rendering as managed task, with cancellation;
 wenzelm parents: 
48355diff
changeset | 18 | /* prending interrupts */ | 
| 
97964515a676
text_rendering as managed task, with cancellation;
 wenzelm parents: 
48355diff
changeset | 19 | |
| 
97964515a676
text_rendering as managed task, with cancellation;
 wenzelm parents: 
48355diff
changeset | 20 | def interrupted_exception(): Unit = | 
| 
97964515a676
text_rendering as managed task, with cancellation;
 wenzelm parents: 
48355diff
changeset | 21 | if (Thread.interrupted()) throw new InterruptedException | 
| 
97964515a676
text_rendering as managed task, with cancellation;
 wenzelm parents: 
48355diff
changeset | 22 | |
| 
97964515a676
text_rendering as managed task, with cancellation;
 wenzelm parents: 
48355diff
changeset | 23 | |
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 24 | /* plain thread */ | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 25 | |
| 39577 | 26 | def fork(name: String = "", daemon: Boolean = false)(body: => Unit): Thread = | 
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 27 |   {
 | 
| 39577 | 28 | val thread = | 
| 29 |       if (name == null || name == "") new Thread() { override def run = body }
 | |
| 30 |       else new Thread(name) { override def run = body }
 | |
| 38638 | 31 | thread.setDaemon(daemon) | 
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 32 | thread.start | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 33 | thread | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 34 | } | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 35 | |
| 39579 | 36 | |
| 37 | /* future result via thread */ | |
| 39577 | 38 | |
| 48355 
6b36da29a0bf
support for detached Bash_Job with some control operations;
 wenzelm parents: 
45673diff
changeset | 39 | def future[A](name: String = "", daemon: Boolean = false)(body: => A): (Thread, Future[A]) = | 
| 39577 | 40 |   {
 | 
| 41 | val result = Future.promise[A] | |
| 48355 
6b36da29a0bf
support for detached Bash_Job with some control operations;
 wenzelm parents: 
45673diff
changeset | 42 |     val thread = fork(name, daemon) { result.fulfill_result(Exn.capture(body)) }
 | 
| 
6b36da29a0bf
support for detached Bash_Job with some control operations;
 wenzelm parents: 
45673diff
changeset | 43 | (thread, result) | 
| 39577 | 44 | } | 
| 45 | ||
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 46 | |
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 47 | /* thread as actor */ | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 48 | |
| 39572 | 49 | def actor(name: String, daemon: Boolean = false)(body: => Unit): (Thread, Actor) = | 
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 50 |   {
 | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 51 | val actor = Future.promise[Actor] | 
| 38638 | 52 |     val thread = fork(name, daemon) { actor.fulfill(Actor.self); body }
 | 
| 39572 | 53 | (thread, actor.join) | 
| 38636 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 54 | } | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 55 | } | 
| 
b7647ca7de5a
module for simplified thread operations (Scala version);
 wenzelm parents: diff
changeset | 56 |