src/Pure/Concurrent/future.scala
author wenzelm
Tue, 29 Dec 2015 16:23:34 +0100
changeset 61959 364007370bb7
parent 61563 91c3aedbfc5e
child 62247 ec35b8aca636
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Concurrent/future.scala
45673
cd41e3903fbf separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents: 45667
diff changeset
     2
    Module:     PIDE
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     4
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
     5
Value-oriented parallel execution via futures and promises.
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     6
*/
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     7
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     8
package isabelle
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     9
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    10
61563
91c3aedbfc5e tuned signature;
wenzelm
parents: 61559
diff changeset
    11
import java.util.concurrent.Callable
91c3aedbfc5e tuned signature;
wenzelm
parents: 61559
diff changeset
    12
91c3aedbfc5e tuned signature;
wenzelm
parents: 61559
diff changeset
    13
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    14
/* futures and promises */
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    15
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    16
object Future
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    17
{
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    18
  def value[A](x: A): Future[A] = new Value_Future(x)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    19
  def fork[A](body: => A): Future[A] = new Task_Future[A](body)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    20
  def promise[A]: Promise[A] = new Promise_Future[A]
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    21
  def thread[A](name: String = "", daemon: Boolean = false)(body: => A): Future[A] =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    22
    new Thread_Future[A](name, daemon, body)
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    23
}
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    24
56673
42bf8fffdf6a modernized Future/Promise implementation, bypassing old actors;
wenzelm
parents: 56663
diff changeset
    25
trait Future[A]
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    26
{
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    27
  def peek: Option[Exn.Result[A]]
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    28
  def is_finished: Boolean = peek.isDefined
38848
9483bb678d96 use Future.get_finished where this is the intended meaning -- prefer immediate crash over deadlock due to promises that are never fulfilled;
wenzelm
parents: 34262
diff changeset
    29
  def get_finished: A = { require(is_finished); Exn.release(peek.get) }
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    30
  def join_result: Exn.Result[A]
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    31
  def join: A = Exn.release(join_result)
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    32
  def map[B](f: A => B): Future[B] = Future.fork { f(join) }
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    33
  def cancel: Unit
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    34
57912
wenzelm
parents: 57350
diff changeset
    35
  override def toString: String =
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    36
    peek match {
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    37
      case None => "<future>"
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    38
      case Some(Exn.Exn(_)) => "<failed>"
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    39
      case Some(Exn.Res(x)) => x.toString
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    40
    }
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    41
}
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    42
56673
42bf8fffdf6a modernized Future/Promise implementation, bypassing old actors;
wenzelm
parents: 56663
diff changeset
    43
trait Promise[A] extends Future[A]
34240
3274571e45c1 added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents: 34217
diff changeset
    44
{
34262
a6d2d9c07e46 added Promise.fulfill_result;
wenzelm
parents: 34240
diff changeset
    45
  def fulfill_result(res: Exn.Result[A]): Unit
56674
wenzelm
parents: 56673
diff changeset
    46
  def fulfill(x: A): Unit
34240
3274571e45c1 added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents: 34217
diff changeset
    47
}
3274571e45c1 added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents: 34217
diff changeset
    48
56673
42bf8fffdf6a modernized Future/Promise implementation, bypassing old actors;
wenzelm
parents: 56663
diff changeset
    49
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    50
/* value future */
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    51
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    52
private class Value_Future[A](x: A) extends Future[A]
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    53
{
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    54
  val peek: Option[Exn.Result[A]] = Some(Exn.Res(x))
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    55
  def join_result: Exn.Result[A] = peek.get
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    56
  def cancel {}
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    57
}
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    58
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    59
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    60
/* task future via thread pool */
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    61
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    62
private class Task_Future[A](body: => A) extends Future[A]
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    63
{
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    64
  private sealed abstract class Status
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    65
  private case object Ready extends Status
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    66
  private case class Running(thread: Thread) extends Status
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    67
  private case object Terminated extends Status
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    68
  private case class Finished(result: Exn.Result[A]) extends Status
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    69
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    70
  private val status = Synchronized[Status](Ready)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    71
56673
42bf8fffdf6a modernized Future/Promise implementation, bypassing old actors;
wenzelm
parents: 56663
diff changeset
    72
  def peek: Option[Exn.Result[A]] =
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    73
    status.value match {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    74
      case Finished(result) => Some(result)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    75
      case _ => None
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    76
    }
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    77
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    78
  private def try_run()
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    79
  {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    80
    val do_run =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    81
      status.change_result {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    82
        case Ready => (true, Running(Thread.currentThread))
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    83
        case st => (false, st)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    84
      }
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    85
    if (do_run) {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    86
      val result = Exn.capture(body)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    87
      status.change(_ => Terminated)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    88
      status.change(_ => Finished(if (Thread.interrupted) Exn.Exn(Exn.Interrupt()) else result))
56673
42bf8fffdf6a modernized Future/Promise implementation, bypassing old actors;
wenzelm
parents: 56663
diff changeset
    89
    }
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    90
  }
61563
91c3aedbfc5e tuned signature;
wenzelm
parents: 61559
diff changeset
    91
  private val task = Standard_Thread.pool.submit(new Callable[A] { def call = body })
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    92
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    93
  def join_result: Exn.Result[A] =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    94
  {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    95
    try_run()
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    96
    status.guarded_access {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    97
      case st @ Finished(result) => Some((result, st))
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    98
      case _ => None
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    99
    }
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   100
  }
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   101
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   102
  def cancel =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   103
  {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   104
    status.change {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   105
      case Ready => task.cancel(false); Finished(Exn.Exn(Exn.Interrupt()))
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   106
      case st @ Running(thread) => thread.interrupt; st
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   107
      case st => st
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   108
    }
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   109
  }
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
   110
}
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
   111
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   112
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   113
/* promise future */
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   114
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   115
private class Promise_Future[A] extends Promise[A]
34240
3274571e45c1 added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents: 34217
diff changeset
   116
{
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   117
  private val state = Synchronized[Option[Exn.Result[A]]](None)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   118
  def peek: Option[Exn.Result[A]] = state.value
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   119
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   120
  def join_result: Exn.Result[A] =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   121
    state.guarded_access(st => if (st.isEmpty) None else Some((st.get, st)))
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   122
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   123
  def fulfill_result(result: Exn.Result[A]): Unit =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   124
    state.change(st => if (st.isEmpty) Some(result) else throw new IllegalStateException)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   125
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   126
  def fulfill(x: A): Unit = fulfill_result(Exn.Res(x))
34240
3274571e45c1 added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents: 34217
diff changeset
   127
59365
b5d43b01a6b3 added Promise.cancel;
wenzelm
parents: 57912
diff changeset
   128
  def cancel: Unit =
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   129
    state.change(st => if (st.isEmpty) Some(Exn.Exn(Exn.Interrupt())) else st)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   130
}
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   131
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   132
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   133
/* thread future */
59365
b5d43b01a6b3 added Promise.cancel;
wenzelm
parents: 57912
diff changeset
   134
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   135
private class Thread_Future[A](name: String, daemon: Boolean, body: => A) extends Future[A]
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   136
{
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   137
  private val result = Future.promise[A]
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   138
  private val thread =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   139
    Standard_Thread.fork(name, daemon) { result.fulfill_result(Exn.capture(body)) }
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   140
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   141
  def peek: Option[Exn.Result[A]] = result.peek
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   142
  def join_result: Exn.Result[A] = result.join_result
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   143
  def cancel: Unit = thread.interrupt
34240
3274571e45c1 added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents: 34217
diff changeset
   144
}