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

81
LINES

< > BotCompany Repo | #1015711 // Thread Hibernation Spike v2 [dev.]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (7614L/51K).

1  
!7
2  
3  
p-experiment {
4  
  catchHibernationException(r {
5  
    callHibernatable(f serveLater, fg(f print, f reversedString));
6  
  });
7  
  catchHibernationException(r {
8  
    callHibernatable(f serveNow, fg(f print, f reversedString));
9  
  });
10  
}
11  
12  
svoid catchHibernationException(Runnable r) {
13  
  try {
14  
    callF(r);
15  
  } catch (RuntimeException e) {
16  
    Throwable e2 = innerException(e);
17  
    if (!e2 instanceof HibernatingTheThread) throw rethrow(e);
18  
  }
19  
}
20  
21  
sclass HibernationFuture {
22  
  new Flag done;
23  
  O value;
24  
  Runnable continuation;
25  
26  
  void setContinuation(Runnable r) {
27  
    synchronized(done) {
28  
      if (!done.isUp()) ret with continuation = r;
29  
    }
30  
    callF(r);
31  
  }
32  
33  
  void setValue(O value) {
34  
    synchronized(done) {
35  
      if (done.isUp()) fail("setValue called twice");
36  
      this.value = value;
37  
      done.raise();
38  
    }
39  
    callF(continuation);
40  
  }
41  
42  
  O getValue() {
43  
    assertTrue(done.isUp());
44  
    ret value;
45  
  }
46  
}
47  
48  
// f1: func -> A, f2 : func(A) -> B, returns: B
49  
static O callHibernatable(O f1, fO f2) {
50  
  O result;
51  
  try {
52  
    result = callF(f1);
53  
  } catch (RuntimeException e) {
54  
    Throwable e2 = innerException(e);
55  
    if (!e2 instanceof HibernatingTheThread) throw rethrow(e);
56  
    final HibernationFuture f = e2/HibernatingTheThread.future;
57  
    
58  
    final new HibernationFuture future2;
59  
    f.setContinuation(r {
60  
      future2.setValue(callF(f2, f.getValue()))
61  
    });
62  
    throw HibernatingTheThread(future2);
63  
  }
64  
  ret callF(f2, result);
65  
}
66  
67  
sS serveNow() {
68  
  ret "Served now";
69  
}
70  
71  
sS serveLater() {
72  
  final new HibernationFuture f;
73  
  doLater(1000, r { f.setValue("Served late") });
74  
  throw HibernatingTheThread(f);
75  
}
76  
77  
sclass HibernatingTheThread extends QuickException {
78  
  HibernationFuture future;
79  
80  
  *(HibernationFuture *future) {}
81  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015711
Snippet name: Thread Hibernation Spike v2 [dev.]
Eternal ID of this version: #1015711/11
Text MD5: 25dd09cd8862113a6c0c1e0e0625cee2
Transpilation MD5: 6f898c9dea2902f75ece044e64ccdbde
Author: stefan
Category:
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-24 17:32:44
Source code size: 1739 bytes / 81 lines
Pitched / IR pitched: No / No
Views / Downloads: 307 / 803
Version history: 10 change(s)
Referenced in: [show references]