Libraryless. Click here for Pure Java version (7123L/40K).
1 | // In the newest pinging system (with flag PingV3), a ping source |
2 | // is the object that "allows" some code to run. |
3 | // When that code calls ping(), the ping source's action (if defined) |
4 | // is triggered. |
5 | |
6 | // This allows randomly interrupting code execution, for example. |
7 | |
8 | sclass PingSource {
|
9 | // returns true if it slept |
10 | settable volatile IF0<Bool> action; |
11 | |
12 | // optional description of this ping source |
13 | S text; |
14 | |
15 | // optional thread pool that this ping source likes to run in |
16 | ThreadPool threadPool; |
17 | |
18 | *() {}
|
19 | *(ThreadPool *threadPool) {}
|
20 | *(ThreadPool *threadPool, S *text) {}
|
21 | *(IF0<Bool> *action) {}
|
22 | |
23 | // returns true if it slept |
24 | final bool get() {
|
25 | var a = action; |
26 | ret a != null && a!; |
27 | } |
28 | |
29 | final void ping {
|
30 | var a = action; |
31 | if (a != null) a!; |
32 | } |
33 | |
34 | void cancel {
|
35 | action = new Cancelled; |
36 | } |
37 | |
38 | class Cancelled is IF0<Bool> {
|
39 | public Bool get() { throw new PingSourceCancelledException(PingSource.this); }
|
40 | } |
41 | |
42 | record noeq Encapsulated(Runnable r) is Runnable {
|
43 | run {
|
44 | //System.out.println("Encapsulated running: " + r);
|
45 | try {
|
46 | pingSource_tl().set(PingSource.this); |
47 | //System.out.println("Ping source set");
|
48 | ping(); |
49 | r.run(); |
50 | //System.out.println("Done running");
|
51 | } finally {
|
52 | //System.out.println("Finally");
|
53 | pingSource_tl().set(null); |
54 | } |
55 | } |
56 | |
57 | toString { ret PingSource.this + ": " + r; }
|
58 | } |
59 | |
60 | void do(Runnable r) {
|
61 | if (r == null) ret; |
62 | threadPool.acquireThreadOrQueue(new Encapsulated(r)); |
63 | } |
64 | |
65 | toString { S t = text; ret nempty(t) ? t : super.toString(); }
|
66 | |
67 | ISleeper_v2 sleeper() { ret threadPool.sleeper(); }
|
68 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
| Snippet ID: | #1033336 |
| Snippet name: | PingSource |
| Eternal ID of this version: | #1033336/35 |
| Text MD5: | 849a575b25154cabb0f69c1abf276a32 |
| Transpilation MD5: | 06ec60de52763821cc6ca88d76478062 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-01-28 21:40:25 |
| Source code size: | 1746 bytes / 68 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 624 / 929 |
| Version history: | 34 change(s) |
| Referenced in: | [show references] |