summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2018-01-26 14:32:06 -0800
committerGitHub <noreply@github.com>2018-01-26 14:32:06 -0800
commit6c081144e5bcf31edece0806c38d63c97257cc4c (patch)
tree517b939d2b0abdfe5b9c4908df62ddb9f7206093 /test
parent4ef6655e887e4088b8faf0cf5857fd49edbcd498 (diff)
downloadbinaryen-6c081144e5bcf31edece0806c38d63c97257cc4c.tar.gz
binaryen-6c081144e5bcf31edece0806c38d63c97257cc4c.tar.bz2
binaryen-6c081144e5bcf31edece0806c38d63c97257cc4c.zip
ThreadPool refactoring (#1389)
Refactor ThreadPool code for clarity and to fix some bugs with using the pool from different threads in parallel. We have a singleton pool, and need to ensure it is created only once and used only by one thread at a time. This model is a simple way to ensure we use a number of threads equal to the number of cores, more or less (a pool per Module might lead to number of cores * number of Modules being optimized). This refactoring adds a parent pointer in the worker threads (giving them direct access to the pool makes it simpler to make sure that pool and thread creation and teardown are threadsafe). This commit also adds proper locking around pool creation and pool usage.
Diffstat (limited to 'test')
-rw-r--r--test/example/cpp-threads.cpp58
-rw-r--r--test/example/cpp-threads.txt4
2 files changed, 62 insertions, 0 deletions
diff --git a/test/example/cpp-threads.cpp b/test/example/cpp-threads.cpp
new file mode 100644
index 000000000..4a41249e7
--- /dev/null
+++ b/test/example/cpp-threads.cpp
@@ -0,0 +1,58 @@
+// test multiple uses of the threadPool
+
+#include <iostream>
+#include <thread>
+#include <vector>
+
+#include <binaryen-c.h>
+
+int NUM_THREADS = 33;
+
+void worker() {
+ BinaryenModuleRef module = BinaryenModuleCreate();
+
+ // Create a function type for i32 (i32, i32)
+ BinaryenType params[2] = { BinaryenTypeInt32(), BinaryenTypeInt32() };
+ BinaryenFunctionTypeRef iii = BinaryenAddFunctionType(module, "iii", BinaryenTypeInt32(), params, 2);
+
+ // Get the 0 and 1 arguments, and add them
+ BinaryenExpressionRef x = BinaryenGetLocal(module, 0, BinaryenTypeInt32()),
+ y = BinaryenGetLocal(module, 1, BinaryenTypeInt32());
+ BinaryenExpressionRef add = BinaryenBinary(module, BinaryenAddInt32(), x, y);
+ BinaryenExpressionRef ret = BinaryenReturn(module, add);
+
+ // Create the add function
+ // Note: no additional local variables
+ // Note: no basic blocks here, we are an AST. The function body is just an expression node.
+ BinaryenFunctionRef adder = BinaryenAddFunction(module, "adder", iii, NULL, 0, ret);
+
+ // validate it
+ BinaryenModuleValidate(module);
+
+ // optimize it
+ BinaryenModuleOptimize(module);
+ BinaryenModuleValidate(module);
+
+ // Clean up the module, which owns all the objects we created above
+ BinaryenModuleDispose(module);
+}
+
+int main()
+{
+ std::vector<std::thread> threads;
+
+ std::cout << "create threads...\n";
+ for (int i = 0; i < NUM_THREADS; i++) {
+ threads.emplace_back(worker);
+ }
+ std::cout << "threads running in parallel...\n";
+
+ std::cout << "waiting for threads to join...\n";
+ for (auto& thread : threads) {
+ thread.join();
+ }
+
+ std::cout << "all done.\n";
+
+ return 0;
+}
diff --git a/test/example/cpp-threads.txt b/test/example/cpp-threads.txt
new file mode 100644
index 000000000..2c638aaab
--- /dev/null
+++ b/test/example/cpp-threads.txt
@@ -0,0 +1,4 @@
+create threads...
+threads running in parallel...
+waiting for threads to join...
+all done.