Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define PLATFORM and PLATFORM_NUMBER based on the minimum Android SDK Version. #1826

Closed
wants to merge 13 commits into from
Closed
2 changes: 1 addition & 1 deletion src/lime/_internal/backend/html5/HTML5Thread.hx
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ abstract Message(Dynamic) from Dynamic to Dynamic
// Skip `null` for obvious reasons.
return object == null
// No need to preserve a primitive type.
|| !#if (haxe_ver >= 4.2) Std.isOfType #else untyped __js__ #end (object, Object)
|| !#if (haxe_ver >= 4.2) Std.isOfType #else Std.is #end (object, Object)
// Objects with this field have been deliberately excluded.
|| Reflect.field(object, SKIP_FIELD) == true
// A `Uint8Array` (the type used by `haxe.io.Bytes`) can have
Expand Down
6 changes: 0 additions & 6 deletions src/lime/app/Future.hx
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,11 @@ import lime.utils.Log;
var result = bundle.work.dispatch(bundle.state);
if (result != null || bundle.legacyCode)
{
#if (lime_threads && html5)
bundle.work.makePortable();
#end
output.sendComplete(result);
}
}
catch (e:Dynamic)
{
#if (lime_threads && html5)
bundle.work.makePortable();
#end
output.sendError(e);
}
}
Expand Down
50 changes: 31 additions & 19 deletions src/lime/system/ThreadPool.hx
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ class ThreadPool extends WorkOutput
/**
The set of threads actively running a job.
**/
private var __activeThreads:Map<Int, Thread> = new Map();
private var __activeThreads:Map<Int, Thread>;

/**
A list of idle threads. Not to be confused with `idleThreads`, a public
variable equal to `__idleThreads.length`.
**/
private var __idleThreads:List<Thread> = new List();
private var __idleThreads:Array<Thread>;
#end

private var __jobQueue:JobList = new JobList();
Expand All @@ -219,6 +219,14 @@ class ThreadPool extends WorkOutput

this.minThreads = minThreads;
this.maxThreads = maxThreads;

#if lime_threads
if (this.mode == MULTI_THREADED)
{
__activeThreads = new Map();
__idleThreads = [];
}
#end
}

