trait Future[A] { def peek: Option[Exn.Result[A]] def is_finished: Boolean = peek.isDefined def get_finished: A = { require(is_finished, "future not finished"); Exn.release(peek.get) } def join_result: Exn.Result[A] def join: A = Exn.release(join_result) def map[B](f: A => B): Future[B] = Future.fork { f(join) } def cancel(): Unit
overridedef toString: String =
peek match { case None => "" case Some(Exn.Exn(_)) => "" case Some(Exn.Res(x)) => x.toString
}
}
trait Promise[A] extends Future[A] { def fulfill_result(res: Exn.Result[A]): Unit def fulfill(x: A): Unit
}
/* value future */
privateclass Value_Future[A](x: A) extends Future[A] { val peek: Option[Exn.Result[A]] = Some(Exn.Res(x)) def join_result: Exn.Result[A] = peek.get def cancel(): Unit = {}
}
/* task future via thread pool */
privateclass Task_Future[A](body: => A) extends Future[A] { private enum Status { case Ready extends Status case Running(thread: Thread) extends Status case Terminated extends Status case Finished(result: Exn.Result[A]) extends Status
}
privateval status = Synchronized[Status](Status.Ready)
def peek: Option[Exn.Result[A]] =
status.value match { case Status.Finished(result) => Some(result) case _ => None
}
privatedef try_run(): Unit = { val do_run =
status.change_result { case Status.Ready => (true, Status.Running(Thread.currentThread)) case st => (false, st)
} if (do_run) { val result = Exn.capture(body)
status.change(_ => Status.Terminated)
status.change(_ =>
Status.Finished(if (Thread.interrupted()) Exn.Exn(Exn.Interrupt()) else result))
}
} privateval task = Isabelle_Thread.pool.submit(new Callable[Unit] { def call = try_run() })
def join_result: Exn.Result[A] = {
try_run()
status.guarded_access { case st @ Status.Finished(result) => Some((result, st)) case _ => None
}
}
def cancel(): Unit = {
status.change { case Status.Ready => task.cancel(false); Status.Finished(Exn.Exn(Exn.Interrupt())) case st @ Status.Running(thread) => thread.interrupt(); st case st => st
}
}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.2Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.