2019-07-12 14:00:53 +00:00
|
|
|
// Copyright 2019 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "test/wasm-api-tests/wasm-api-test.h"
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
using ::wasm::Shared;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const int kNumThreads = 10;
|
|
|
|
const int kIterationsPerThread = 3;
|
|
|
|
int g_traces;
|
|
|
|
|
2019-08-19 14:35:47 +00:00
|
|
|
own<Trap> Callback(void* env, const Val args[], Val results[]) {
|
2019-07-12 14:00:53 +00:00
|
|
|
std::lock_guard<std::mutex> lock(*reinterpret_cast<std::mutex*>(env));
|
|
|
|
g_traces += args[0].i32();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Main(Engine* engine, Shared<Module>* shared, std::mutex* mutex, int id) {
|
2019-08-19 14:35:47 +00:00
|
|
|
own<Store> store = Store::make(engine);
|
|
|
|
own<Module> module = Module::obtain(store.get(), shared);
|
2019-07-12 14:00:53 +00:00
|
|
|
EXPECT_NE(nullptr, module.get());
|
|
|
|
for (int i = 0; i < kIterationsPerThread; i++) {
|
|
|
|
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
|
|
|
|
|
|
|
// Create imports.
|
2019-08-19 14:35:47 +00:00
|
|
|
own<FuncType> func_type =
|
|
|
|
FuncType::make(ownvec<ValType>::make(ValType::make(::wasm::I32)),
|
|
|
|
ownvec<ValType>::make());
|
|
|
|
own<Func> func = Func::make(store.get(), func_type.get(), Callback, mutex);
|
|
|
|
own<::wasm::GlobalType> global_type =
|
2019-07-12 14:00:53 +00:00
|
|
|
::wasm::GlobalType::make(ValType::make(::wasm::I32), ::wasm::CONST);
|
2019-08-19 14:35:47 +00:00
|
|
|
own<Global> global =
|
2019-07-12 14:00:53 +00:00
|
|
|
Global::make(store.get(), global_type.get(), Val::i32(id));
|
|
|
|
|
|
|
|
// Instantiate and run.
|
|
|
|
// With the current implementation of the WasmModuleBuilder, global
|
|
|
|
// imports always come before function imports, regardless of the
|
|
|
|
// order of builder()->Add*Import() calls below.
|
|
|
|
Extern* imports[] = {global.get(), func.get()};
|
2019-08-19 14:35:47 +00:00
|
|
|
own<Instance> instance = Instance::make(store.get(), module.get(), imports);
|
|
|
|
ownvec<Extern> exports = instance->exports();
|
2019-07-12 14:00:53 +00:00
|
|
|
Func* run_func = exports[0]->func();
|
|
|
|
run_func->call();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_F(WasmCapiTest, Threads) {
|
|
|
|
// Create module.
|
|
|
|
ValueType i32_type[] = {kWasmI32};
|
|
|
|
FunctionSig param_i32(0, 1, i32_type);
|
|
|
|
uint32_t callback_index =
|
2021-06-17 15:43:55 +00:00
|
|
|
builder()->AddImport(base::CStrVector("callback"), ¶m_i32);
|
2019-07-12 14:00:53 +00:00
|
|
|
uint32_t global_index =
|
2021-06-17 15:43:55 +00:00
|
|
|
builder()->AddGlobalImport(base::CStrVector("id"), kWasmI32, false);
|
2019-07-12 14:00:53 +00:00
|
|
|
|
|
|
|
byte code[] = {
|
2020-12-17 16:56:08 +00:00
|
|
|
WASM_CALL_FUNCTION(callback_index, WASM_GLOBAL_GET(global_index))};
|
2019-07-12 14:00:53 +00:00
|
|
|
FunctionSig empty_sig(0, 0, nullptr);
|
2021-06-17 15:43:55 +00:00
|
|
|
AddExportedFunction(base::CStrVector("run"), code, sizeof(code), &empty_sig);
|
2019-07-12 14:00:53 +00:00
|
|
|
Compile();
|
2019-08-19 14:35:47 +00:00
|
|
|
own<Shared<Module>> shared = module()->share();
|
2019-07-12 14:00:53 +00:00
|
|
|
|
|
|
|
// Spawn threads.
|
|
|
|
g_traces = 0;
|
|
|
|
std::mutex mutex;
|
|
|
|
std::thread threads[kNumThreads];
|
|
|
|
for (int i = 0; i < kNumThreads; i++) {
|
|
|
|
threads[i] = std::thread(Main, engine(), shared.get(), &mutex, i);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < kNumThreads; i++) {
|
|
|
|
threads[i].join();
|
|
|
|
}
|
|
|
|
// Each thread in each iteration adds its ID to {traces}, so in the end
|
|
|
|
// we expect kIterationsPerThread * sum([0, ..., kNumThreads-1]).
|
|
|
|
// Per Gauss:
|
|
|
|
const int kExpected =
|
|
|
|
kIterationsPerThread * (kNumThreads - 1) * kNumThreads / 2;
|
|
|
|
EXPECT_EQ(kExpected, g_traces);
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:55:38 +00:00
|
|
|
TEST_F(WasmCapiTest, MultiStoresOneThread) {
|
|
|
|
// These Stores intentionally have overlapping, but non-nested lifetimes.
|
2019-08-19 14:35:47 +00:00
|
|
|
own<Store> store1 = Store::make(engine());
|
|
|
|
own<Store> store2 = Store::make(engine());
|
|
|
|
own<Store> store3 = Store::make(engine());
|
2019-07-12 14:55:38 +00:00
|
|
|
store1.reset();
|
|
|
|
store2.reset();
|
|
|
|
store3.reset();
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:00:53 +00:00
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|