|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| package edu.rice.cs.plt.concurrent; |
|
36 |
| |
|
37 |
| import java.io.*; |
|
38 |
| import java.util.concurrent.ArrayBlockingQueue; |
|
39 |
| import java.util.concurrent.BlockingQueue; |
|
40 |
| import java.util.concurrent.Executor; |
|
41 |
| |
|
42 |
| import edu.rice.cs.plt.io.IOUtil; |
|
43 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
44 |
| import edu.rice.cs.plt.lambda.Runnable1; |
|
45 |
| import edu.rice.cs.plt.lambda.WrappedException; |
|
46 |
| |
|
47 |
| import static edu.rice.cs.plt.debug.DebugUtil.error; |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| public class ProcessIncrementalTaskController<I, R> extends IncrementalTaskController<I, R> { |
|
62 |
| |
|
63 |
| |
|
64 |
| private JVMBuilder _jvmBuilder; |
|
65 |
| private Executor _executor; |
|
66 |
| private IncrementalTask<? extends I, ? extends R> _task; |
|
67 |
| |
|
68 |
| private Runnable1<? super Process> _onExit; |
|
69 |
| |
|
70 |
| private volatile Thread _t; |
|
71 |
| private volatile ObjectOutputStream _commandSink; |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
1
| public ProcessIncrementalTaskController(JVMBuilder jvmBuilder, Executor executor,
|
|
83 |
| IncrementalTask<? extends I, ? extends R> task, |
|
84 |
| boolean ignoreIntermediate) { |
|
85 |
1
| super(ignoreIntermediate);
|
|
86 |
1
| _jvmBuilder = jvmBuilder;
|
|
87 |
1
| _executor = executor;
|
|
88 |
1
| _task = task;
|
|
89 |
1
| _onExit = null;
|
|
90 |
1
| _t = null;
|
|
91 |
1
| _commandSink = null;
|
|
92 |
| } |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
0
| public ProcessIncrementalTaskController(JVMBuilder jvmBuilder,
|
|
110 |
| Executor executor, |
|
111 |
| IncrementalTask<? extends I, ? extends R> task, |
|
112 |
| boolean ignoreIntermediate, |
|
113 |
| Runnable1<? super Process> onExit) { |
|
114 |
0
| super(ignoreIntermediate);
|
|
115 |
0
| _jvmBuilder = jvmBuilder;
|
|
116 |
0
| _executor = executor;
|
|
117 |
0
| _task = task;
|
|
118 |
0
| _onExit = onExit;
|
|
119 |
0
| _t = null;
|
|
120 |
0
| _commandSink = null;
|
|
121 |
| } |
|
122 |
| |
|
123 |
| |
|
124 |
1
| protected void doStart() {
|
|
125 |
1
| _executor.execute(new Runnable() {
|
|
126 |
1
| public void run() {
|
|
127 |
1
| _t = Thread.currentThread();
|
|
128 |
1
| try {
|
|
129 |
| |
|
130 |
0
| if (Thread.interrupted()) { throw new InterruptedException(); }
|
|
131 |
1
| Process p = _jvmBuilder.start(Runner.class.getName(), IterUtil.<String>empty());
|
|
132 |
1
| try {
|
|
133 |
1
| InputStream in = p.getInputStream();
|
|
134 |
| |
|
135 |
1
| int matching = 0;
|
|
136 |
1
| while (matching < Runner.PREFIX.length) {
|
|
137 |
4
| int read = in.read();
|
|
138 |
0
| if (read == -1) { throw new EOFException("Data prefix not found"); }
|
|
139 |
4
| else if ((byte) read == Runner.PREFIX[matching]) { matching++; }
|
|
140 |
0
| else if ((byte) read == Runner.PREFIX[0]) { matching = 1; }
|
|
141 |
0
| else { matching = 0; }
|
|
142 |
| } |
|
143 |
| |
|
144 |
1
| ObjectInputStream objIn = new ObjectInputStream(in);
|
|
145 |
1
| try {
|
|
146 |
1
| ObjectOutputStream objOut = new ObjectOutputStream(p.getOutputStream());
|
|
147 |
1
| try {
|
|
148 |
1
| objOut.writeObject(_task);
|
|
149 |
1
| objOut.writeObject(Command.RUN);
|
|
150 |
1
| objOut.flush();
|
|
151 |
1
| _commandSink = objOut;
|
|
152 |
| |
|
153 |
1
| Result r;
|
|
154 |
1
| do {
|
|
155 |
9
| r = (Result) objIn.readObject();
|
|
156 |
9
| r.handle(ProcessIncrementalTaskController.this);
|
|
157 |
9
| } while (!(r instanceof FinishResult));
|
|
158 |
1
| if (r instanceof CleanFinishResult) {
|
|
159 |
| |
|
160 |
1
| Runnable1<? super Process> onExit = _onExit;
|
|
161 |
0
| if (onExit != null) { p.waitFor(); onExit.run(p); }
|
|
162 |
| } |
|
163 |
0
| else { p.destroy(); }
|
|
164 |
| } |
|
165 |
1
| finally { objOut.close(); }
|
|
166 |
| } |
|
167 |
1
| finally { objIn.close(); }
|
|
168 |
| } |
|
169 |
| catch (EOFException e) { |
|
170 |
0
| p.destroy();
|
|
171 |
0
| throw new IOException("Unable to run process; class path may need to be adjusted");
|
|
172 |
| } |
|
173 |
| |
|
174 |
0
| catch (Throwable e) { p.destroy(); throw e; }
|
|
175 |
| } |
|
176 |
| catch (InterruptedException e) { } |
|
177 |
| catch (InterruptedIOException e) { } |
|
178 |
0
| catch (RuntimeException e) { finishedWithImplementationException(e); }
|
|
179 |
0
| catch (Throwable t) { finishedWithImplementationException(new WrappedException(t)); }
|
|
180 |
| } |
|
181 |
| }); |
|
182 |
| } |
|
183 |
| |
|
184 |
0
| protected void doStop() { writeCommand(Command.CANCEL); }
|
|
185 |
0
| protected void doPause() { writeCommand(Command.PAUSE); }
|
|
186 |
0
| protected void doResume() { writeCommand(Command.RUN); }
|
|
187 |
| |
|
188 |
0
| private void writeCommand(Command c) {
|
|
189 |
0
| try { _commandSink.writeObject(c); _commandSink.flush(); }
|
|
190 |
| catch (IOException e) { |
|
191 |
0
| finishedWithImplementationException(new WrappedException(e));
|
|
192 |
0
| _t.interrupt();
|
|
193 |
| } |
|
194 |
| } |
|
195 |
| |
|
196 |
1
| protected void discard() {
|
|
197 |
1
| _jvmBuilder = null;
|
|
198 |
1
| _executor = null;
|
|
199 |
1
| _task = null;
|
|
200 |
1
| _onExit = null;
|
|
201 |
1
| _t = null;
|
|
202 |
1
| _commandSink = null;
|
|
203 |
| } |
|
204 |
| |
|
205 |
| |
|
206 |
| private static enum Command { RUN, PAUSE, CANCEL; } |
|
207 |
| |
|
208 |
| |
|
209 |
| private static abstract class Result implements Serializable { |
|
210 |
| public abstract <I, R> void handle(ProcessIncrementalTaskController<I, R> c); |
|
211 |
| } |
|
212 |
| |
|
213 |
| private static class StartedResult extends Result { |
|
214 |
2
| public <I, R> void handle(ProcessIncrementalTaskController<I, R> c) { c.started(); }
|
|
215 |
| } |
|
216 |
| |
|
217 |
| private static class PausedResult extends Result { |
|
218 |
1
| public <I, R> void handle(ProcessIncrementalTaskController<I, R> c) { c.paused(); }
|
|
219 |
| } |
|
220 |
| |
|
221 |
| private static class StepResult extends Result { |
|
222 |
| private final Object _value; |
|
223 |
5
| public StepResult(Object value) { _value = value; }
|
|
224 |
| |
|
225 |
5
| @SuppressWarnings("unchecked") public <I, R> void handle(ProcessIncrementalTaskController<I, R> c) {
|
|
226 |
5
| c.stepped((I) _value);
|
|
227 |
| } |
|
228 |
| } |
|
229 |
| |
|
230 |
| private static abstract class FinishResult extends Result {} |
|
231 |
| |
|
232 |
| private static class CleanFinishResult extends FinishResult { |
|
233 |
| private final Object _value; |
|
234 |
1
| public CleanFinishResult(Object value) { _value = value; }
|
|
235 |
| |
|
236 |
1
| @SuppressWarnings("unchecked") public <I, R> void handle(ProcessIncrementalTaskController<I, R> c) {
|
|
237 |
1
| c.finishedCleanly((R) _value);
|
|
238 |
| } |
|
239 |
| } |
|
240 |
| |
|
241 |
| private static class TaskExceptionResult extends FinishResult { |
|
242 |
| private final Exception _e; |
|
243 |
0
| public TaskExceptionResult(Exception e) { _e = e; }
|
|
244 |
0
| public <I, R> void handle(ProcessIncrementalTaskController<I, R> c) { c.finishedWithTaskException(_e); }
|
|
245 |
| } |
|
246 |
| |
|
247 |
| private static class ImplementationExceptionResult extends FinishResult { |
|
248 |
| private final RuntimeException _e; |
|
249 |
1
| public ImplementationExceptionResult(Throwable t) {
|
|
250 |
0
| if (t instanceof RuntimeException) { _e = (RuntimeException) t; }
|
|
251 |
1
| else { _e = new WrappedException(t); }
|
|
252 |
| } |
|
253 |
0
| public <I, R> void handle(ProcessIncrementalTaskController<I, R> c) { c.finishedWithImplementationException(_e); }
|
|
254 |
| } |
|
255 |
| |
|
256 |
| private static class CanceledResult extends FinishResult { |
|
257 |
0
| public <I, R> void handle(ProcessIncrementalTaskController<I, R> c) { c.stopped(); }
|
|
258 |
| } |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| private static class Runner { |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| public static final byte[] PREFIX = { 0x00, 0x7f, 0x03, -0x80 }; |
|
275 |
| |
|
276 |
| private final IncrementalTask<?, ?> _task; |
|
277 |
| private final ObjectOutputStream _objOut; |
|
278 |
| private final ObjectInputStream _objIn; |
|
279 |
| private final CompletionMonitor _continueMonitor; |
|
280 |
| private final BlockingQueue<Result> _results; |
|
281 |
| private final Thread _taskThread; |
|
282 |
| private final Thread _objInReader; |
|
283 |
| |
|
284 |
1
| public Runner(IncrementalTask<?, ?> task, ObjectOutputStream objOut, ObjectInputStream objIn) {
|
|
285 |
1
| _task = task;
|
|
286 |
1
| _objOut = objOut;
|
|
287 |
1
| _objIn = objIn;
|
|
288 |
1
| _continueMonitor = new CompletionMonitor(false);
|
|
289 |
1
| _results = new ArrayBlockingQueue<Result>(256);
|
|
290 |
| |
|
291 |
1
| _taskThread = new Thread("task runner") {
|
|
292 |
1
| public void run() {
|
|
293 |
1
| try {
|
|
294 |
1
| try {
|
|
295 |
1
| while (!_task.isResolved()) {
|
|
296 |
5
| authorizeContinue();
|
|
297 |
5
| _results.put(new StepResult(_task.step()));
|
|
298 |
| } |
|
299 |
1
| authorizeContinue();
|
|
300 |
1
| _results.put(new CleanFinishResult(_task.value()));
|
|
301 |
| } |
|
302 |
0
| catch (InterruptedException e) { _results.put(new CanceledResult()); }
|
|
303 |
| catch (WrappedException e) { |
|
304 |
0
| if (e.getCause() instanceof InterruptedException) { _results.put(new CanceledResult()); }
|
|
305 |
0
| else { _results.put(new TaskExceptionResult(e)); }
|
|
306 |
| } |
|
307 |
0
| catch (RuntimeException e) { _results.put(new TaskExceptionResult(e)); }
|
|
308 |
0
| catch (Throwable t) { _results.put(new ImplementationExceptionResult(t)); }
|
|
309 |
| } |
|
310 |
| catch (InterruptedException e) { } |
|
311 |
| } |
|
312 |
| }; |
|
313 |
| |
|
314 |
1
| _objInReader = new Thread("objIn reader") {
|
|
315 |
1
| public void run() {
|
|
316 |
1
| try {
|
|
317 |
1
| try {
|
|
318 |
2
| while (!Thread.interrupted()) {
|
|
319 |
2
| Command c = (Command) _objIn.readObject();
|
|
320 |
1
| switch (c) {
|
|
321 |
1
| case RUN: _continueMonitor.signal(); break;
|
|
322 |
0
| case PAUSE: _continueMonitor.reset(); break;
|
|
323 |
0
| case CANCEL: _taskThread.interrupt(); break;
|
|
324 |
| } |
|
325 |
| } |
|
326 |
| } |
|
327 |
| catch (InterruptedIOException e) { } |
|
328 |
1
| catch (Throwable t) { _results.put(new ImplementationExceptionResult(t)); }
|
|
329 |
| } |
|
330 |
| catch (InterruptedException e) { } |
|
331 |
| } |
|
332 |
| }; |
|
333 |
| } |
|
334 |
| |
|
335 |
1
| public void run() throws IOException, InterruptedException {
|
|
336 |
| |
|
337 |
| |
|
338 |
1
| _objInReader.start();
|
|
339 |
1
| _taskThread.start();
|
|
340 |
1
| try {
|
|
341 |
1
| Result r;
|
|
342 |
1
| do {
|
|
343 |
8
| r = _results.take();
|
|
344 |
8
| _objOut.writeObject(r);
|
|
345 |
8
| _objOut.flush();
|
|
346 |
8
| } while (!(r instanceof FinishResult));
|
|
347 |
| } |
|
348 |
| finally { |
|
349 |
| |
|
350 |
1
| _objInReader.interrupt();
|
|
351 |
1
| _taskThread.interrupt();
|
|
352 |
| } |
|
353 |
| } |
|
354 |
| |
|
355 |
6
| private void authorizeContinue() throws InterruptedException {
|
|
356 |
0
| if (Thread.interrupted()) { throw new InterruptedException(); }
|
|
357 |
6
| if (!_continueMonitor.isSignaled()) {
|
|
358 |
1
| _results.put(new PausedResult());
|
|
359 |
1
| _continueMonitor.ensureSignaled();
|
|
360 |
1
| _results.put(new StartedResult());
|
|
361 |
| } |
|
362 |
| } |
|
363 |
| |
|
364 |
1
| public static void main(String... args) {
|
|
365 |
1
| OutputStream out = System.out;
|
|
366 |
1
| IOUtil.attemptClose(System.err);
|
|
367 |
1
| IOUtil.ignoreSystemOut();
|
|
368 |
1
| IOUtil.ignoreSystemErr();
|
|
369 |
1
| try {
|
|
370 |
1
| out.write(PREFIX);
|
|
371 |
1
| out.flush();
|
|
372 |
1
| ObjectOutputStream objOut = new ObjectOutputStream(out);
|
|
373 |
1
| try {
|
|
374 |
1
| objOut.writeObject(new StartedResult());
|
|
375 |
1
| objOut.flush();
|
|
376 |
1
| ObjectInputStream objIn = new ObjectInputStream(System.in);
|
|
377 |
1
| try {
|
|
378 |
1
| IncrementalTask<?, ?> task = (IncrementalTask<?, ?>) objIn.readObject();
|
|
379 |
1
| Runner runner = new Runner(task, objOut, objIn);
|
|
380 |
1
| runner.run();
|
|
381 |
| } |
|
382 |
1
| finally { objIn.close(); }
|
|
383 |
| } |
|
384 |
0
| catch (RuntimeException e) { objOut.writeObject(new ImplementationExceptionResult(e)); }
|
|
385 |
0
| catch (Throwable t) { objOut.writeObject(new ImplementationExceptionResult(t)); }
|
|
386 |
1
| finally { objOut.close(); }
|
|
387 |
| } |
|
388 |
0
| catch (IOException e) { error.log("Error writing to System.out", e); }
|
|
389 |
| } |
|
390 |
| |
|
391 |
| } |
|
392 |
| |
|
393 |
| } |