/
EntryPoint.hx
145 lines (132 loc) · 2.99 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
package haxe;
#if target.threaded
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 sleepLock = new Lock();
static var mutex = new Mutex();
#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
sleepLock.release();
#end
}
public static function runInMainThread(f:Void->Void) {
#if sys
mutex.acquire();
pending.push(f);
mutex.release();
wakeup();
#else
pending.push(f);
#end
}
public static function addThread(f:Void->Void) {
#if sys
mutex.acquire();
threadCount++;
mutex.release();
Thread.create(function() {
f();
mutex.acquire();
threadCount--;
if (threadCount == 0)
wakeup();
mutex.release();
});
#else
threadCount++;
pending.push(function() {
f();
threadCount--;
});
#end
}
static function processEvents():Float {
// 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;
}
/**
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 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
}
}