Run formatter.

This commit is contained in:
Joseph Cloutier
2024-08-17 23:47:46 -04:00
parent 9b7c7914bf
commit b3e44ba03d
3 changed files with 75 additions and 32 deletions

View File

@@ -319,7 +319,7 @@ import lime.utils.Log;
@:dox(hide) class FutureWork @:dox(hide) class FutureWork
{ {
private static var threadPool:ThreadPool; private static var threadPool:ThreadPool;
private static var promises:Map<Int, {complete:Dynamic -> Dynamic, error:Dynamic -> Dynamic}>; private static var promises:Map<Int, {complete:Dynamic->Dynamic, error:Dynamic->Dynamic}>;
public static var minThreads(default, set):Int = 0; public static var minThreads(default, set):Int = 0;
public static var maxThreads(default, set):Int = 1; public static var maxThreads(default, set):Int = 1;
@@ -328,7 +328,8 @@ import lime.utils.Log;
@:allow(lime.app.Future) @:allow(lime.app.Future)
private static function run<T>(work:Void->T, promise:Promise<T>):Void private static function run<T>(work:Void->T, promise:Promise<T>):Void
{ {
if(threadPool == null) { if (threadPool == null)
{
threadPool = new ThreadPool(minThreads, maxThreads, MULTI_THREADED); threadPool = new ThreadPool(minThreads, maxThreads, MULTI_THREADED);
threadPool.onComplete.add(threadPool_onComplete); threadPool.onComplete.add(threadPool_onComplete);
threadPool.onError.add(threadPool_onError); threadPool.onError.add(threadPool_onError);

View File

@@ -88,7 +88,7 @@ class ThreadPool extends WorkOutput
frame. See `workIterations` for instructions to improve the accuracy of frame. See `workIterations` for instructions to improve the accuracy of
this estimate. this estimate.
**/ **/
public static var workLoad:Float = 1/2; public static var workLoad:Float = 1 / 2;
/** /**
__Access this only from the main thread.__ __Access this only from the main thread.__
@@ -171,16 +171,19 @@ class ThreadPool extends WorkOutput
Dispatched at most once per job. Dispatched at most once per job.
**/ **/
public var onComplete(default, null) = new Event<Dynamic->Void>(); public var onComplete(default, null) = new Event<Dynamic->Void>();
/** /**
Dispatched on the main thread when `doWork` calls `sendError()`. Dispatched on the main thread when `doWork` calls `sendError()`.
Dispatched at most once per job. Dispatched at most once per job.
**/ **/
public var onError(default, null) = new Event<Dynamic->Void>(); public var onError(default, null) = new Event<Dynamic->Void>();
/** /**
Dispatched on the main thread when `doWork` calls `sendProgress()`. May Dispatched on the main thread when `doWork` calls `sendProgress()`. May
be dispatched any number of times per job. be dispatched any number of times per job.
**/ **/
public var onProgress(default, null) = new Event<Dynamic->Void>(); public var onProgress(default, null) = new Event<Dynamic->Void>();
/** /**
Dispatched on the main thread when a new job begins. Dispatched exactly Dispatched on the main thread when a new job begins. Dispatched exactly
once per job. once per job.
@@ -199,6 +202,7 @@ class ThreadPool extends WorkOutput
@:deprecated("Instead pass the callback to ThreadPool.run().") @:deprecated("Instead pass the callback to ThreadPool.run().")
@:noCompletion @:dox(hide) public var doWork(get, never):PseudoEvent; @:noCompletion @:dox(hide) public var doWork(get, never):PseudoEvent;
private var __doWork:WorkFunction<State->WorkOutput->Void>; private var __doWork:WorkFunction<State->WorkOutput->Void>;
private var __activeJobs:JobList; private var __activeJobs:JobList;
@@ -409,6 +413,7 @@ class ThreadPool extends WorkOutput
**/ **/
private static function __executeThread():Void private static function __executeThread():Void
{ {
// @formatter:off
JSAsync.async({ JSAsync.async({
var output:WorkOutput = #if html5 new WorkOutput(MULTI_THREADED) #else cast(Thread.readMessage(true), WorkOutput) #end; var output:WorkOutput = #if html5 new WorkOutput(MULTI_THREADED) #else cast(Thread.readMessage(true), WorkOutput) #end;
var event:ThreadEvent = null; var event:ThreadEvent = null;
@@ -467,7 +472,7 @@ class ThreadPool extends WorkOutput
// Work is done; wait for more. // Work is done; wait for more.
event = interruption; event = interruption;
} }
else if(Reflect.hasField(interruption, "event")) else if (Reflect.hasField(interruption, "event"))
{ {
// Work on the new job. // Work on the new job.
event = interruption; event = interruption;
@@ -481,6 +486,7 @@ class ThreadPool extends WorkOutput
// Do it all again. // Do it all again.
} }
}); });
// @formatter:on
} }
#end #end
@@ -538,8 +544,7 @@ class ThreadPool extends WorkOutput
// `workLoad / frameRate` is the total time that pools may use per // `workLoad / frameRate` is the total time that pools may use per
// frame. `workPriority / __totalWorkPriority` is this pool's // frame. `workPriority / __totalWorkPriority` is this pool's
// fraction of that total. // fraction of that total.
var maxTimeElapsed:Float = workPriority * workLoad var maxTimeElapsed:Float = workPriority * workLoad / (__totalWorkPriority * Application.current.window.frameRate);
/ (__totalWorkPriority * Application.current.window.frameRate);
var startTime:Float = timestamp(); var startTime:Float = timestamp();
var timeElapsed:Float = 0; var timeElapsed:Float = 0;
@@ -683,33 +688,56 @@ class ThreadPool extends WorkOutput
} }
@:access(lime.system.ThreadPool) @:forward(canceled) @:access(lime.system.ThreadPool) @:forward(canceled)
private abstract PseudoEvent(ThreadPool) from ThreadPool { private abstract PseudoEvent(ThreadPool) from ThreadPool
{
@:noCompletion @:dox(hide) public var __listeners(get, never):Array<Dynamic>; @:noCompletion @:dox(hide) public var __listeners(get, never):Array<Dynamic>;
private inline function get___listeners():Array<Dynamic> { return []; };
@:noCompletion @:dox(hide) public var __repeat(get, never):Array<Bool>;
private inline function get___repeat():Array<Bool> { return []; };
public function add(callback:Dynamic -> Void):Void { private inline function get___listeners():Array<Dynamic>
{
return [];
};
@:noCompletion @:dox(hide) public var __repeat(get, never):Array<Bool>;
private inline function get___repeat():Array<Bool>
{
return [];
};
public function add(callback:Dynamic->Void):Void
{
function callCallback(state:State, output:WorkOutput):Void function callCallback(state:State, output:WorkOutput):Void
{ {
callback(state); callback(state);
} }
#if (lime_threads && html5) #if (lime_threads && html5)
if (this.mode == MULTI_THREADED) if (this.mode == MULTI_THREADED) throw "Unsupported operation; instead pass the callback to ThreadPool's constructor.";
throw "Unsupported operation; instead pass the callback to ThreadPool's constructor.";
else else
this.__doWork = { func: callCallback }; this.__doWork = {func: callCallback};
#else #else
this.__doWork = callCallback; this.__doWork = callCallback;
#end #end
} }
public inline function cancel():Void {} public inline function cancel():Void {}
public inline function dispatch():Void {} public inline function dispatch():Void {}
public inline function has(callback:Dynamic -> Void):Bool { return this.__doWork != null; }
public inline function remove(callback:Dynamic -> Void):Void { this.__doWork = null; } public inline function has(callback:Dynamic->Void):Bool
public inline function removeAll():Void { this.__doWork = null; } {
return this.__doWork != null;
}
public inline function remove(callback:Dynamic->Void):Void
{
this.__doWork = null;
}
public inline function removeAll():Void
{
this.__doWork = null;
}
} }
class JobList class JobList
@@ -853,7 +881,8 @@ class JobList
// Getters & Setters // Getters & Setters
private inline function set___addingWorkPriority(value:Bool):Bool { private inline function set___addingWorkPriority(value:Bool):Bool
{
if (pool != null && __addingWorkPriority != value && ThreadPool.isMainThread()) if (pool != null && __addingWorkPriority != value && ThreadPool.isMainThread())
{ {
if (value) if (value)
@@ -888,17 +917,25 @@ class JobList
that's in use by multiple jobs, the wrong job may be selected or canceled. that's in use by multiple jobs, the wrong job may be selected or canceled.
**/ **/
@:forward @:forward
abstract JobIdentifier(JobIdentifierImpl) from JobIdentifierImpl { abstract JobIdentifier(JobIdentifierImpl) from JobIdentifierImpl
@:from private static inline function fromJob(job:JobData):JobIdentifier { {
@:from private static inline function fromJob(job:JobData):JobIdentifier
{
return ID(job.id); return ID(job.id);
} }
@:from private static inline function fromID(id:Int):JobIdentifier {
@:from private static inline function fromID(id:Int):JobIdentifier
{
return ID(id); return ID(id);
} }
@:from private static inline function fromFunction(doWork:WorkFunction<State->WorkOutput->Void>):JobIdentifier {
@:from private static inline function fromFunction(doWork:WorkFunction<State->WorkOutput->Void>):JobIdentifier
{
return FUNCTION(doWork); return FUNCTION(doWork);
} }
@:from private static inline function fromState(state:State):JobIdentifier {
@:from private static inline function fromState(state:State):JobIdentifier
{
return STATE(state); return STATE(state);
} }
} }

View File

@@ -13,12 +13,10 @@ import neko.vm.Deque;
import neko.vm.Thread; import neko.vm.Thread;
import neko.vm.Tls; import neko.vm.Tls;
#end #end
#if html5 #if html5
import lime._internal.backend.html5.HTML5Thread as Thread; import lime._internal.backend.html5.HTML5Thread as Thread;
import lime._internal.backend.html5.HTML5Thread.Transferable; import lime._internal.backend.html5.HTML5Thread.Transferable;
#end #end
#if macro #if macro
import haxe.macro.Expr; import haxe.macro.Expr;
@@ -54,6 +52,7 @@ class WorkOutput
available on this target, `mode` will always be `SINGLE_THREADED`. available on this target, `mode` will always be `SINGLE_THREADED`.
**/ **/
public var mode(get, never):ThreadMode; public var mode(get, never):ThreadMode;
#if lime_threads #if lime_threads
/** /**
__Set this only via the constructor.__ __Set this only via the constructor.__
@@ -65,6 +64,7 @@ class WorkOutput
Messages sent by active jobs, received by the main thread. Messages sent by active jobs, received by the main thread.
**/ **/
private var __jobOutput:Deque<ThreadEvent> = new Deque(); private var __jobOutput:Deque<ThreadEvent> = new Deque();
/** /**
Thread-local storage. Tracks whether `sendError()` or `sendComplete()` Thread-local storage. Tracks whether `sendError()` or `sendComplete()`
was called by this job. was called by this job.
@@ -77,6 +77,7 @@ class WorkOutput
Will be null in all other cases. Will be null in all other cases.
**/ **/
public var activeJob(get, set):Null<JobData>; public var activeJob(get, set):Null<JobData>;
@:noCompletion private var __activeJob:Tls<JobData> = new Tls(); @:noCompletion private var __activeJob:Tls<JobData> = new Tls();
private inline function new(mode:Null<ThreadMode>) private inline function new(mode:Null<ThreadMode>)
@@ -171,7 +172,8 @@ class WorkOutput
var thread:Thread = Thread.create(executeThread); var thread:Thread = Thread.create(executeThread);
#if html5 #if html5
thread.onMessage.add(function(event:ThreadEvent) { thread.onMessage.add(function(event:ThreadEvent)
{
__jobOutput.add(event); __jobOutput.add(event);
}); });
#end #end
@@ -195,6 +197,7 @@ class WorkOutput
{ {
return __activeJob.value; return __activeJob.value;
} }
private inline function set_activeJob(value:JobData):JobData private inline function set_activeJob(value:JobData):JobData
{ {
return __activeJob.value = value; return __activeJob.value = value;
@@ -261,8 +264,8 @@ abstract WorkFunction<T:haxe.Constraints.Function>(T) from T to T
{ {
switch (self.typeof().follow().toComplexType()) switch (self.typeof().follow().toComplexType())
{ {
case TPath({ sub: "WorkFunction", params: [TPType(t)] }): case TPath({sub: "WorkFunction", params: [TPType(t)]}):
return macro ($self:$t)($a{args}); return macro($self : $t)($a{args});
default: default:
throw "Underlying function type not found."; throw "Underlying function type not found.";
} }
@@ -275,8 +278,8 @@ abstract WorkFunction<T:haxe.Constraints.Function>(T) from T to T
only accepts a single argument, you can pass multiple values as part of an only accepts a single argument, you can pass multiple values as part of an
anonymous structure. (Or an array, or a class.) anonymous structure. (Or an array, or a class.)
// Does not work: too many arguments. // Does not work: too many arguments.
// threadPool.run(doWork, argument0, argument1, argument2); // threadPool.run(doWork, argument0, argument1, argument2);
// Works: all arguments are combined into one `State` object. // Works: all arguments are combined into one `State` object.
threadPool.run(doWork, { arg0: argument0, arg1: argument1, arg2: argument2 }); threadPool.run(doWork, { arg0: argument0, arg1: argument1, arg2: argument2 });
@@ -299,6 +302,7 @@ typedef State = Dynamic;
class JobData class JobData
{ {
private static var nextID:Int = 0; private static var nextID:Int = 0;
/** /**
`JobData` instances will regularly be copied in HTML5, so checking `JobData` instances will regularly be copied in HTML5, so checking
equality won't work. Instead, compare identifiers. equality won't work. Instead, compare identifiers.
@@ -339,6 +343,7 @@ class JobData
} }
#if haxe4 enum #else @:enum #end abstract ThreadEventType(String) #if haxe4 enum #else @:enum #end abstract ThreadEventType(String)
{ {
// Events sent from a worker thread to the main thread // Events sent from a worker thread to the main thread
var COMPLETE = "COMPLETE"; var COMPLETE = "COMPLETE";
@@ -351,7 +356,8 @@ class JobData
var EXIT = "EXIT"; var EXIT = "EXIT";
} }
typedef ThreadEvent = { typedef ThreadEvent =
{
var event:ThreadEventType; var event:ThreadEventType;
@:optional var message:Dynamic; @:optional var message:Dynamic;
@:optional var job:JobData; @:optional var job:JobData;
@@ -379,7 +385,6 @@ class JSAsync
} }
// Define platform-specific types // Define platform-specific types
#if target.threaded #if target.threaded
// Haxe 3 compatibility: "target.threaded" can't go in parentheses. // Haxe 3 compatibility: "target.threaded" can't go in parentheses.
#elseif !(cpp || neko) #elseif !(cpp || neko)