diff --git a/lime/system/ThreadPool.hx b/lime/system/ThreadPool.hx index 385b7808a..f60ee3c17 100644 --- a/lime/system/ThreadPool.hx +++ b/lime/system/ThreadPool.hx @@ -68,7 +68,7 @@ class ThreadPool { __workIncoming.add (new ThreadPoolMessage (WORK, id, message)); __workQueued++; - if (currentThreads < maxThreads) { + if (currentThreads < maxThreads && currentThreads < (__workQueued - __workCompleted)) { currentThreads++; Thread.create (__doWork); diff --git a/templates/haxe/DefaultAssetLibrary.hx b/templates/haxe/DefaultAssetLibrary.hx index a0ff0a1ae..6dba668c7 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.BackgroundWorker; +import lime.system.ThreadPool; import lime.text.Font; import lime.utils.ByteArray; import lime.utils.UInt8Array; @@ -40,6 +40,8 @@ class DefaultAssetLibrary extends AssetLibrary { public var type (default, null) = new Map (); private var lastModified:Float; + private var loadHandlers:Map; + private var threadPool:ThreadPool; private var timer:Timer; @@ -132,6 +134,24 @@ class DefaultAssetLibrary extends AssetLibrary { } + private function createThreadPool ():Void { + + threadPool = new ThreadPool (0, 2); + threadPool.doWork.add (function (id, getMethod) { + + threadPool.sendComplete (id, getMethod (id)); + + }); + threadPool.onComplete.add (function (id, data) { + + var handler = loadHandlers.get (id); + handler (data); + + }); + + } + + public override function exists (id:String, type:String):Bool { var requestedType = type != null ? cast (type, AssetType) : null; @@ -548,16 +568,15 @@ class DefaultAssetLibrary extends AssetLibrary { #else - var worker = new BackgroundWorker (); - - worker.doWork.add (function (_) { + if (threadPool == null) { - worker.sendComplete (getBytes (id)); + loadHandlers = new Map (); + createThreadPool (); - }); + } - worker.onComplete.add (handler); - worker.run (); + loadHandlers.set (id, handler); + threadPool.queue (id, getBytes); #end @@ -605,16 +624,15 @@ class DefaultAssetLibrary extends AssetLibrary { #else - var worker = new BackgroundWorker (); - - worker.doWork.add (function (_) { + if (threadPool == null) { - worker.sendComplete (getImage (id)); + loadHandlers = new Map (); + createThreadPool (); - }); + } - worker.onComplete.add (handler); - worker.run (); + loadHandlers.set (id, handler); + threadPool.queue (id, getImage); #end