|
19 | 19 | import java.util.LinkedHashSet; |
20 | 20 | import java.util.List; |
21 | 21 | import java.util.Set; |
| 22 | +import java.util.concurrent.ExecutorService; |
| 23 | +import java.util.concurrent.Executors; |
22 | 24 | import java.util.stream.Stream; |
23 | 25 |
|
24 | 26 | import com.semmle.js.extractor.ExtractorConfig.SourceType; |
@@ -173,6 +175,7 @@ public class AutoBuild { |
173 | 175 | private final Path LGTM_SRC, SEMMLE_DIST; |
174 | 176 | private final TypeScriptMode typeScriptMode; |
175 | 177 | private final String defaultEncoding; |
| 178 | + private ExecutorService threadPool; |
176 | 179 |
|
177 | 180 | public AutoBuild() { |
178 | 181 | this.LGTM_SRC = toRealPath(getPathFromEnvVar("LGTM_SRC")); |
@@ -372,8 +375,13 @@ private boolean addPathPattern(Set<Path> patterns, Path base, String pattern) { |
372 | 375 | * Perform extraction. |
373 | 376 | */ |
374 | 377 | public void run() throws IOException { |
375 | | - extractExterns(); |
376 | | - extractSource(); |
| 378 | + threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); |
| 379 | + try { |
| 380 | + extractExterns(); |
| 381 | + extractSource(); |
| 382 | + } finally { |
| 383 | + threadPool.shutdown(); |
| 384 | + } |
377 | 385 | } |
378 | 386 |
|
379 | 387 | /** |
@@ -595,18 +603,32 @@ private SourceType getSourceType() { |
595 | 603 | } |
596 | 604 |
|
597 | 605 | /** |
598 | | - * Extract a single file. |
| 606 | + * Extract a single file using the given extractor and state. |
| 607 | + * |
| 608 | + * If the state is {@code null}, the extraction job will be submitted to the {@link #threadPool}, |
| 609 | + * otherwise extraction will happen on the main thread. |
599 | 610 | */ |
600 | | - protected void extract(FileExtractor extractor, Path file, ExtractorState state) throws IOException { |
| 611 | + protected void extract(FileExtractor extractor, Path file, ExtractorState state) { |
| 612 | + if (state == null) |
| 613 | + threadPool.submit(() -> doExtract(extractor, file, state)); |
| 614 | + else |
| 615 | + doExtract(extractor, file, state); |
| 616 | + } |
| 617 | + |
| 618 | + private void doExtract(FileExtractor extractor, Path file, ExtractorState state) { |
601 | 619 | File f = file.toFile(); |
602 | 620 | if (!f.exists()) { |
603 | 621 | warn("Skipping " + file + ", which does not exist."); |
604 | 622 | return; |
605 | 623 | } |
606 | 624 |
|
607 | | - long start = logBeginProcess("Extracting " + file); |
608 | | - extractor.extract(f, state); |
609 | | - logEndProcess(start); |
| 625 | + try { |
| 626 | + long start = logBeginProcess("Extracting " + file); |
| 627 | + extractor.extract(f, state); |
| 628 | + logEndProcess(start); |
| 629 | + } catch (IOException e) { |
| 630 | + throw new ResourceError("Exception while extracting " + file + ".", e); |
| 631 | + } |
610 | 632 | } |
611 | 633 |
|
612 | 634 | private void warn(String msg) { |
|
0 commit comments