src/Pure/Concurrent/future.scala
author nipkow
Wed, 04 Dec 2019 18:28:24 +0100
changeset 71230 095cf95d7725
parent 64370 865b39487b5d
child 71685 d5773922358d
permissions -rw-r--r--
moved segment lemmas where they belong
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
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     3
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
     4
Value-oriented parallel execution via futures and promises.
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     5
*/
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     6
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     7
package isabelle
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     8
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
     9
61563
91c3aedbfc5e tuned signature;
wenzelm
parents: 61559
diff changeset
    10
import java.util.concurrent.Callable
91c3aedbfc5e tuned signature;
wenzelm
parents: 61559
diff changeset
    11
91c3aedbfc5e tuned signature;
wenzelm
parents: 61559
diff changeset
    12
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    13
/* futures and promises */
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    14
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    15
object Future
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    16
{
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    17
  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
    18
  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
    19
  def promise[A]: Promise[A] = new Promise_Future[A]
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    20
  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
    21
    new Thread_Future[A](name, daemon, body)
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    22
}
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    23
56673
42bf8fffdf6a modernized Future/Promise implementation, bypassing old actors;
wenzelm
parents: 56663
diff changeset
    24
trait Future[A]
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    25
{
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    26
  def peek: Option[Exn.Result[A]]
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    27
  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
    28
  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
    29
  def join_result: Exn.Result[A]
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    30
  def join: A = Exn.release(join_result)
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    31
  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
    32
  def cancel: Unit
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    33
57912
wenzelm
parents: 57350
diff changeset
    34
  override def toString: String =
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    35
    peek match {
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    36
      case None => "<future>"
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    37
      case Some(Exn.Exn(_)) => "<failed>"
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    38
      case Some(Exn.Res(x)) => x.toString
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    39
    }
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    40
}
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    41
56673
42bf8fffdf6a modernized Future/Promise implementation, bypassing old actors;
wenzelm
parents: 56663
diff changeset
    42
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
    43
{
34262
a6d2d9c07e46 added Promise.fulfill_result;
wenzelm
parents: 34240
diff changeset
    44
  def fulfill_result(res: Exn.Result[A]): Unit
56674
wenzelm
parents: 56673
diff changeset
    45
  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
    46
}
3274571e45c1 added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents: 34217
diff changeset
    47
56673
42bf8fffdf6a modernized Future/Promise implementation, bypassing old actors;
wenzelm
parents: 56663
diff changeset
    48
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    49
/* value future */
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    50
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    51
private class Value_Future[A](x: A) extends Future[A]
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    52
{
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    53
  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
    54
  def join_result: Exn.Result[A] = peek.get
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    55
  def cancel {}
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    56
}
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    57
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    58
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    59
/* task future via thread pool */
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    60
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    61
private class Task_Future[A](body: => A) extends Future[A]
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    62
{
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    63
  private sealed abstract class Status
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    64
  private case object Ready extends Status
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    65
  private case class Running(thread: Thread) extends Status
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    66
  private case object Terminated extends Status
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    67
  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
    68
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    69
  private val status = Synchronized[Status](Ready)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    70
56673
42bf8fffdf6a modernized Future/Promise implementation, bypassing old actors;
wenzelm
parents: 56663
diff changeset
    71
  def peek: Option[Exn.Result[A]] =
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    72
    status.value match {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    73
      case Finished(result) => Some(result)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    74
      case _ => None
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    75
    }
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
  private def try_run()
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    78
  {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    79
    val do_run =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    80
      status.change_result {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    81
        case Ready => (true, Running(Thread.currentThread))
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    82
        case st => (false, st)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    83
      }
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    84
    if (do_run) {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    85
      val result = Exn.capture(body)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    86
      status.change(_ => Terminated)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    87
      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
    88
    }
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    89
  }
62247
ec35b8aca636 proper try_run for exactly one evaluation of body (amending 91c3aedbfc5e);
wenzelm
parents: 61563
diff changeset
    90
  private val task = Standard_Thread.pool.submit(new Callable[Unit] { def call = try_run() })
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
    91
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    92
  def join_result: Exn.Result[A] =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    93
  {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    94
    try_run()
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    95
    status.guarded_access {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    96
      case st @ Finished(result) => Some((result, st))
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    97
      case _ => None
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
    98
    }
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
  def cancel =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   102
  {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   103
    status.change {
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   104
      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
   105
      case st @ Running(thread) => thread.interrupt; st
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   106
      case st => st
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   107
    }
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   108
  }
34217
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
   109
}
67e1ac2d3b2c Future values -- Scala version.
wenzelm
parents:
diff changeset
   110
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   111
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   112
/* promise future */
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   113
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   114
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
   115
{
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   116
  private val state = Synchronized[Option[Exn.Result[A]]](None)
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   117
  def peek: Option[Exn.Result[A]] = state.value
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   118
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   119
  def join_result: Exn.Result[A] =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   120
    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
   121
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   122
  def fulfill_result(result: Exn.Result[A]): Unit =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   123
    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
   124
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   125
  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
   126
59365
b5d43b01a6b3 added Promise.cancel;
wenzelm
parents: 57912
diff changeset
   127
  def cancel: Unit =
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   128
    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
   129
}
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
/* thread future */
59365
b5d43b01a6b3 added Promise.cancel;
wenzelm
parents: 57912
diff changeset
   133
61559
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   134
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
   135
{
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   136
  private val result = Future.promise[A]
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   137
  private val thread =
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   138
    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
   139
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   140
  def peek: Option[Exn.Result[A]] = result.peek
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   141
  def join_result: Exn.Result[A] = result.join_result
313eca3fa847 more direct task future implementation, with proper cancel operation;
wenzelm
parents: 61556
diff changeset
   142
  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
   143
}