diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-06-26 13:39:31 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-26 13:39:31 -0700 |
commit | 29afb7a67fafb7b6325d5e8f60d37be0f65b0cb0 (patch) | |
tree | d07066424c58d0dbb672b80d8e5b3735620465cc | |
parent | 45b358706c86415c5982f9e777fa9e19a33b27a3 (diff) | |
download | binaryen-29afb7a67fafb7b6325d5e8f60d37be0f65b0cb0.tar.gz binaryen-29afb7a67fafb7b6325d5e8f60d37be0f65b0cb0.tar.bz2 binaryen-29afb7a67fafb7b6325d5e8f60d37be0f65b0cb0.zip |
make creating functions and types parallelizable in c api (#603)
-rw-r--r-- | src/binaryen-c.cpp | 22 | ||||
-rw-r--r-- | src/binaryen-c.h | 15 |
2 files changed, 35 insertions, 2 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index a9a22ab34..ff8fae06e 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -18,6 +18,8 @@ // Binaryen C API implementation //=============================== +#include <mutex> + #include "binaryen-c.h" #include "pass.h" #include "wasm.h" @@ -87,7 +89,15 @@ BinaryenFunctionTypeRef BinaryenAddFunctionType(BinaryenModuleRef module, const for (BinaryenIndex i = 0; i < numParams; i++) { ret->params.push_back(WasmType(paramTypes[i])); } - wasm->addFunctionType(ret); + + // 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); + wasm->addFunctionType(ret); + } + return ret; } @@ -385,7 +395,15 @@ BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module, const char* na ret->vars.push_back(WasmType(localTypes[i])); } ret->body = (Expression*)body; - wasm->addFunction(ret); + + // 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); + wasm->addFunction(ret); + } + return ret; } diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 7403d0e81..1421325d9 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -23,6 +23,19 @@ // // The third part of the API lets you provide a general control-flow // graph (CFG) as input. +// +// --------------- +// +// Thread safety: You can create Expressions in parallel, as they do not +// refer to global state. BinaryenAddFunction and +// BinaryenAddFunctionType are also thread-safe, which means +// that you can create functions and their contents in multiple +// threads. This is important since functions are where the +// majority of the work is done. +// Other methods - creating imports, exports, etc. - are +// not currently thread-safe (as there is typically no need +// to parallelize them). +// //================ #ifndef binaryen_h @@ -80,6 +93,7 @@ void BinaryenModuleDispose(BinaryenModuleRef module); typedef void* BinaryenFunctionTypeRef; +// Add a new function type. This is thread-safe. // Note: name can be NULL, in which case we auto-generate a name BinaryenFunctionTypeRef BinaryenAddFunctionType(BinaryenModuleRef module, const char* name, BinaryenType result, BinaryenType* paramTypes, BinaryenIndex numParams); @@ -295,6 +309,7 @@ void BinaryenExpressionPrint(BinaryenExpressionRef expr); typedef void* BinaryenFunctionRef; +// Adds a function to the module. This is thread-safe. BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module, const char* name, BinaryenFunctionTypeRef type, BinaryenType* localTypes, BinaryenIndex numLocalTypes, BinaryenExpressionRef body); // Imports |