static Set evalWithTimeout_inTime = synchroSet(); static Set evalWithTimeout_allThreads = newWeakHashSet(); static new ThreadLocal evalWithTimeout_threadName; // Either calculation result or Thread with timed-out computation // Tries to interrupt thread after timeout // Errors are rethrown. static Either evalWithTimeout(int timeoutMS, fO r) { final new Flag done; final new Flag doneWaiting; final new Var var; final new Var error; Thread t = newThread(getAndClearThreadLocal(evalWithTimeout_threadName), r { try { try { var.set(callF(r)); } finally { evalWithTimeout_allThreads.remove(currentThread()); } } catch e { error.set(e); if (doneWaiting.isUp()) printStackTrace(e); } finally { done.raise(); } }); beforeDelegatingToThread(t); try { startThread(t); evalWithTimeout_inTime.add(t); evalWithTimeout_allThreads.add(t); try { done.waitUntilUp(timeoutMS); 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); } } static Either evalWithTimeout(double timeoutSeconds, fO r) { ret evalWithTimeout(toMS_int(timeoutSeconds), r); }