Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

73
LINES

< > BotCompany Repo | #1009329 // evalWithTimeout

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (6077L/33K).

1  
static Set<Thread> evalWithTimeout_inTime = synchroSet();
2  
static Set<Thread> evalWithTimeout_allThreads = newWeakHashSet();
3  
4  
static new ThreadLocal<S> evalWithTimeout_threadName;
5  
6  
static <A> Either<A, Thread> evalWithTimeout(double timeoutSeconds, IF0<A> r) {
7  
  ret (Either) evalWithTimeout(timeoutSeconds, (O) r);
8  
}
9  
10  
// Either calculation result or Thread with timed-out computation
11  
// Tries to interrupt thread after timeout
12  
// Errors are rethrown.
13  
static Either<O, Thread> evalWithTimeout(int timeoutMS, O r) {
14  
  ret evalWithTimeout(toSeconds(timeoutMS), r);
15  
}
16  
17  
// timeoutSeconds may be infinity()
18  
static Either<O, Thread> evalWithTimeout(double timeoutSeconds, O r) {
19  
  final new Flag done;
20  
  final new Flag doneWaiting;
21  
  final new Var var;
22  
  final new Var<Throwable> error;
23  
  
24  
  // copy redirected print handler to new thread
25  
  var printer = print_byThread()!;
26  
  
27  
  Thread t = newThread(getAndClearThreadLocal(evalWithTimeout_threadName), r {
28  
    try {
29  
      temp tempSetTL(print_byThread(), printer);
30  
      
31  
      try {
32  
        var.set(callF(r));
33  
      } finally {
34  
        evalWithTimeout_allThreads.remove(currentThread());
35  
      }
36  
    } catch e {
37  
      error.set(e);
38  
      if (doneWaiting.isUp())
39  
        printStackTrace_inPossiblyCancelledThread(e);
40  
    } finally {
41  
      done.raise();
42  
    }
43  
  });
44  
  
45  
  beforeDelegatingToThread(t);
46  
  try {
47  
    startThread(t);
48  
    
49  
    evalWithTimeout_inTime.add(t);
50  
    evalWithTimeout_allThreads.add(t);
51  
    try {
52  
      done.waitUntilUp(timeoutSeconds);
53  
      doneWaiting.raise();
54  
    } finally {
55  
      evalWithTimeout_inTime.remove(t);
56  
    }
57  
      
58  
    // timeout! cancel/interrupt and return thread object
59  
    if (!done.isUp()) {
60  
      print("Cancelling thread (timeout)");
61  
      cancelAndInterruptThread(t);
62  
      ret either2(t);
63  
    }
64  
    
65  
    // thread ended with error
66  
    if (error! != null) rethrow(error!);
67  
    
68  
    // thread ended naturally
69  
    ret either1(var!);
70  
  } finally {
71  
    afterDelegatingToThread(t);
72  
  }
73  
}

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: 566 / 693
Version history: 30 change(s)
Referenced in: [show references]