blob: 49f89bd078bd3d133bfcda4f4470b21a98a66739 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package NET.worlds.console;
import NET.worlds.core.IniFile;
import NET.worlds.core.Std;
import java.util.Vector;
public class Main {
public static int profile = IniFile.gamma().getIniInt("Profile", 0);
private static Thread mainThread;
private static boolean stopFlag = false;
private static Vector<MainCallback> v = new Vector<MainCallback>();
private static int lastAt = -1;
private Main() {
}
public static void mainLoop() {
for (mainThread = Thread.currentThread(); !stopFlag; Thread.yield()) {
MainCallback cb = getNextCallback();
if (cb != null) {
if (profile != 0) {
int start = Std.getRealTime();
long startBytes = Runtime.getRuntime().freeMemory();
cb.mainCallback();
int dur = Std.getRealTime() - start;
long used = startBytes - Runtime.getRuntime().freeMemory();
if (dur > profile && !(cb instanceof Console)) {
System.out.println("Took " + dur + "ms and " + used + " bytes to call mainCallback " + cb);
}
} else {
cb.mainCallback();
}
}
}
MainCallback cb;
while ((cb = getNextCallback()) != null) {
if (cb instanceof MainTerminalCallback) {
((MainTerminalCallback)cb).terminalCallback();
Thread.yield();
} else {
unregister(cb);
}
}
mainThread = null;
}
public static void end() {
stopFlag = true;
}
public static boolean isMainThread() {
return Thread.currentThread() == mainThread;
}
public static int queueLength() {
return v.size();
}
private static MainCallback getNextCallback() {
synchronized (v) {
int n = v.size();
MainCallback cb;
if (n == 0) {
lastAt = -1;
cb = null;
} else {
if (++lastAt >= n) {
lastAt = 0;
}
cb = v.elementAt(lastAt);
}
return cb;
}
}
public static void register(MainCallback x) {
v.addElement(x);
}
public static void unregister(MainCallback x) {
synchronized (v) {
int i = v.indexOf(x);
v.removeElementAt(i);
if (lastAt >= i) {
lastAt--;
}
}
}
}
|