src/Pure/System/invoke_scala.scala
changeset 56387 d92eb5c3960d
parent 56385 76acce58aeab
child 56667 65e84b0ef974
equal deleted inserted replaced
56386:fe520afb8041 56387:d92eb5c3960d
    70 
    70 
    71 class Invoke_Scala extends Session.Protocol_Handler
    71 class Invoke_Scala extends Session.Protocol_Handler
    72 {
    72 {
    73   private var futures = Map.empty[String, java.util.concurrent.Future[Unit]]
    73   private var futures = Map.empty[String, java.util.concurrent.Future[Unit]]
    74 
    74 
    75   private def fulfill(prover: Session.Prover,
    75   private def fulfill(prover: Prover, id: String, tag: Invoke_Scala.Tag.Value, res: String): Unit =
    76     id: String, tag: Invoke_Scala.Tag.Value, res: String): Unit = synchronized
    76     synchronized
    77   {
    77     {
    78     if (futures.isDefinedAt(id)) {
    78       if (futures.isDefinedAt(id)) {
    79       prover.protocol_command("Invoke_Scala.fulfill", id, tag.toString, res)
    79         prover.protocol_command("Invoke_Scala.fulfill", id, tag.toString, res)
    80       futures -= id
    80         futures -= id
       
    81       }
    81     }
    82     }
    82   }
       
    83 
    83 
    84   private def cancel(prover: Session.Prover,
    84   private def cancel(prover: Prover, id: String, future: java.util.concurrent.Future[Unit])
    85     id: String, future: java.util.concurrent.Future[Unit])
       
    86   {
    85   {
    87     future.cancel(true)
    86     future.cancel(true)
    88     fulfill(prover, id, Invoke_Scala.Tag.INTERRUPT, "")
    87     fulfill(prover, id, Invoke_Scala.Tag.INTERRUPT, "")
    89   }
    88   }
    90 
    89 
    91   private def invoke_scala(
    90   private def invoke_scala(prover: Prover, msg: Prover.Protocol_Output): Boolean = synchronized
    92     prover: Session.Prover, msg: Prover.Protocol_Output): Boolean = synchronized
       
    93   {
    91   {
    94     msg.properties match {
    92     msg.properties match {
    95       case Markup.Invoke_Scala(name, id) =>
    93       case Markup.Invoke_Scala(name, id) =>
    96         futures += (id ->
    94         futures += (id ->
    97           default_thread_pool.submit(() =>
    95           default_thread_pool.submit(() =>
   102         true
   100         true
   103       case _ => false
   101       case _ => false
   104     }
   102     }
   105   }
   103   }
   106 
   104 
   107   private def cancel_scala(
   105   private def cancel_scala(prover: Prover, msg: Prover.Protocol_Output): Boolean = synchronized
   108     prover: Session.Prover, msg: Prover.Protocol_Output): Boolean = synchronized
       
   109   {
   106   {
   110     msg.properties match {
   107     msg.properties match {
   111       case Markup.Cancel_Scala(id) =>
   108       case Markup.Cancel_Scala(id) =>
   112         futures.get(id) match {
   109         futures.get(id) match {
   113           case Some(future) => cancel(prover, id, future)
   110           case Some(future) => cancel(prover, id, future)
   116         true
   113         true
   117       case _ => false
   114       case _ => false
   118     }
   115     }
   119   }
   116   }
   120 
   117 
   121   override def stop(prover: Session.Prover): Unit = synchronized
   118   override def stop(prover: Prover): Unit = synchronized
   122   {
   119   {
   123     for ((id, future) <- futures) cancel(prover, id, future)
   120     for ((id, future) <- futures) cancel(prover, id, future)
   124     futures = Map.empty
   121     futures = Map.empty
   125   }
   122   }
   126 
   123