Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions tests/test_threads.py
Original file line number Diff line number Diff line change
@@ -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)


47 changes: 47 additions & 0 deletions tests/threads.cxx
Original file line number Diff line number Diff line change
@@ -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 <sstream>
#include <iostream>
#include <atomic>
#include <thread>
#include <vector>


int main(int argc, char * argv[])
{
int num_threads = 4;
if (argc > 1)
{
num_threads = std::stoi(argv[1]);
}

std::vector<std::thread> threads;
threads.reserve(num_threads);

std::atomic<int> 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;
}
Binary file added tests/threads.wasm
Binary file not shown.
Loading