Libraryless. Click here for Pure Java version (6077L/33K).
static Set<Thread> evalWithTimeout_inTime = synchroSet();
static Set<Thread> evalWithTimeout_allThreads = newWeakHashSet();
static new ThreadLocal<S> evalWithTimeout_threadName;
static <A> Either<A, Thread> evalWithTimeout(double timeoutSeconds, IF0<A> r) {
ret (Either) evalWithTimeout(timeoutSeconds, (O) r);
}
// Either calculation result or Thread with timed-out computation
// Tries to interrupt thread after timeout
// Errors are rethrown.
static Either<O, Thread> evalWithTimeout(int timeoutMS, O r) {
ret evalWithTimeout(toSeconds(timeoutMS), r);
}
// timeoutSeconds may be infinity()
static Either<O, Thread> evalWithTimeout(double timeoutSeconds, O r) {
final new Flag done;
final new Flag doneWaiting;
final new Var var;
final new Var<Throwable> error;
// copy redirected print handler to new thread
var printer = print_byThread()!;
Thread t = newThread(getAndClearThreadLocal(evalWithTimeout_threadName), r {
try {
temp tempSetTL(print_byThread(), printer);
try {
var.set(callF(r));
} finally {
evalWithTimeout_allThreads.remove(currentThread());
}
} catch e {
error.set(e);
if (doneWaiting.isUp())
printStackTrace_inPossiblyCancelledThread(e);
} finally {
done.raise();
}
});
beforeDelegatingToThread(t);
try {
startThread(t);
evalWithTimeout_inTime.add(t);
evalWithTimeout_allThreads.add(t);
try {
done.waitUntilUp(timeoutSeconds);
doneWaiting.raise();
} finally {
evalWithTimeout_inTime.remove(t);
}
// timeout! cancel/interrupt and return thread object
if (!done.isUp()) {
print("Cancelling thread (timeout)");
cancelAndInterruptThread(t);
ret either2(t);
}
// thread ended with error
if (error! != null) rethrow(error!);
// thread ended naturally
ret either1(var!);
} finally {
afterDelegatingToThread(t);
}
}download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1009329 |
| Snippet name: | evalWithTimeout |
| Eternal ID of this version: | #1009329/31 |
| Text MD5: | 01a22a0ed1af1dc1a3602a509fa5c3dc |
| Transpilation MD5: | 1ce81210b409401124570312331bf7bb |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-03-25 22:48:43 |
| Source code size: | 2042 bytes / 73 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 1224 / 1461 |
| Version history: | 30 change(s) |
| Referenced in: | [show references] |