|
@@ -1,36 +1,28 @@
|
|
package haxe;
|
|
package haxe;
|
|
|
|
|
|
-#if (neko && !macro && !interp)
|
|
|
|
|
|
+#if (target.threaded)
|
|
import sys.thread.Lock;
|
|
import sys.thread.Lock;
|
|
import sys.thread.Mutex;
|
|
import sys.thread.Mutex;
|
|
import sys.thread.Thread;
|
|
import sys.thread.Thread;
|
|
-#elseif cpp
|
|
|
|
-import sys.thread.Lock;
|
|
|
|
-import sys.thread.Mutex;
|
|
|
|
-import sys.thread.Thread;
|
|
|
|
-#elseif java
|
|
|
|
-import java.vm.Lock;
|
|
|
|
-import java.vm.Mutex;
|
|
|
|
-import java.vm.Thread;
|
|
|
|
#elseif sys
|
|
#elseif sys
|
|
private class Lock {
|
|
private class Lock {
|
|
- public function new() {
|
|
|
|
- }
|
|
|
|
- public inline function release() {
|
|
|
|
- }
|
|
|
|
- public inline function wait( ?t : Float ) {
|
|
|
|
- }
|
|
|
|
|
|
+ public function new() {}
|
|
|
|
+
|
|
|
|
+ public inline function release() {}
|
|
|
|
+
|
|
|
|
+ public inline function wait(?t:Float) {}
|
|
}
|
|
}
|
|
|
|
+
|
|
private class Mutex {
|
|
private class Mutex {
|
|
- public function new() {
|
|
|
|
- }
|
|
|
|
- public inline function acquire() {
|
|
|
|
- }
|
|
|
|
- public inline function release() {
|
|
|
|
- }
|
|
|
|
|
|
+ public function new() {}
|
|
|
|
+
|
|
|
|
+ public inline function acquire() {}
|
|
|
|
+
|
|
|
|
+ public inline function release() {}
|
|
}
|
|
}
|
|
|
|
+
|
|
private class Thread {
|
|
private class Thread {
|
|
- public static function create( f : Void -> Void ) {
|
|
|
|
|
|
+ public static function create(f:Void->Void) {
|
|
f();
|
|
f();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -41,14 +33,12 @@ private class Thread {
|
|
This class can be redefined by custom frameworks so they can handle their own main loop logic.
|
|
This class can be redefined by custom frameworks so they can handle their own main loop logic.
|
|
**/
|
|
**/
|
|
class EntryPoint {
|
|
class EntryPoint {
|
|
-
|
|
|
|
#if sys
|
|
#if sys
|
|
static var sleepLock = new Lock();
|
|
static var sleepLock = new Lock();
|
|
static var mutex = new Mutex();
|
|
static var mutex = new Mutex();
|
|
#end
|
|
#end
|
|
static var pending = new Array<Void->Void>();
|
|
static var pending = new Array<Void->Void>();
|
|
-
|
|
|
|
- public static var threadCount(default,null) : Int = 0;
|
|
|
|
|
|
+ public static var threadCount(default, null):Int = 0;
|
|
|
|
|
|
/**
|
|
/**
|
|
Wakeup a sleeping run()
|
|
Wakeup a sleeping run()
|
|
@@ -59,7 +49,7 @@ class EntryPoint {
|
|
#end
|
|
#end
|
|
}
|
|
}
|
|
|
|
|
|
- public static function runInMainThread( f : Void -> Void ) {
|
|
|
|
|
|
+ public static function runInMainThread(f:Void->Void) {
|
|
#if sys
|
|
#if sys
|
|
mutex.acquire();
|
|
mutex.acquire();
|
|
pending.push(f);
|
|
pending.push(f);
|
|
@@ -70,7 +60,7 @@ class EntryPoint {
|
|
#end
|
|
#end
|
|
}
|
|
}
|
|
|
|
|
|
- public static function addThread( f : Void -> Void ) {
|
|
|
|
|
|
+ public static function addThread(f:Void->Void) {
|
|
#if sys
|
|
#if sys
|
|
mutex.acquire();
|
|
mutex.acquire();
|
|
threadCount++;
|
|
threadCount++;
|
|
@@ -79,18 +69,22 @@ class EntryPoint {
|
|
f();
|
|
f();
|
|
mutex.acquire();
|
|
mutex.acquire();
|
|
threadCount--;
|
|
threadCount--;
|
|
- if( threadCount == 0 ) wakeup();
|
|
|
|
|
|
+ if (threadCount == 0)
|
|
|
|
+ wakeup();
|
|
mutex.release();
|
|
mutex.release();
|
|
});
|
|
});
|
|
#else
|
|
#else
|
|
threadCount++;
|
|
threadCount++;
|
|
- pending.push(function() { f(); threadCount--; } );
|
|
|
|
|
|
+ pending.push(function() {
|
|
|
|
+ f();
|
|
|
|
+ threadCount--;
|
|
|
|
+ });
|
|
#end
|
|
#end
|
|
}
|
|
}
|
|
|
|
|
|
- static function processEvents() : Float {
|
|
|
|
|
|
+ static function processEvents():Float {
|
|
// flush all pending calls
|
|
// flush all pending calls
|
|
- while( true ) {
|
|
|
|
|
|
+ while (true) {
|
|
#if sys
|
|
#if sys
|
|
mutex.acquire();
|
|
mutex.acquire();
|
|
var f = pending.shift();
|
|
var f = pending.shift();
|
|
@@ -98,11 +92,12 @@ class EntryPoint {
|
|
#else
|
|
#else
|
|
var f = pending.shift();
|
|
var f = pending.shift();
|
|
#end
|
|
#end
|
|
- if( f == null ) break;
|
|
|
|
|
|
+ if (f == null)
|
|
|
|
+ break;
|
|
f();
|
|
f();
|
|
}
|
|
}
|
|
var time = @:privateAccess MainLoop.tick();
|
|
var time = @:privateAccess MainLoop.tick();
|
|
- if( !MainLoop.hasEvents() && threadCount == 0 )
|
|
|
|
|
|
+ if (!MainLoop.hasEvents() && threadCount == 0)
|
|
return -1;
|
|
return -1;
|
|
return time;
|
|
return time;
|
|
}
|
|
}
|
|
@@ -112,37 +107,29 @@ class EntryPoint {
|
|
**/
|
|
**/
|
|
@:keep public static function run() @:privateAccess {
|
|
@:keep public static function run() @:privateAccess {
|
|
#if js
|
|
#if js
|
|
-
|
|
|
|
var nextTick = processEvents();
|
|
var nextTick = processEvents();
|
|
|
|
|
|
#if nodejs
|
|
#if nodejs
|
|
- if( nextTick < 0 )
|
|
|
|
|
|
+ if (nextTick < 0)
|
|
return;
|
|
return;
|
|
- (untyped setTimeout)(run,nextTick);
|
|
|
|
|
|
+ (untyped setTimeout) (run, nextTick);
|
|
#else
|
|
#else
|
|
- var window : Dynamic = js.Browser.window;
|
|
|
|
- var rqf : Dynamic = window.requestAnimationFrame ||
|
|
|
|
- window.webkitRequestAnimationFrame ||
|
|
|
|
- window.mozRequestAnimationFrame;
|
|
|
|
|
|
+ var window:Dynamic = js.Browser.window;
|
|
|
|
+ var rqf:Dynamic = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
|
|
rqf(run);
|
|
rqf(run);
|
|
#end
|
|
#end
|
|
-
|
|
|
|
#elseif flash
|
|
#elseif flash
|
|
-
|
|
|
|
flash.Lib.current.stage.addEventListener(flash.events.Event.ENTER_FRAME, function(_) processEvents());
|
|
flash.Lib.current.stage.addEventListener(flash.events.Event.ENTER_FRAME, function(_) processEvents());
|
|
-
|
|
|
|
#elseif sys
|
|
#elseif sys
|
|
- while( true ) {
|
|
|
|
|
|
+ while (true) {
|
|
var nextTick = processEvents();
|
|
var nextTick = processEvents();
|
|
- if( nextTick < 0 )
|
|
|
|
|
|
+ if (nextTick < 0)
|
|
break;
|
|
break;
|
|
- if( nextTick > 0 )
|
|
|
|
|
|
+ if (nextTick > 0)
|
|
sleepLock.wait(nextTick); // wait until nextTick or wakeup() call
|
|
sleepLock.wait(nextTick); // wait until nextTick or wakeup() call
|
|
}
|
|
}
|
|
#else
|
|
#else
|
|
-
|
|
|
|
// no implementation available, let's exit immediately
|
|
// no implementation available, let's exit immediately
|
|
-
|
|
|
|
#end
|
|
#end
|
|
}
|
|
}
|
|
}
|
|
}
|