/**
Expand All @@ -245,12 +253,12 @@ class ThreadPool extends WorkOutput
var thread:Thread = __activeThreads[job.id];
if (idleThreads < minThreads)
{
thread.sendMessage(new ThreadEvent(WORK, null, null));
thread.sendMessage({event: CANCEL});
__idleThreads.push(thread);
}
else
{
thread.sendMessage(new ThreadEvent(EXIT, null, null));
thread.sendMessage({event: EXIT});
}
}
#end
Expand All @@ -270,10 +278,10 @@ class ThreadPool extends WorkOutput
__activeJobs.clear();

#if lime_threads
// Cancel idle threads if there are more than the minimum.
// Exit idle threads if there are more than the minimum.
while (idleThreads > minThreads)
{
__idleThreads.pop().sendMessage(new ThreadEvent(EXIT, null, null));
__idleThreads.pop().sendMessage({event: EXIT});
}
#end

Expand Down Expand Up @@ -310,7 +318,7 @@ class ThreadPool extends WorkOutput
var thread:Thread = __activeThreads[data.id];
if (thread != null)
{
thread.sendMessage(new ThreadEvent(WORK, null, null));
thread.sendMessage({event: CANCEL});
__activeThreads.remove(data.id);
__idleThreads.push(thread);
}
Expand Down Expand Up @@ -395,7 +403,7 @@ class ThreadPool extends WorkOutput
{
event = Thread.readMessage(true);
}
while (!#if (haxe_ver >= 4.2) Std.isOfType #else Std.is #end (event, ThreadEvent));
while (event == null || !Reflect.hasField(event, "event"));

output.resetJobProgress();
}
Expand Down Expand Up @@ -438,9 +446,9 @@ class ThreadPool extends WorkOutput
if (interruption == null || output.__jobComplete.value)
{
// Work is done; wait for more.
event = null;
event = interruption;
}
else if(#if (haxe_ver >= 4.2) Std.isOfType #else Std.is #end (interruption, ThreadEvent))
else if(Reflect.hasField(interruption, "event"))
{
// Work on the new job.
event = interruption;
Expand Down Expand Up @@ -492,9 +500,9 @@ class ThreadPool extends WorkOutput
job.doWork.makePortable();
#end

var thread:Thread = __idleThreads.isEmpty() ? createThread(__executeThread) : __idleThreads.pop();
var thread:Thread = __idleThreads.length == 0 ? createThread(__executeThread) : __idleThreads.pop();
__activeThreads[job.id] = thread;
thread.sendMessage(new ThreadEvent(WORK, null, job));
thread.sendMessage({event: WORK, job: job});
}
#end
}
Expand Down Expand Up @@ -539,15 +547,19 @@ class ThreadPool extends WorkOutput
var threadEvent:ThreadEvent;
while ((threadEvent = __jobOutput.pop(false)) != null)
{
if (!__activeJobs.exists(threadEvent.job))
if (threadEvent.jobID != null)
{
// Ignore events from canceled jobs.
continue;
activeJob = __activeJobs.getByID(threadEvent.jobID);
}
else
{
activeJob = threadEvent.job;
}

// Get by ID because in HTML5, the object will have been cloned,
// which will interfere with attempts to test equality.
activeJob = __activeJobs.getByID(threadEvent.job.id);
if (activeJob == null || !__activeJobs.exists(activeJob))
{
continue;
}

if (mode == MULTI_THREADED)
{
Expand Down Expand Up @@ -582,7 +594,7 @@ class ThreadPool extends WorkOutput

if (currentThreads > maxThreads || __jobQueue.length == 0 && currentThreads > minThreads)
{
thread.sendMessage(new ThreadEvent(EXIT, null, null));
thread.sendMessage({event: EXIT});
}
else
{
Expand Down
54 changes: 15 additions & 39 deletions src/lime/system/WorkOutput.hx
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,11 @@ class WorkOutput
#if (lime_threads && html5)
if (mode == MULTI_THREADED)
{
activeJob.doWork.makePortable();
Thread.returnMessage(new ThreadEvent(COMPLETE, message, activeJob), transferList);
Thread.returnMessage({event: COMPLETE, message: message, jobID: activeJob.id}, transferList);
}
else
#end
__jobOutput.add(new ThreadEvent(COMPLETE, message, activeJob));
__jobOutput.add({event: COMPLETE, message: message, jobID: activeJob.id});
}
}

Expand All @@ -130,12 +129,11 @@ class WorkOutput
#if (lime_threads && html5)
if (mode == MULTI_THREADED)
{
activeJob.doWork.makePortable();
Thread.returnMessage(new ThreadEvent(ERROR, message, activeJob), transferList);
Thread.returnMessage({event: ERROR, message: message, jobID: activeJob.id}, transferList);
}
else
#end
__jobOutput.add(new ThreadEvent(ERROR, message, activeJob));
__jobOutput.add({event: ERROR, message: message, jobID: activeJob.id});
}
}

Expand All @@ -153,12 +151,11 @@ class WorkOutput
#if (lime_threads && html5)
if (mode == MULTI_THREADED)
{
activeJob.doWork.makePortable();
Thread.returnMessage(new ThreadEvent(PROGRESS, message, activeJob), transferList);
Thread.returnMessage({event: PROGRESS, message: message, jobID: activeJob.id}, transferList);
}
else
#end
__jobOutput.add(new ThreadEvent(PROGRESS, message, activeJob));
__jobOutput.add({event: PROGRESS, message: message, jobID: activeJob.id});
}
}

Expand Down Expand Up @@ -343,43 +340,22 @@ class JobData

#if haxe4 enum #else @:enum #end abstract ThreadEventType(String)
{
/**
Sent by the background thread, indicating completion.
**/
// Events sent from a worker thread to the main thread
var COMPLETE = "COMPLETE";
/**
Sent by the background thread, indicating failure.
**/
var ERROR = "ERROR";
/**
Sent by the background thread.
**/
var PROGRESS = "PROGRESS";
/**
Sent by the main thread, indicating that the provided job should begin
in place of any ongoing job. If `state == null`, the existing job will
stop and the thread will go idle. (To run a job with no argument, set
`state = {}` instead.)
**/

// Commands sent from the main thread to a worker thread
var WORK = "WORK";
/**
Sent by the main thread to shut down a thread.
**/
var CANCEL = "CANCEL";
var EXIT = "EXIT";
}

