(String tag, Supplier<K> in, Consumer<V> out, Callable<V> r, Consumer<Throwable> h)
| 220 | |
| 221 | |
| 222 | public <K, V> Fiber<K, V> run(String tag, Supplier<K> in, Consumer<V> out, Callable<V> r, Consumer<Throwable> h) throws InterruptedException { |
| 223 | Fiber<K, V> f = new Fiber<>(); |
| 224 | live.add(f); |
| 225 | f.debugDescription = tag; |
| 226 | f.handler = h; |
| 227 | f.in = in; |
| 228 | f.input.put(in.get()); |
| 229 | f.out = out; |
| 230 | threadingModel.set(this); |
| 231 | ClassLoader loader = Thread.currentThread() |
| 232 | .getContextClassLoader(); |
| 233 | |
| 234 | f.runner = executor.submit(() -> { |
| 235 | |
| 236 | try { |
| 237 | Thread.currentThread() |
| 238 | .setName("" + f.debugDescription); |
| 239 | Thread.currentThread() |
| 240 | .setContextClassLoader(loader); |
| 241 | |
| 242 | fiber.set(f); |
| 243 | f.input.take(); |
| 244 | |
| 245 | Object a = r.call(); |
| 246 | if (a == null) f.output.put(NULL); |
| 247 | else f.output.put(a == null ? NULL : a); |
| 248 | |
| 249 | return a; |
| 250 | } catch (Stop s) { |
| 251 | f.runner.cancel(true); |
| 252 | f.output.put(NULL); |
| 253 | return null; |
| 254 | } catch (Throwable t) { |
| 255 | f.exception = t; |
| 256 | f.runner.cancel(true); |
| 257 | System.err.println(" -- caught throwable "); |
| 258 | f.output.put(NULL); |
| 259 | t.printStackTrace(); |
| 260 | System.err.println(" -- rethrowing <" + f.output.peek() + "> -> " + f.handler); |
| 261 | if (f.output.peek() == null) f.output.put(NULL); |
| 262 | throw t; |
| 263 | } finally { |
| 264 | // need to unwide sub fibers to allow recursion. |
| 265 | if (models.containsKey(Thread.currentThread())) { |
| 266 | ThreadSync m = ThreadSync.get(); |
| 267 | m.shutdown(); |
| 268 | } |
| 269 | fiber.set(null); |
| 270 | } |
| 271 | }); |
| 272 | currentFiber = f; |
| 273 | try { |
| 274 | |
| 275 | Object o = debugTake(f); |
| 276 | if (f.runner.isDone()) live.remove(this); |
| 277 | if (o == NULL) o = null; |
| 278 | out.accept((V) o); |
| 279 | f.lastReturn = o == NULL ? null : (V) o; |
no test coverage detected