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

79
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 (7460L/42K).

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

// legacy mode
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;

// ignore pingSource if not PingV3
sbool ping(PingSource pingSource) { ret ping(); }

// always returns true
sbool 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/43
Text MD5: 8a6e1ce2555f50071ef01d82cca44b01
Transpilation MD5: 825a50215f7d3d44f7357c03b9fbc149
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-26 02:01:58
Source code size: 2050 bytes / 79 lines
Pitched / IR pitched: No / No
Views / Downloads: 1078 / 1567
Version history: 42 change(s)
Referenced in: [show references]