Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/ComputeInExecutor.java @ 22350:b1c71f0c1a85
Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Thu, 05 Nov 2015 18:42:36 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
22349:522155ade3bf | 22350:b1c71f0c1a85 |
---|---|
1 /* | |
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. Oracle designates this | |
8 * particular file as subject to the "Classpath" exception as provided | |
9 * by Oracle in the LICENSE file that accompanied this code. | |
10 * | |
11 * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 * version 2 for more details (a copy is included in the LICENSE file that | |
15 * accompanied this code). | |
16 * | |
17 * You should have received a copy of the GNU General Public License version | |
18 * 2 along with this work; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 * | |
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
22 * or visit www.oracle.com if you need additional information or have any | |
23 * questions. | |
24 */ | |
25 package com.oracle.truffle.api.vm; | |
26 | |
27 import java.io.IOException; | |
28 import java.io.InterruptedIOException; | |
29 import java.util.concurrent.Executor; | |
30 | |
31 abstract class ComputeInExecutor<R> implements Runnable { | |
32 private final Executor executor; | |
33 private R result; | |
34 private Throwable exception; | |
35 private boolean started; | |
36 private boolean done; | |
37 | |
38 protected ComputeInExecutor(Executor executor) { | |
39 this.executor = executor; | |
40 } | |
41 | |
42 protected abstract R compute() throws IOException; | |
43 | |
44 public final R get() throws IOException { | |
45 perform(); | |
46 if (executor != null) { | |
47 waitForDone(); | |
48 } | |
49 exceptionCheck(); | |
50 return result; | |
51 } | |
52 | |
53 private void waitForDone() throws InterruptedIOException { | |
54 synchronized (this) { | |
55 while (!done) { | |
56 try { | |
57 wait(); | |
58 } catch (InterruptedException ex) { | |
59 throw new InterruptedIOException(ex.getMessage()); | |
60 } | |
61 } | |
62 } | |
63 } | |
64 | |
65 private void exceptionCheck() throws IOException, RuntimeException { | |
66 if (exception instanceof IOException) { | |
67 throw (IOException) exception; | |
68 } | |
69 if (exception instanceof RuntimeException) { | |
70 throw (RuntimeException) exception; | |
71 } | |
72 if (exception != null) { | |
73 throw new RuntimeException(exception); | |
74 } | |
75 } | |
76 | |
77 public final void perform() throws IOException { | |
78 if (started) { | |
79 return; | |
80 } | |
81 started = true; | |
82 if (executor == null) { | |
83 run(); | |
84 } else { | |
85 executor.execute(this); | |
86 } | |
87 exceptionCheck(); | |
88 } | |
89 | |
90 @Override | |
91 public final void run() { | |
92 try { | |
93 result = compute(); | |
94 } catch (Exception ex) { | |
95 exception = ex; | |
96 } finally { | |
97 if (executor != null) { | |
98 synchronized (this) { | |
99 done = true; | |
100 notifyAll(); | |
101 } | |
102 } else { | |
103 done = true; | |
104 } | |
105 } | |
106 } | |
107 | |
108 @Override | |
109 public final String toString() { | |
110 return "value=" + result + ",exception=" + exception + ",computed=" + done; | |
111 } | |
112 } |