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

48
LINES

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

JavaX fragment (include)

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

// always returns true
static bool ping() {
  if (ping_pauseAll ifndef LeanMode || ping_anyActions endifndef) 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;
  }
  
  ifndef LeanMode
  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.");
  }
  endifndef
  
  ret false;
}

download  show line numbers  debug dex   

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

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/26
Text MD5: 183132674398a9c0990a6961d24aec34
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-12-05 13:05:12
Source code size: 1391 bytes / 48 lines
Pitched / IR pitched: No / No
Views / Downloads: 665 / 957
Version history: 25 change(s)
Referenced in: [show references]

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