| author | paulson <lp15@cam.ac.uk> |
| Wed, 09 Mar 2016 17:22:43 +0000 | |
| changeset 62541 | b351da9b4c7d |
| parent 62400 | 833af0d6d469 |
| child 62543 | 57f379ef662f |
| permissions | -rw-r--r-- |
| 60991 | 1 |
/* Title: Pure/Concurrent/bash.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
GNU bash processes, with propagation of interrupts. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
import java.io.{File => JFile, BufferedReader, InputStreamReader,
|
|
11 |
BufferedWriter, OutputStreamWriter} |
|
12 |
||
13 |
||
14 |
object Bash |
|
15 |
{
|
|
16 |
def process(cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*): Process = |
|
17 |
new Process(cwd, env, redirect, args:_*) |
|
18 |
||
19 |
class Process private [Bash]( |
|
20 |
cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*) |
|
21 |
extends Prover.System_Process |
|
22 |
{
|
|
| 62296 | 23 |
private val proc = |
|
62304
e7a52a838a23
more direct invocation of ISABELLE_BASH_PROCESS on Windows;
wenzelm
parents:
62303
diff
changeset
|
24 |
{
|
|
e7a52a838a23
more direct invocation of ISABELLE_BASH_PROCESS on Windows;
wenzelm
parents:
62303
diff
changeset
|
25 |
val params = |
|
e7a52a838a23
more direct invocation of ISABELLE_BASH_PROCESS on Windows;
wenzelm
parents:
62303
diff
changeset
|
26 |
List(File.platform_path(Path.variable("ISABELLE_BASH_PROCESS")), "-", "bash")
|
|
e7a52a838a23
more direct invocation of ISABELLE_BASH_PROCESS on Windows;
wenzelm
parents:
62303
diff
changeset
|
27 |
Isabelle_System.process( |
|
e7a52a838a23
more direct invocation of ISABELLE_BASH_PROCESS on Windows;
wenzelm
parents:
62303
diff
changeset
|
28 |
cwd, Isabelle_System.settings(env), redirect, (params ::: args.toList):_*) |
|
e7a52a838a23
more direct invocation of ISABELLE_BASH_PROCESS on Windows;
wenzelm
parents:
62303
diff
changeset
|
29 |
} |
| 60991 | 30 |
|
31 |
||
32 |
// channels |
|
33 |
||
34 |
val stdin: BufferedWriter = |
|
35 |
new BufferedWriter(new OutputStreamWriter(proc.getOutputStream, UTF8.charset)) |
|
36 |
||
37 |
val stdout: BufferedReader = |
|
38 |
new BufferedReader(new InputStreamReader(proc.getInputStream, UTF8.charset)) |
|
39 |
||
40 |
val stderr: BufferedReader = |
|
41 |
new BufferedReader(new InputStreamReader(proc.getErrorStream, UTF8.charset)) |
|
42 |
||
43 |
||
44 |
// signals |
|
45 |
||
46 |
private val pid = stdout.readLine |
|
47 |
||
48 |
private def kill(signal: String): Boolean = |
|
| 61025 | 49 |
Exn.Interrupt.postpone {
|
50 |
Isabelle_System.kill(signal, pid) |
|
51 |
Isabelle_System.kill("0", pid)._2 == 0 } getOrElse true
|
|
| 60991 | 52 |
|
53 |
private def multi_kill(signal: String): Boolean = |
|
54 |
{
|
|
55 |
var running = true |
|
56 |
var count = 10 |
|
57 |
while (running && count > 0) {
|
|
58 |
if (kill(signal)) {
|
|
59 |
Exn.Interrupt.postpone {
|
|
60 |
Thread.sleep(100) |
|
61 |
count -= 1 |
|
62 |
} |
|
63 |
} |
|
64 |
else running = false |
|
65 |
} |
|
66 |
running |
|
67 |
} |
|
68 |
||
69 |
def interrupt() { multi_kill("INT") }
|
|
70 |
def terminate() { multi_kill("INT") && multi_kill("TERM") && kill("KILL"); proc.destroy }
|
|
71 |
||
72 |
||
73 |
// JVM shutdown hook |
|
74 |
||
75 |
private val shutdown_hook = new Thread { override def run = terminate() }
|
|
76 |
||
77 |
try { Runtime.getRuntime.addShutdownHook(shutdown_hook) }
|
|
78 |
catch { case _: IllegalStateException => }
|
|
79 |
||
80 |
private def cleanup() = |
|
81 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) }
|
|
82 |
catch { case _: IllegalStateException => }
|
|
83 |
||
84 |
||
85 |
/* result */ |
|
86 |
||
87 |
def join: Int = { val rc = proc.waitFor; cleanup(); rc }
|
|
88 |
} |
|
89 |
} |