diff options
author | Alon Zakai <alonzakai@gmail.com> | 2017-06-07 13:38:53 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-07 13:38:53 -0700 |
commit | df58435d385ddf49c501eafd4e324ebe0caa8cbb (patch) | |
tree | 35c96b18df0aafa8ea1e81651d69411b47b95c6e /src | |
parent | 2c220f5cebd915447e786f0b365b0bac1e2f719f (diff) | |
download | binaryen-df58435d385ddf49c501eafd4e324ebe0caa8cbb.tar.gz binaryen-df58435d385ddf49c501eafd4e324ebe0caa8cbb.tar.bz2 binaryen-df58435d385ddf49c501eafd4e324ebe0caa8cbb.zip |
C API fixes (#1042)
* fix mutex usage in C API, and some minor cleanup around it too
* update testcase that was forgotten to be updated before
Diffstat (limited to 'src')
-rw-r--r-- | src/binaryen-c.cpp | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index a824d77f7..842d96f69 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -60,6 +60,13 @@ Literal fromBinaryenLiteral(BinaryenLiteral x) { } } +// Mutexes (global for now; in theory if multiple modules +// are used at once this should be optimized to be per- +// module, but likely it doesn't matter) + +static std::mutex BinaryenFunctionMutex; +static std::mutex BinaryenFunctionTypeMutex; + // Tracing support static int tracing = 0; @@ -137,9 +144,8 @@ BinaryenFunctionTypeRef BinaryenAddFunctionType(BinaryenModuleRef module, const // Lock. This can be called from multiple threads at once, and is a // point where they all access and modify the module. - static std::mutex BinaryenAddFunctionTypeMutex; { - std::lock_guard<std::mutex> lock(BinaryenAddFunctionTypeMutex); + std::lock_guard<std::mutex> lock(BinaryenFunctionTypeMutex); wasm->addFunctionType(ret); } @@ -728,9 +734,8 @@ BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module, const char* na // Lock. This can be called from multiple threads at once, and is a // point where they all access and modify the module. - static std::mutex BinaryenAddFunctionMutex; { - std::lock_guard<std::mutex> lock(BinaryenAddFunctionMutex); + std::lock_guard<std::mutex> lock(BinaryenFunctionMutex); wasm->addFunction(ret); } @@ -1104,27 +1109,23 @@ BinaryenFunctionTypeRef BinaryenGetFunctionTypeBySignature(BinaryenModuleRef mod } auto* wasm = (Module*)module; - auto* test = new FunctionType; - test->result = WasmType(result); + FunctionType test; + test.result = WasmType(result); for (BinaryenIndex i = 0; i < numParams; i++) { - test->params.push_back(WasmType(paramTypes[i])); + test.params.push_back(WasmType(paramTypes[i])); } - // Lock. This can be called from multiple threads at once, and is a - // point where they all access and modify the module. - static std::mutex BinaryenAddFunctionTypeMutex; + // Lock. Guard against reading the list while types are being added. { - std::lock_guard<std::mutex> lock(BinaryenAddFunctionTypeMutex); + std::lock_guard<std::mutex> lock(BinaryenFunctionTypeMutex); for (BinaryenIndex i = 0; i < wasm->functionTypes.size(); i++) { - FunctionType* current = wasm->functionTypes[i].get(); - if (current->structuralComparison(*test)) { - delete test; - return current; + FunctionType* curr = wasm->functionTypes[i].get(); + if (curr->structuralComparison(test)) { + return curr; } } } - delete test; return NULL; } |