author | wenzelm |
Mon, 11 Sep 2023 19:30:48 +0200 | |
changeset 78659 | b5f3d1051b13 |
parent 77414 | 0d5994eef9e6 |
child 79050 | 4d8716098d41 |
permissions | -rw-r--r-- |
43283 | 1 |
/* Title: Pure/System/isabelle_process.scala |
27949
6eb0327c0b9b
Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
6eb0327c0b9b
Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff
changeset
|
3 |
|
57916 | 4 |
Isabelle process wrapper. |
27949
6eb0327c0b9b
Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff
changeset
|
5 |
*/ |
6eb0327c0b9b
Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff
changeset
|
6 |
|
6eb0327c0b9b
Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
6eb0327c0b9b
Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff
changeset
|
8 |
|
55618 | 9 |
|
73897 | 10 |
import java.util.{Map => JMap} |
65310 | 11 |
import java.io.{File => JFile} |
12 |
||
13 |
||
75393 | 14 |
object Isabelle_Process { |
73802 | 15 |
def start( |
65216 | 16 |
options: Options, |
76729 | 17 |
session: Session, |
76656 | 18 |
session_background: Sessions.Background, |
77414
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents:
77368
diff
changeset
|
19 |
session_heaps: List[Path], |
71639
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents:
71607
diff
changeset
|
20 |
use_prelude: List[String] = Nil, |
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents:
71607
diff
changeset
|
21 |
eval_main: String = "", |
65216 | 22 |
modes: List[String] = Nil, |
65310 | 23 |
cwd: JFile = null, |
75393 | 24 |
env: JMap[String, String] = Isabelle_System.settings() |
25 |
): Isabelle_Process = { |
|
62556
c115e69f457f
more abstract Session.start, without prover command-line;
wenzelm
parents:
62555
diff
changeset
|
26 |
val channel = System_Channel() |
c115e69f457f
more abstract Session.start, without prover command-line;
wenzelm
parents:
62555
diff
changeset
|
27 |
val process = |
57916 | 28 |
try { |
76729 | 29 |
val ml_options = |
30 |
options. |
|
31 |
string.update("system_channel_address", channel.address). |
|
69572
09a6a7c04b45
more robust system channel via options that are private to the user;
wenzelm
parents:
68209
diff
changeset
|
32 |
string.update("system_channel_password", channel.password) |
77414
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents:
77368
diff
changeset
|
33 |
ML_Process(ml_options, session_background, session_heaps, |
71639
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents:
71607
diff
changeset
|
34 |
use_prelude = use_prelude, eval_main = eval_main, |
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents:
71607
diff
changeset
|
35 |
modes = modes, cwd = cwd, env = env) |
57916 | 36 |
} |
69572
09a6a7c04b45
more robust system channel via options that are private to the user;
wenzelm
parents:
68209
diff
changeset
|
37 |
catch { case exn @ ERROR(_) => channel.shutdown(); throw exn } |
57917 | 38 |
|
73802 | 39 |
val isabelle_process = new Isabelle_Process(session, process) |
73803
2141d6c83511
tuned --- potentially more robust (e.g. session.phase_changed vs. isabelle_process.terminated);
wenzelm
parents:
73802
diff
changeset
|
40 |
process.stdin.close() |
73802 | 41 |
session.start(receiver => new Prover(receiver, session.cache, channel, process)) |
42 |
||
43 |
isabelle_process |
|
57917 | 44 |
} |
57916 | 45 |
} |
71604 | 46 |
|
75393 | 47 |
class Isabelle_Process private(session: Session, process: Bash.Process) { |
71607 | 48 |
private val startup = Future.promise[String] |
49 |
private val terminated = Future.promise[Process_Result] |
|
71604 | 50 |
|
51 |
session.phase_changed += |
|
52 |
Session.Consumer(getClass.getName) { |
|
53 |
case Session.Ready => |
|
71607 | 54 |
startup.fulfill("") |
55 |
case Session.Terminated(result) => |
|
56 |
if (!result.ok && !startup.is_finished) { |
|
76488 | 57 |
val syslog = session.syslog.content() |
77368 | 58 |
val err = "Session startup failed" + if_proper(syslog, ":\n" + syslog) |
71607 | 59 |
startup.fulfill(err) |
60 |
} |
|
61 |
terminated.fulfill(result) |
|
71604 | 62 |
case _ => |
63 |
} |
|
64 |
||
71607 | 65 |
def await_startup(): Isabelle_Process = |
66 |
startup.join match { |
|
67 |
case "" => this |
|
68 |
case err => session.stop(); error(err) |
|
71604 | 69 |
} |
70 |
||
75393 | 71 |
def await_shutdown(): Process_Result = { |
71667
4d2de35214c5
proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents:
71639
diff
changeset
|
72 |
val result = terminated.join |
4d2de35214c5
proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents:
71639
diff
changeset
|
73 |
session.stop() |
4d2de35214c5
proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents:
71639
diff
changeset
|
74 |
result |
4d2de35214c5
proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents:
71639
diff
changeset
|
75 |
} |
71718 | 76 |
|
73367 | 77 |
def terminate(): Unit = process.terminate() |
71667
4d2de35214c5
proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents:
71639
diff
changeset
|
78 |
} |