Renamed WorkerThread to BackgroundWorker, improve
This commit is contained in:
171
lime/system/BackgroundWorker.hx
Normal file
171
lime/system/BackgroundWorker.hx
Normal file
@@ -0,0 +1,171 @@
|
||||
package lime.system;
|
||||
|
||||
|
||||
import lime.app.Application;
|
||||
import lime.app.Event;
|
||||
|
||||
#if cpp
|
||||
import cpp.vm.Deque;
|
||||
import cpp.vm.Thread;
|
||||
#elseif neko
|
||||
import neko.vm.Deque;
|
||||
import neko.vm.Thread;
|
||||
#end
|
||||
|
||||
|
||||
class BackgroundWorker {
|
||||
|
||||
|
||||
public var canceled (default, null):Bool;
|
||||
public var doWork = new Event<Dynamic->Void> ();
|
||||
public var onComplete = new Event<Dynamic->Void> ();
|
||||
public var onProgress = new Event<Dynamic->Void> ();
|
||||
|
||||
private var __runMessage:Dynamic;
|
||||
|
||||
#if (cpp || neko)
|
||||
private var __messageQueue:Deque<Dynamic>;
|
||||
private var __workerThread:Thread;
|
||||
#end
|
||||
|
||||
|
||||
public function new () {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function cancel ():Void {
|
||||
|
||||
canceled = true;
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
__workerThread = null;
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function run (message:Dynamic = null):Void {
|
||||
|
||||
canceled = false;
|
||||
__runMessage = message;
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
__messageQueue = new Deque<Dynamic> ();
|
||||
__workerThread = Thread.create (__doWork);
|
||||
|
||||
Application.current.onUpdate.add (__update);
|
||||
|
||||
#else
|
||||
|
||||
__doWork ();
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function sendComplete (message:Dynamic):Void {
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
__messageQueue.add ("__DONE__");
|
||||
__messageQueue.add (message);
|
||||
|
||||
#else
|
||||
|
||||
if (!canceled) {
|
||||
|
||||
canceled = true;
|
||||
onComplete.dispatch (message);
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function sendProgress (message:Dynamic):Void {
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
__messageQueue.add (message);
|
||||
|
||||
#else
|
||||
|
||||
if (!canceled) {
|
||||
|
||||
onProgress.dispatch (message);
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function __doWork ():Void {
|
||||
|
||||
doWork.dispatch (__runMessage);
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
__messageQueue.add ("__DONE__");
|
||||
|
||||
#else
|
||||
|
||||
if (!canceled) {
|
||||
|
||||
canceled = true;
|
||||
onComplete.dispatch (null);
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function __update (deltaTime:Int):Void {
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
var message = __messageQueue.pop (false);
|
||||
|
||||
if (message != null) {
|
||||
|
||||
if (message != "__DONE__") {
|
||||
|
||||
if (!canceled) {
|
||||
|
||||
onProgress.dispatch (message);
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
Application.current.onUpdate.remove (__update);
|
||||
|
||||
if (!canceled) {
|
||||
|
||||
canceled = true;
|
||||
onComplete.dispatch (__messageQueue.pop (false));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
package lime.system;
|
||||
|
||||
|
||||
import lime.app.Application;
|
||||
import lime.app.Event;
|
||||
|
||||
#if cpp
|
||||
import cpp.vm.Deque;
|
||||
import cpp.vm.Mutex;
|
||||
import cpp.vm.Thread;
|
||||
#elseif neko
|
||||
import neko.vm.Deque;
|
||||
import neko.vm.Mutex;
|
||||
import neko.vm.Thread;
|
||||
#end
|
||||
|
||||
|
||||
class WorkerThread {
|
||||
|
||||
|
||||
public var active (default, null):Bool;
|
||||
public var doWork:Void->Void;
|
||||
public var onComplete = new Event<Void->Void> ();
|
||||
public var onUpdate = new Event<Dynamic->Void> ();
|
||||
|
||||
#if (cpp || neko)
|
||||
private var messageQueue:Deque<Dynamic>;
|
||||
private var mutex:Mutex;
|
||||
private var workerThread:Thread;
|
||||
#end
|
||||
|
||||
|
||||
public function new () {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function run ():Void {
|
||||
|
||||
active = true;
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
messageQueue = new Deque<Dynamic> ();
|
||||
mutex = new Mutex ();
|
||||
workerThread = Thread.create (__doWork);
|
||||
|
||||
Application.current.onUpdate.add (__update);
|
||||
|
||||
#else
|
||||
|
||||
__doWork ();
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function sendUpdate (message:Dynamic):Void {
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
messageQueue.add (message);
|
||||
|
||||
#else
|
||||
|
||||
if (active) {
|
||||
|
||||
onUpdate.dispatch (message);
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function stop ():Void {
|
||||
|
||||
active = false;
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
workerThread = null;
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function __doWork ():Void {
|
||||
|
||||
if (doWork != null) {
|
||||
|
||||
doWork ();
|
||||
|
||||
}
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
messageQueue.add ("__DONE");
|
||||
|
||||
#else
|
||||
|
||||
active = false;
|
||||
onComplete.dispatch ();
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function __update (deltaTime:Int):Void {
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
var message = messageQueue.pop (false);
|
||||
|
||||
if (message != null) {
|
||||
|
||||
if (message != "__DONE") {
|
||||
|
||||
if (active) {
|
||||
|
||||
onUpdate.dispatch (message);
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
Application.current.onUpdate.remove (__update);
|
||||
|
||||
if (active) {
|
||||
|
||||
active = false;
|
||||
onComplete.dispatch ();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import lime.audio.AudioSource;
|
||||
import lime.audio.openal.AL;
|
||||
import lime.audio.AudioBuffer;
|
||||
import lime.graphics.Image;
|
||||
import lime.system.WorkerThread;
|
||||
import lime.system.BackgroundWorker;
|
||||
import lime.text.Font;
|
||||
import lime.utils.ByteArray;
|
||||
import lime.utils.UInt8Array;
|
||||
@@ -548,14 +548,15 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
#else
|
||||
|
||||
var worker = new WorkerThread ();
|
||||
worker.doWork = function () {
|
||||
var worker = new BackgroundWorker ();
|
||||
|
||||
var bytes = getBytes (id);
|
||||
worker.sendUpdate (bytes);
|
||||
worker.doWork.add (function (_) {
|
||||
|
||||
}
|
||||
worker.onUpdate.add (function (msg) handler (msg));
|
||||
worker.sendComplete (getBytes (id));
|
||||
|
||||
});
|
||||
|
||||
worker.onComplete.add (function (bytes) handler (bytes));
|
||||
worker.run ();
|
||||
|
||||
#end
|
||||
@@ -604,14 +605,15 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
#else
|
||||
|
||||
var worker = new WorkerThread ();
|
||||
worker.doWork = function () {
|
||||
var worker = new BackgroundWorker ();
|
||||
|
||||
var image = getImage (id);
|
||||
worker.sendUpdate (image);
|
||||
worker.doWork.add (function (_) {
|
||||
|
||||
}
|
||||
worker.onUpdate.add (function (msg) handler (msg));
|
||||
worker.sendComplete (getImage (id));
|
||||
|
||||
});
|
||||
|
||||
worker.onComplete.add (function (image) handler (image));
|
||||
worker.run ();
|
||||
|
||||
#end
|
||||
|
||||
Reference in New Issue
Block a user