src/Pure/Concurrent/simple_thread.scala
author blanchet
Thu, 16 Dec 2010 22:45:02 +0100
changeset 41220 4d11b0de7dd8
parent 39586 ea8f3ea13a95
child 45667 546d78f0d81f
permissions -rw-r--r--
discriminate SMT errors a bit better
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
51bcd6003984 added Simple_Thread.future convenience;
wenzelm
parents: 39572
diff changeset
    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
51bcd6003984 added Simple_Thread.future convenience;
wenzelm
parents: 39572
diff changeset
    21
    val thread =
51bcd6003984 added Simple_Thread.future convenience;
wenzelm
parents: 39572
diff changeset
    22
      if (name == null || name == "") new Thread() { override def run = body }
51bcd6003984 added Simple_Thread.future convenience;
wenzelm
parents: 39572
diff changeset
    23
      else new Thread(name) { override def run = body }
38638
94ed0f34aea2 optional daemon flag;
wenzelm
parents: 38636
diff changeset
    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
wenzelm
parents: 39577
diff changeset
    29
wenzelm
parents: 39577
diff changeset
    30
  /* future result via thread */
39577
51bcd6003984 added Simple_Thread.future convenience;
wenzelm
parents: 39572
diff changeset
    31
51bcd6003984 added Simple_Thread.future convenience;
wenzelm
parents: 39572
diff changeset
    32
  def future[A](name: String = "", daemon: Boolean = false)(body: => A): Future[A] =
51bcd6003984 added Simple_Thread.future convenience;
wenzelm
parents: 39572
diff changeset
    33
  {
51bcd6003984 added Simple_Thread.future convenience;
wenzelm
parents: 39572
diff changeset
    34
    val result = Future.promise[A]
51bcd6003984 added Simple_Thread.future convenience;
wenzelm
parents: 39572
diff changeset
    35
    fork(name, daemon) { result.fulfill_result(Exn.capture(body)) }
51bcd6003984 added Simple_Thread.future convenience;
wenzelm
parents: 39572
diff changeset
    36
    result
51bcd6003984 added Simple_Thread.future convenience;
wenzelm
parents: 39572
diff changeset
    37
  }
51bcd6003984 added Simple_Thread.future convenience;
wenzelm
parents: 39572
diff changeset
    38
38636
b7647ca7de5a module for simplified thread operations (Scala version);
wenzelm
parents:
diff changeset
    39
b7647ca7de5a module for simplified thread operations (Scala version);
wenzelm
parents:
diff changeset
    40
  /* thread as actor */
b7647ca7de5a module for simplified thread operations (Scala version);
wenzelm
parents:
diff changeset
    41
39572
bb3469024b6a added Isabelle_Process.syslog;
wenzelm
parents: 38638
diff changeset
    42
  def actor(name: String, daemon: Boolean = false)(body: => Unit): (Thread, Actor) =
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
    val actor = Future.promise[Actor]
38638
94ed0f34aea2 optional daemon flag;
wenzelm
parents: 38636
diff changeset
    45
    val thread = fork(name, daemon) { actor.fulfill(Actor.self); body }
39572
bb3469024b6a added Isabelle_Process.syslog;
wenzelm
parents: 38638
diff changeset
    46
    (thread, actor.join)
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
}
b7647ca7de5a module for simplified thread operations (Scala version);
wenzelm
parents:
diff changeset
    49