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

47
LINES

< > BotCompany Repo | #1030787 // RSTOverQ (ReliableSingleThread that shares thread with a Q) [probably works]

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

Libraryless. Click here for Pure Java version (7417L/41K).

1  
transient sclass RSTOverQ implements Runnable {
2  
  Runnable runnable;
3  
  Q q = startQ();
4  
  F0<AutoCloseable> enter; // optional ownership marker, e.g. for DynModules
5  
  bool trigger, running;
6  
7  
  *(Runnable *runnable) {}
8  
  *(Runnable *runnable, Q *q)  { assertNotNull(q); }
9  
  *(Q q, Runnable runnable) { this(runnable, q); }
10  
11  
  void trigger() { go(); }
12  
  synchronized void go() {
13  
    if (trigger) ret;
14  
    trigger = true;
15  
    q.add(r _run);
16  
  }
17  
  public void run() { go(); }
18  
  void get() { go(); } // so you can use the ! syntax
19  
  
20  
  synchronized bool running() { ret running; }
21  
  
22  
  void _run() {
23  
    synchronized(this) {
24  
      if (!trigger)
25  
        ret;
26  
      running = true;
27  
      trigger = false;
28  
    }
29  
30  
    try {
31  
      temp callF(enter);
32  
      pcallF(runnable);
33  
    } finally {
34  
      synchronized(this) {
35  
        running = false;
36  
      }
37  
    }
38  
  }
39  
  
40  
  synchronized bool triggered() { ret trigger; }
41  
  
42  
  void waitUntilDone {
43  
    while (running()) sleep(1);
44  
  }
45  
  
46  
  S stats() { ret renderVars(+trigger, +running, +q, +runnable); }
47  
}

Author comment

Began life as a copy of #1006319

download  show line numbers  debug dex  old transpilations   

Travelled to 5 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030787
Snippet name: RSTOverQ (ReliableSingleThread that shares thread with a Q) [probably works]
Eternal ID of this version: #1030787/11
Text MD5: 5ad14b275de166318de3817fc866d53f
Transpilation MD5: 08be27ad77fb2e546b65338df58d47ba
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-03 16:49:50
Source code size: 1080 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 234 / 590
Version history: 10 change(s)
Referenced in: [show references]