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).

1  
ifdef PingV3
2  
3  
sbool ping(PingSource source default pingSource()) {
4  
  ping_v3(source);
5  
  true;
6  
}
7  
  
8  
// don't think this is of any use anymore
9  
sbool ping_impl(bool okInCleanUp) { ret ping(); }
10  
11  
//sbool ping_okInCleanUp() { ret ping(); }
12  
13  
// phase this out too!
14  
static volatile bool ping_pauseAll;
15  
16  
endifdef
17  
18  
// legacy mode
19  
ifndef PingV3
20  
21  
ifdef LeanMode
22  
// you can change this function to allow interrupting long calculations from the outside. just throw a RuntimeException.
23  
sbool ping() { true; }
24  
sbool ping_impl(bool okInCleanUp) { true; }
25  
endifdef
26  
27  
ifndef LeanMode
28  
//sbool ping_actions_shareable = true;
29  
static volatile bool ping_pauseAll;
30  
static int ping_sleep = 100; // poll pauseAll flag every 100
31  
static volatile bool ping_anyActions;
32  
static Map<Thread, O> ping_actions = newWeakHashMap();
33  
static new ThreadLocal<Bool> ping_isCleanUpThread;
34  
35  
// ignore pingSource if not PingV3
36  
sbool ping(PingSource pingSource) { ret ping(); }
37  
38  
// always returns true
39  
sbool ping() {
40  
  //ifdef useNewPing
41  
  newPing();
42  
  //endifdef
43  
  if (ping_pauseAll || ping_anyActions) ping_impl(true /* XXX */);
44  
  //ifndef LeanMode ping_impl(); endifndef
45  
  true;
46  
}
47  
48  
// returns true when it slept
49  
static bool ping_impl(bool okInCleanUp) ctex {
50  
  if (ping_pauseAll && !isAWTThread()) {
51  
    do
52  
      Thread.sleep(ping_sleep);
53  
    while (ping_pauseAll);
54  
    true;
55  
  }
56  
  
57  
  if (ping_anyActions) { // don't allow sharing ping_actions
58  
    if (!okInCleanUp && !isTrue(ping_isCleanUpThread!))
59  
      failIfUnlicensed();
60  
    O action = null;
61  
    synchronized(ping_actions) {
62  
      if (!ping_actions.isEmpty()) {
63  
        action = ping_actions.get(currentThread());
64  
        if (action instanceof Runnable)
65  
          ping_actions.remove(currentThread());
66  
        if (ping_actions.isEmpty()) ping_anyActions = false;
67  
      }
68  
    }
69  
    
70  
    if (action instanceof Runnable)
71  
      ((Runnable) action).run();
72  
    else if (eq(action, "cancelled"))
73  
      fail("Thread cancelled.");
74  
  }
75  
76  
  ret false;
77  
}
78  
endifndef
79  
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: 1294 / 1827
Version history: 42 change(s)
Referenced in: [show references]