From e9536adc6a7a5f9f90879b61cd5cde68d28d5092 Mon Sep 17 00:00:00 2001 From: Artsiom Trubchyk Date: Fri, 21 Aug 2020 16:09:09 +0300 Subject: [PATCH 1/2] Update Extender.java --- .../java/com/defold/extender/Extender.java | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/com/defold/extender/Extender.java b/server/src/main/java/com/defold/extender/Extender.java index e497ca12..497a7b40 100644 --- a/server/src/main/java/com/defold/extender/Extender.java +++ b/server/src/main/java/com/defold/extender/Extender.java @@ -17,6 +17,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.*; +import java.util.concurrent.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -442,7 +443,7 @@ else if(f.getName().endsWith(".aar")) { return allLibJars; } - private File compileFile(int index, File extDir, File src, Map manifestContext) throws IOException, InterruptedException, ExtenderException { + private File compileFile(int index, File extDir, File src, Map manifestContext, List commands) throws IOException, InterruptedException, ExtenderException { List includes = new ArrayList<>(); includes.add( ExtenderUtil.getRelativePath(jobDirectory, new File(extDir, "include") ) ); includes.add( ExtenderUtil.getRelativePath(jobDirectory, uploadDirectory) ); @@ -457,7 +458,7 @@ private File compileFile(int index, File extDir, File src, Map m context.put("ext", ImmutableMap.of("includes", includes, "frameworks", frameworks, "frameworkPaths", frameworkPaths)); String command = templateExecutor.execute(platformConfig.compileCmd, context); - processExecutor.execute(command); + commands.add(command); return o; } @@ -504,15 +505,44 @@ private void buildExtension(File manifest, Map manifestContext) Arrays.sort(srcFiles, NameFileComparator.NAME_INSENSITIVE_COMPARATOR); List objs = new ArrayList<>(); + List commands = new ArrayList<>(); // Compile C++ source into object files int i = getAndIncreaseNameCount(); for (File src : srcFiles) { - File o = compileFile(i, extDir, src, manifestContext); + File o = compileFile(i, extDir, src, manifestContext, commands); objs.add(ExtenderUtil.getRelativePath(jobDirectory, o)); i++; } + ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + List> callables = new ArrayList<>(); + for (String command : commands) { + callables.add(new Callable() { + @Override + public Void call() throws Exception { + processExecutor.execute(command); + return null; + } + }); + } + List> futures = executor.invokeAll(callables); + try { + for (Future future : futures) { + future.get(); + } + } catch (ExecutionException e) { + if (e.getCause() instanceof IOException) { + throw (IOException)e.getCause(); + } else if (e.getCause() instanceof InterruptedException) { + throw (InterruptedException)e.getCause(); + } else { + throw new ExtenderException(e.getCause().toString()); + } + } finally { + executor.shutdown(); + } + // Create c++ library File lib = null; if (platformConfig.writeLibPattern != null) { From 2c7b7c539eaa9b6ba7b810fc061b77f92a51e9d0 Mon Sep 17 00:00:00 2001 From: Artsiom Trubchyk Date: Fri, 21 Aug 2020 16:09:51 +0300 Subject: [PATCH 2/2] Update ProcessExecutor for thread-safety --- server/src/main/java/com/defold/extender/ProcessExecutor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/java/com/defold/extender/ProcessExecutor.java b/server/src/main/java/com/defold/extender/ProcessExecutor.java index 0df1a744..2f432578 100644 --- a/server/src/main/java/com/defold/extender/ProcessExecutor.java +++ b/server/src/main/java/com/defold/extender/ProcessExecutor.java @@ -8,7 +8,7 @@ import java.util.stream.Collectors; public class ProcessExecutor { - private final StringBuilder output = new StringBuilder(); + private final StringBuffer output = new StringBuffer(); private final HashMap env = new HashMap<>(); private File cwd = null; private boolean DM_DEBUG_COMMANDS = System.getenv("DM_DEBUG_COMMANDS") != null;