Skip to content

Commit 2d01bf7

Browse files
committed
[XABT] Scan for JCWs for each ABI in parallel.
1 parent 48239a5 commit 2d01bf7

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
using Xamarin.Android.Tools;
1717
using Microsoft.Android.Build.Tasks;
1818
using Java.Interop.Tools.JavaCallableWrappers.Adapters;
19+
using System.Threading.Tasks;
20+
using System.Collections.Concurrent;
1921

2022
namespace Xamarin.Android.Tasks
2123
{
@@ -183,26 +185,36 @@ void Run (bool useMarshalMethods)
183185
}
184186

185187
// Now that "never" never happened, we can proceed knowing that at least the assembly sets are the same for each architecture
186-
var nativeCodeGenStates = new Dictionary<AndroidTargetArch, NativeCodeGenState> ();
187-
bool generateJavaCode = true;
188+
var nativeCodeGenStates = new ConcurrentDictionary<AndroidTargetArch, NativeCodeGenState> ();
188189
NativeCodeGenState? templateCodeGenState = null;
189190

190-
foreach (var kvp in allAssembliesPerArch) {
191+
var firstArch = allAssembliesPerArch.First ().Key;
192+
var generateSucceeded = true;
193+
194+
// Generate Java sources in parallel
195+
Parallel.ForEach (allAssembliesPerArch, (kvp) => {
191196
AndroidTargetArch arch = kvp.Key;
192197
Dictionary<string, ITaskItem> archAssemblies = kvp.Value;
198+
199+
// We only need to generate Java code for one ABI, as the Java code is ABI-agnostic
200+
// Pick the "first" one as the one to generate Java code for
201+
var generateJavaCode = arch == firstArch;
202+
193203
(bool success, NativeCodeGenState? state) = GenerateJavaSourcesAndMaybeClassifyMarshalMethods (arch, archAssemblies, MaybeGetArchAssemblies (userAssembliesPerArch, arch), useMarshalMethods, generateJavaCode);
194204

195-
if (!success) {
196-
return;
197-
}
205+
if (!success)
206+
generateSucceeded = false;
198207

199-
if (generateJavaCode) {
208+
// If this is the first architecture, we need to store the state for later use
209+
if (generateJavaCode)
200210
templateCodeGenState = state;
201-
generateJavaCode = false;
202-
}
203211

204-
nativeCodeGenStates.Add (arch, state);
205-
}
212+
nativeCodeGenStates.TryAdd (arch, state);
213+
});
214+
215+
// If we hit an error generating the Java code, we should bail out now
216+
if (!generateSucceeded)
217+
return;
206218

207219
if (templateCodeGenState == null) {
208220
throw new InvalidOperationException ($"Internal error: no native code generator state defined");

src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Java.Interop.Tools.TypeNameMappings;
1414
using Xamarin.Android.Tools;
1515
using Microsoft.Android.Build.Tasks;
16+
using System.Collections.Concurrent;
1617

1718
namespace Xamarin.Android.Tasks
1819
{
@@ -324,9 +325,9 @@ void AddEnvironment ()
324325
}
325326
}
326327

327-
Dictionary<AndroidTargetArch, NativeCodeGenState>? nativeCodeGenStates = null;
328+
ConcurrentDictionary<AndroidTargetArch, NativeCodeGenState>? nativeCodeGenStates = null;
328329
if (enableMarshalMethods) {
329-
nativeCodeGenStates = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<Dictionary<AndroidTargetArch, NativeCodeGenState>> (
330+
nativeCodeGenStates = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<ConcurrentDictionary<AndroidTargetArch, NativeCodeGenState>> (
330331
ProjectSpecificTaskObjectKey (GenerateJavaStubs.NativeCodeGenStateRegisterTaskKey),
331332
RegisteredTaskObjectLifetime.Build
332333
);

src/Xamarin.Android.Build.Tasks/Utilities/JCWGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.Build.Utilities;
1616
using Mono.Cecil;
1717
using Xamarin.Android.Tools;
18+
using System.Collections.Concurrent;
1819

1920
namespace Xamarin.Android.Tasks;
2021

@@ -220,7 +221,7 @@ static string GetMonoInitSource (string androidSdkPlatform)
220221
return builder.ToString ();
221222
}
222223

223-
public static void EnsureAllArchitecturesAreIdentical (TaskLoggingHelper logger, Dictionary<AndroidTargetArch, NativeCodeGenState> javaStubStates)
224+
public static void EnsureAllArchitecturesAreIdentical (TaskLoggingHelper logger, ConcurrentDictionary<AndroidTargetArch, NativeCodeGenState> javaStubStates)
224225
{
225226
if (javaStubStates.Count <= 1) {
226227
return;

0 commit comments

Comments
 (0)