diff --git a/tests/test_threads.py b/tests/test_threads.py new file mode 100644 index 00000000..c32d08d9 --- /dev/null +++ b/tests/test_threads.py @@ -0,0 +1,28 @@ +import unittest + +from wasmtime import * + + +class TestThreads(unittest.TestCase): + def test_threads(self): + config = Config() + config.wasm_threads = True + + engine = Engine(config) + linker = Linker(engine) + store = Store(engine) + + linker.define_wasi() + + wasi_config = WasiConfig() + wasi_config.argv = ['3'] + wasi_config.inherit_argv() + wasi_config.inherit_stdout() + store.set_wasi(wasi_config) + + module = Module.from_file(engine, 'tests/threads.wasm') + # linker.define_wasi_threads(store, module) + # instance = linker.instantiate(store, module) + # instance.exports(store)["_start"](store) + + diff --git a/tests/threads.cxx b/tests/threads.cxx new file mode 100644 index 00000000..f7d505f4 --- /dev/null +++ b/tests/threads.cxx @@ -0,0 +1,47 @@ +// Demo CLI that creates threads and uses atomics. +// +// Compile with the wasi-sdk: +// +// ${WASI_SDK_PATH}/bin/clang++ -pthread --target=wasm32-wasi-threads --sysroot=${WASI_SYSROOT} -matomics -Wl,--import-memory,--export-memory,--initial-memory=8388608,--max-memory=4294967296,--shared-memory -fno-exceptions -Os threads.cxx + +#include +#include +#include +#include +#include + + +int main(int argc, char * argv[]) +{ + int num_threads = 4; + if (argc > 1) + { + num_threads = std::stoi(argv[1]); + } + + std::vector threads; + threads.reserve(num_threads); + + std::atomic created_threads(0); + + for (int i = 0; i < num_threads; ++i) + { + threads.emplace_back([i, &created_threads]() + { + created_threads++; + }); + } + + for (auto &t : threads) + { + t.join(); + } + + std::cout << "Created " << created_threads << " threads." << std::endl; + + if (created_threads == num_threads) + { + return EXIT_SUCCESS; + } + return EXIT_FAILURE; +} diff --git a/tests/threads.wasm b/tests/threads.wasm new file mode 100755 index 00000000..103040b3 Binary files /dev/null and b/tests/threads.wasm differ