Skip to content

Commit

Permalink
[WASI Threads] handle imported shared memory
Browse files Browse the repository at this point in the history
  • Loading branch information
LFsWang committed May 3, 2023
1 parent 0c597bf commit 44a487a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/runtime/storemgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ class StoreManager {
return {};
}

// Expect<void>
// addMemoryInModule(std::string_view ModName,std::string_view MemName,
// std::unique_ptr<Instance::MemoryInstance> &&MemIns) {
// std::unique_lock Lock(Mutex);
// auto Iter = NamedMod.find(ModName);
// if (likely(Iter != NamedMod.cend())) {
// return Unexpect(ErrCode::Value::ModuleNameConflict); // TODO
// }
// Iter->second->addHostMemory(MemName, std::move(MemIns)); // Iter is const...
// return {};
// }

/// Collect the instantiation failed module.
void recycleModule(std::unique_ptr<Instance::ModuleInstance> &&Mod) {
FailedMod = std::move(Mod);
Expand Down
25 changes: 25 additions & 0 deletions lib/executor/instantiate/import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,31 @@ checkImportMatched(std::string_view ModName, std::string_view ExtName,
Expect<void> Executor::instantiate(Runtime::StoreManager &StoreMgr,
Runtime::Instance::ModuleInstance &ModInst,
const AST::ImportSection &ImportSec) {
// Find the first shared memory and create it
if (Conf.hasProposal(Proposal::Threads)) {
// Inject an shared memory in env
// TODO: if env is existing?
if (StoreMgr.findModule("env") == nullptr) {
for (const auto &ImpDesc : ImportSec.getContent()) {
auto ExtType = ImpDesc.getExternalType();
auto ModName = ImpDesc.getModuleName();
auto ExtName = ImpDesc.getExternalName();
if (ExtType == ExternalType::Memory && ModName == "env") {
auto MemType = ImpDesc.getExternalMemoryType();
if (MemType.getLimit().isShared()) {
spdlog::error("isShared");
spdlog::error(ExtName);
auto EnvMod = new Runtime::Instance::ModuleInstance("env");
EnvMod->addHostMemory(
ExtName, std::make_unique<
WasmEdge::Runtime::Instance::MemoryInstance>(MemType));
StoreMgr.registerModule(EnvMod);
}
}
}
}
}

// Iterate and instantiate import descriptions.
for (const auto &ImpDesc : ImportSec.getContent()) {
// Get data from import description and find import module.
Expand Down

0 comments on commit 44a487a

Please sign in to comment.