class ThreadEvent
{
public var event(default, null):ThreadEventType;
public var message(default, null):State;
public var job(default, null):JobData;

public inline function new(event:ThreadEventType, message:State, job:JobData)
{
this.event = event;
this.message = message;
this.job = job;
}
typedef ThreadEvent = {
var event:ThreadEventType;
@:optional var message:Dynamic;
@:optional var job:JobData;
@:optional var jobID:Int;
}

class JSAsync
Expand Down
24 changes: 13 additions & 11 deletions tools/platforms/AndroidPlatform.hx
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ class AndroidPlatform extends PlatformTarget

for (architecture in architectures)
{
var haxeParams = [hxml, "-D", "android", "-D", "PLATFORM=android-21"];
var cppParams = ["-Dandroid", "-DPLATFORM=android-21"];
var minimumSDKVersion = project.config.getInt("android.minimum-sdk-version", 21);
var haxeParams = [hxml, "-D", "android", "-D", "PLATFORM=android-" + minimumSDKVersion, "-D", "PLATFORM_NUMBER=" + minimumSDKVersion];
var cppParams = ["-Dandroid", "-DPLATFORM=android-" + minimumSDKVersion, "-DPLATFORM_NUMBER=" + minimumSDKVersion];
var path = sourceSet + "/jniLibs/armeabi";
var suffix = ".so";

Expand Down Expand Up @@ -363,21 +364,22 @@ class AndroidPlatform extends PlatformTarget

public override function rebuild():Void
{
var armv5 = (/*command == "rebuild" ||*/
ArrayTools.containsValue(project.architectures, Architecture.ARMV5)
|| ArrayTools.containsValue(project.architectures, Architecture.ARMV6));
var armv5 = ArrayTools.containsValue(project.architectures, Architecture.ARMV5)
|| ArrayTools.containsValue(project.architectures, Architecture.ARMV6);
var armv7 = (command == "rebuild" || ArrayTools.containsValue(project.architectures, Architecture.ARMV7));
var arm64 = (command == "rebuild" || ArrayTools.containsValue(project.architectures, Architecture.ARM64));
var x86 = (command == "rebuild" || ArrayTools.containsValue(project.architectures, Architecture.X86));
var x64 = (/*command == "rebuild" ||*/ ArrayTools.containsValue(project.architectures, Architecture.X64));
var x64 = ArrayTools.containsValue(project.architectures, Architecture.X64);

var commands = [];

if (armv5) commands.push(["-Dandroid", "-DPLATFORM=android-21"]);
if (armv7) commands.push(["-Dandroid", "-DHXCPP_ARMV7", "-DPLATFORM=android-21"]);
if (arm64) commands.push(["-Dandroid", "-DHXCPP_ARM64", "-DPLATFORM=android-21"]);
if (x86) commands.push(["-Dandroid", "-DHXCPP_X86", "-DPLATFORM=android-21"]);
if (x64) commands.push(["-Dandroid", "-DHXCPP_X86_64", "-DPLATFORM=android-21"]);
var minimumSDKVersion = project.config.getInt("android.minimum-sdk-version", 21);

if (armv5) commands.push(["-Dandroid", "-DPLATFORM=android-" + minimumSDKVersion, "-DPLATFORM_NUMBER=" + minimumSDKVersion]);
if (armv7) commands.push(["-Dandroid", "-DHXCPP_ARMV7", "-DPLATFORM=android-" + minimumSDKVersion, "-DPLATFORM_NUMBER=" + minimumSDKVersion]);
if (arm64) commands.push(["-Dandroid", "-DHXCPP_ARM64", "-DPLATFORM=android-" + minimumSDKVersion, "-DPLATFORM_NUMBER=" + minimumSDKVersion]);
if (x86) commands.push(["-Dandroid", "-DHXCPP_X86", "-DPLATFORM=android-" + minimumSDKVersion, "-DPLATFORM_NUMBER=" + minimumSDKVersion]);
if (x64) commands.push(["-Dandroid", "-DHXCPP_X86_64", "-DPLATFORM=android-" + minimumSDKVersion, "-DPLATFORM_NUMBER=" + minimumSDKVersion]);

CPPHelper.rebuild(project, commands);
}
Expand Down
Loading