/
EntryPoint.hx
182 lines (169 loc) · 3.91 KB
/
EntryPoint.hx
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package haxe;
#if (target.threaded && !cppia)
import sys.thread.Lock;
import sys.thread.Mutex;
import sys.thread.Thread;
#elseif sys
private class Lock {
public function new() {}
public inline function release() {}
public inline function wait(?t:Float) {}
}
private class Mutex {
public function new() {}
public inline function acquire() {}
public inline function release() {}
}
private class Thread {
public static function create(f:Void->Void) {
f();
}
}
#end
/**
If `haxe.MainLoop` is kept from DCE, then we will insert an `haxe.EntryPoint.run()` call just at then end of `main()`.
This class can be redefined by custom frameworks so they can handle their own main loop logic.
**/
class EntryPoint {
#if sys
static var mutex = new Mutex();
#if (target.threaded && !cppia)
static var mainThread:Thread = Thread.current();
#else
static var sleepLock = new Lock();
#end
#end
static var pending = new Array<Void->Void>();
public static var threadCount(default, null):Int = 0;
/**
Wakeup a sleeping `run()`
**/
public static function wakeup() {
#if (sys && !(target.threaded && !cppia))
sleepLock.release();
#end
}
public static function runInMainThread(f:Void->Void) {
#if sys
#if (target.threaded && !cppia)
mainThread.events.run(f);
#else
mutex.acquire();
pending.push(f);
mutex.release();
wakeup();
#end
#else
pending.push(f);
#end
}
public static function addThread(f:Void->Void) {
#if sys
mutex.acquire();
threadCount++;
mutex.release();
#if (target.threaded && !cppia)
mainThread.events.promise();
#end
Thread.create(function() {
f();
mutex.acquire();
threadCount--;
if (threadCount == 0)
wakeup();
mutex.release();
#if (target.threaded && !cppia)
mainThread.events.runPromised(() -> {});
#end
});
#else
threadCount++;
pending.push(function() {
f();
threadCount--;
});
#end
}
static function processEvents():Float {
#if (target.threaded && !cppia)
return -1;
#else
// flush all pending calls
while (true) {
#if sys
mutex.acquire();
var f = pending.shift();
mutex.release();
#else
var f = pending.shift();
#end
if (f == null)
break;
f();
}
var time = @:privateAccess MainLoop.tick();
if (!MainLoop.hasEvents() && threadCount == 0)
return -1;
return time;
#end
}
/**
Start the main loop. Depending on the platform, this can return immediately or will only return when the application exits.
**/
@:keep public static function run() @:privateAccess {
#if js
var nextTick = processEvents();
inline function setTimeoutNextTick() {
if (nextTick >= 0) {
(untyped setTimeout)(run, nextTick * 1000);
}
}
#if nodejs
setTimeoutNextTick();
#else
if(js.Lib.typeof(js.Browser.window) != 'undefined') {
var window:Dynamic = js.Browser.window;
var rqf:Dynamic = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
if(rqf != null) {
rqf(run);
} else {
setTimeoutNextTick();
}
} else {
setTimeoutNextTick();
}
#end
#elseif flash
flash.Lib.current.stage.addEventListener(flash.events.Event.ENTER_FRAME, function(_) processEvents());
#elseif (target.threaded && !cppia)
//everything is delegated to sys.thread.EventLoop
#elseif lua
inline function luvRun(mode:String):Bool
return untyped __lua__('_hx_luv.run({0})', mode);
while (true) {
var nextTick = processEvents();
if(untyped __lua__('_hx_luv.loop_alive()')) {
if(nextTick < 0)
luvRun("once")
else
luvRun("nowait");
} else {
if (nextTick < 0)
break;
if (nextTick > 0)
sleepLock.wait(nextTick);
}
}
#elseif sys
while (true) {
var nextTick = processEvents();
if (nextTick < 0)
break;
if (nextTick > 0)
sleepLock.wait(nextTick); // wait until nextTick or wakeup() call
}
#else
// no implementation available, let's exit immediately
#end
}
}