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

52
LINES

< > BotCompany Repo | #1003137 - ping - allow pausing all threads & thread-inserted actions

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

Transpiled version (2474L) is out of date.

ifdef LeanMode
// you can change this function to allow interrupting long calculations from the outside. just throw a RuntimeException.
sbool ping() { true; }
sbool ping_impl(bool okInCleanUp) { true; }
endifdef

ifndef LeanMode
//sbool ping_actions_shareable = true;
static volatile bool ping_pauseAll;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile bool ping_anyActions;
static Map<Thread, O> ping_actions = newWeakHashMap();
static new ThreadLocal<Bool> ping_isCleanUpThread;

// always returns true
static bool ping() {
  if (ping_pauseAll || ping_anyActions) ping_impl(true /* XXX */);
  //ifndef LeanMode ping_impl(); endifndef
  true;
}

// returns true when it slept
static bool ping_impl(bool okInCleanUp) ctex {
  if (ping_pauseAll && !isAWTThread()) {
    do
      Thread.sleep(ping_sleep);
    while (ping_pauseAll);
    true;
  }
  
  if (ping_anyActions) { // don't allow sharing ping_actions
    if (!okInCleanUp && !isTrue(ping_isCleanUpThread!))
      failIfUnlicensed();
    O action = null;
    synchronized(ping_actions) {
      if (!ping_actions.isEmpty()) {
        action = ping_actions.get(currentThread());
        if (action instanceof Runnable)
          ping_actions.remove(currentThread());
        if (ping_actions.isEmpty()) ping_anyActions = false;
      }
    }
    
    if (action instanceof Runnable)
      ((Runnable) action).run();
    else if (eq(action, "cancelled"))
      fail("Thread cancelled.");
  }

  ret false;
}
endifndef

download  show line numbers  debug dex   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, xrpafgyirdlv

Comments [hide]

ID Author/Program Comment Date
1292 stefan TODO: check licensed() here too? 2016-08-20 16:51:37

add comment

Snippet ID: #1003137
Snippet name: ping - allow pausing all threads & thread-inserted actions
Eternal ID of this version: #1003137/29
Text MD5: 67fcec485eaa9dc9a7dbe74489f6f03d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-02 11:45:50
Source code size: 1550 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 740 / 1074
Version history: 28 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1003137 & http://1003137.tinybrain.de