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

74
LINES

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

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

Libraryless. Click here for Pure Java version (4506L/25K).

ifdef PingV3

sbool ping(PingSource source default pingSource()) {
  ping_v3(source);
  true;
}
  
// don't think this is of any use anymore
sbool ping_impl(bool okInCleanUp) { ret ping(); }

//sbool ping_okInCleanUp() { ret ping(); }

// phase this out too!
static volatile bool ping_pauseAll;

endifdef
ifndef PingV3

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() {
  //ifdef useNewPing
  newPing();
  //endifdef
  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
endifndef

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, 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/42
Text MD5: 48d65ae3638970738b8f8e5af4ba3e62
Transpilation MD5: 7d90aedc72a2adb720757513a9ea14f1
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-27 07:37:40
Source code size: 1949 bytes / 74 lines
Pitched / IR pitched: No / No
Views / Downloads: 940 / 1374
Version history: 41 change(s)
Referenced in: [show references]