summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLogan Chien <tzuhsiang.chien@gmail.com>2016-08-26 02:13:00 +0800
committerLogan Chien <tzuhsiang.chien@gmail.com>2016-08-26 02:47:00 +0800
commita008d4fd7af3b04a3d351a03d56df7c28998690e (patch)
treee82a2dc300b867ff995b856f1c4d0e503af251b2
parenta08643d788dd025345c63798f2e3ed7ca9b12e06 (diff)
downloadbinaryen-a008d4fd7af3b04a3d351a03d56df7c28998690e.tar.gz
binaryen-a008d4fd7af3b04a3d351a03d56df7c28998690e.tar.bz2
binaryen-a008d4fd7af3b04a3d351a03d56df7c28998690e.zip
Fix asm2wasm dead lock caused by empty modules.
This commit fixes an asm2wasm dead lock when asm2wasm is compiling an empty module, i.e. a module without any functions. Without this commit, worker threads are likely to leave `workerMain()` and decrease `liveWorkers` early. Consequently, `waitUntilAllReady()` will never observe `liveWorkers == numWorkers`.
-rw-r--r--src/wasm-module-building.h10
-rw-r--r--test/empty.asm.js4
-rw-r--r--test/empty.fromasm4
-rw-r--r--test/empty.fromasm.imprecise4
-rw-r--r--test/empty.fromasm.imprecise.no-opts4
-rw-r--r--test/empty.fromasm.no-opts4
6 files changed, 29 insertions, 1 deletions
diff --git a/src/wasm-module-building.h b/src/wasm-module-building.h
index cb72fef3f..ead074991 100644
--- a/src/wasm-module-building.h
+++ b/src/wasm-module-building.h
@@ -86,7 +86,15 @@ class OptimizingIncrementalModuleBuilder {
public:
// numFunctions must be equal to the number of functions allocated, or higher. Knowing
// this bounds helps avoid locking.
- OptimizingIncrementalModuleBuilder(Module* wasm, Index numFunctions) : wasm(wasm), numFunctions(numFunctions), nextFunction(0), finishing(false) {
+ OptimizingIncrementalModuleBuilder(Module* wasm, Index numFunctions)
+ : wasm(wasm), numFunctions(numFunctions), endMarker(nullptr), list(nullptr), nextFunction(0),
+ numWorkers(0), liveWorkers(0), activeWorkers(0), availableFuncs(0), finishedFuncs(0),
+ finishing(false) {
+ if (numFunctions == 0) {
+ // special case: no functions to be optimized. Don't create any threads.
+ return;
+ }
+
// prepare work list
endMarker = new Function();
list = new std::atomic<Function*>[numFunctions];
diff --git a/test/empty.asm.js b/test/empty.asm.js
new file mode 100644
index 000000000..94fa6bb38
--- /dev/null
+++ b/test/empty.asm.js
@@ -0,0 +1,4 @@
+function EmptyModule() {
+ 'use asm';
+ return {};
+}
diff --git a/test/empty.fromasm b/test/empty.fromasm
new file mode 100644
index 000000000..939cbb87c
--- /dev/null
+++ b/test/empty.fromasm
@@ -0,0 +1,4 @@
+(module
+ (memory 256 256)
+ (export "memory" memory)
+)
diff --git a/test/empty.fromasm.imprecise b/test/empty.fromasm.imprecise
new file mode 100644
index 000000000..939cbb87c
--- /dev/null
+++ b/test/empty.fromasm.imprecise
@@ -0,0 +1,4 @@
+(module
+ (memory 256 256)
+ (export "memory" memory)
+)
diff --git a/test/empty.fromasm.imprecise.no-opts b/test/empty.fromasm.imprecise.no-opts
new file mode 100644
index 000000000..939cbb87c
--- /dev/null
+++ b/test/empty.fromasm.imprecise.no-opts
@@ -0,0 +1,4 @@
+(module
+ (memory 256 256)
+ (export "memory" memory)
+)
diff --git a/test/empty.fromasm.no-opts b/test/empty.fromasm.no-opts
new file mode 100644
index 000000000..939cbb87c
--- /dev/null
+++ b/test/empty.fromasm.no-opts
@@ -0,0 +1,4 @@
+(module
+ (memory 256 256)
+ (export "memory" memory)
+)