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)

1  
//sbool ping_actions_shareable = true;
2  
static volatile bool ping_pauseAll;
3  
static int ping_sleep = 100; // poll pauseAll flag every 100
4  
ifndef LeanMode
5  
static volatile bool ping_anyActions;
6  
static Map<Thread, O> ping_actions = newWeakHashMap();
7  
static new ThreadLocal<Bool> ping_isCleanUpThread;
8  
endifndef
9  
10  
// always returns true
11  
static bool ping() {
12  
  if (ping_pauseAll ifndef LeanMode || ping_anyActions endifndef) ping_impl(true /* XXX */);
13  
  //ifndef LeanMode ping_impl(); endifndef
14  
  true;
15  
}
16  
17  
// returns true when it slept
18  
static bool ping_impl(bool okInCleanUp) ctex {
19  
  if (ping_pauseAll && !isAWTThread()) {
20  
    do
21  
      Thread.sleep(ping_sleep);
22  
    while (ping_pauseAll);
23  
    true;
24  
  }
25  
  
26  
  ifndef LeanMode
27  
  if (ping_anyActions) { // don't allow sharing ping_actions
28  
    if (!okInCleanUp && !isTrue(ping_isCleanUpThread!))
29  
      failIfUnlicensed();
30  
    O action = null;
31  
    synchronized(ping_actions) {
32  
      if (!ping_actions.isEmpty()) {
33  
        action = ping_actions.get(currentThread());
34  
        if (action instanceof Runnable)
35  
          ping_actions.remove(currentThread());
36  
        if (ping_actions.isEmpty()) ping_anyActions = false;
37  
      }
38  
    }
39  
    
40  
    if (action instanceof Runnable)
41  
      ((Runnable) action).run();
42  
    else if (eq(action, "cancelled"))
43  
      fail("Thread cancelled.");
44  
  }
45  
  endifndef
46  
  
47  
  ret false;
48  
}

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: 676 / 971
Version history: 25 change(s)
Referenced in: [show references]

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