From 0882e27537151582f43527f84fc5f991b519a1c7 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Mon, 6 Jul 2015 10:05:58 -0700 Subject: [PATCH] Renamed WorkerThread to BackgroundWorker, improve --- lime/system/BackgroundWorker.hx | 171 ++++++++++++++++++++++++++ lime/system/WorkerThread.hx | 151 ----------------------- templates/haxe/DefaultAssetLibrary.hx | 28 +++-- 3 files changed, 186 insertions(+), 164 deletions(-) create mode 100644 lime/system/BackgroundWorker.hx delete mode 100644 lime/system/WorkerThread.hx diff --git a/lime/system/BackgroundWorker.hx b/lime/system/BackgroundWorker.hx new file mode 100644 index 000000000..fee183da9 --- /dev/null +++ b/lime/system/BackgroundWorker.hx @@ -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 EventVoid> (); + public var onComplete = new EventVoid> (); + public var onProgress = new EventVoid> (); + + private var __runMessage:Dynamic; + + #if (cpp || neko) + private var __messageQueue:Deque; + 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 (); + __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 + + } + + +} \ No newline at end of file diff --git a/lime/system/WorkerThread.hx b/lime/system/WorkerThread.hx deleted file mode 100644 index c857043d1..000000000 --- a/lime/system/WorkerThread.hx +++ /dev/null @@ -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 EventVoid> (); - public var onUpdate = new EventVoid> (); - - #if (cpp || neko) - private var messageQueue:Deque; - private var mutex:Mutex; - private var workerThread:Thread; - #end - - - public function new () { - - - - } - - - public function run ():Void { - - active = true; - - #if (cpp || neko) - - messageQueue = new Deque (); - 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 - - } - - -} \ No newline at end of file diff --git a/templates/haxe/DefaultAssetLibrary.hx b/templates/haxe/DefaultAssetLibrary.hx index 8df4b56fc..3171bdeee 100644 --- a/templates/haxe/DefaultAssetLibrary.hx +++ b/templates/haxe/DefaultAssetLibrary.hx @@ -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 (); + + worker.doWork.add (function (_) { - var bytes = getBytes (id); - worker.sendUpdate (bytes); + worker.sendComplete (getBytes (id)); - } - worker.onUpdate.add (function (msg) handler (msg)); + }); + + 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 (); + + worker.doWork.add (function (_) { - var image = getImage (id); - worker.sendUpdate (image); + worker.sendComplete (getImage (id)); - } - worker.onUpdate.add (function (msg) handler (msg)); + }); + + worker.onComplete.add (function (image) handler (image)); worker.run (); #end