Add single-thread support to Future.ready().

This commit is contained in:
Joseph Cloutier
2022-02-11 16:05:33 -05:00
parent 3426b0e7d7
commit dfe70daf18

View File

@@ -187,17 +187,6 @@ import lime.utils.Log;
**/ **/
public function ready(waitTime:Int = -1):Future<T> public function ready(waitTime:Int = -1):Future<T>
{ {
#if js
if (isComplete || isError)
{
return this;
}
else
{
Log.warn("Cannot block thread in JavaScript");
return this;
}
#else
if (isComplete || isError) if (isComplete || isError)
{ {
return this; return this;
@@ -205,20 +194,34 @@ import lime.utils.Log;
else else
{ {
var time = System.getTimer(); var time = System.getTimer();
var prevTime = time;
var end = time + waitTime; var end = time + waitTime;
while (!isComplete && !isError && time <= end) while (!isComplete && !isError && time <= end)
{
if (FutureWork.threadPool.activeThreads < 1 && @:privateAccess FutureWork.threadPool.__numPendingJobs < 1)
{
Log.error('Cannot block for a Future without a "work" function.');
return this;
}
if (FutureWork.threadPool.mode == SINGLE_THREADED)
{
@:privateAccess FutureWork.threadPool.__update(time - prevTime);
}
else
{ {
#if sys #if sys
Sys.sleep(0.01); Sys.sleep(0.01);
#end #end
}
prevTime = time;
time = System.getTimer(); time = System.getTimer();
} }
return this; return this;
} }
#end
} }
/** /**
@@ -311,6 +314,7 @@ import lime.utils.Log;
#end #end
@:dox(hide) class FutureWork @:dox(hide) class FutureWork
{ {
@:allow(lime.app.Future)
private static var threadPool:ThreadPool; private static var threadPool:ThreadPool;
private static var states:Array<{work:Void->Dynamic, promise:Promise<Dynamic>}>; private static var states:Array<{work:Void->Dynamic, promise:Promise<Dynamic>}>;
public static var minThreads(default, set):Int = 0; public static var minThreads(default, set):Int = 0;