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).

transient sclass RSTOverQ implements Runnable {
  Runnable runnable;
  Q q = startQ();
  F0<AutoCloseable> enter; // optional ownership marker, e.g. for DynModules
  bool trigger, running;

  *(Runnable *runnable) {}
  *(Runnable *runnable, Q *q)  { assertNotNull(q); }
  *(Q q, Runnable runnable) { this(runnable, q); }

  void trigger() { go(); }
  synchronized void go() {
    if (trigger) ret;
    trigger = true;
    q.add(r _run);
  }
  public void run() { go(); }
  void get() { go(); } // so you can use the ! syntax
  
  synchronized bool running() { ret running; }
  
  void _run() {
    synchronized(this) {
      if (!trigger)
        ret;
      running = true;
      trigger = false;
    }

    try {
      temp callF(enter);
      pcallF(runnable);
    } finally {
      synchronized(this) {
        running = false;
      }
    }
  }
  
  synchronized bool triggered() { ret trigger; }
  
  void waitUntilDone {
    while (running()) sleep(1);
  }
  
  S stats() { ret renderVars(+trigger, +running, +q, +runnable); }
}

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: 169 / 493
Version history: 10 change(s)
Referenced in: [show references]