Skip to content

Commit

Permalink
allow loading wasm in threads
Browse files Browse the repository at this point in the history
  • Loading branch information
malytomas committed Jan 22, 2025
1 parent 4ea0687 commit 0b007c2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 0 additions & 2 deletions externals/wamr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ set(WAMR_DISABLE_HW_BOUND_CHECK 0)
set(WAMR_DISABLE_STACK_HW_BOUND_CHECK 0)
set(WAMR_DISABLE_WAKEUP_BLOCKING_OP 0)
add_compile_definitions(BUILTIN_LIBC_BUFFERED_PRINTF=1 BUILTIN_LIBC_BUFFERED_PRINT_SIZE=512)

add_compile_definitions("$<$<CONFIG:Debug>:BH_DEBUG=1>")

include("${CMAKE_CURRENT_LIST_DIR}/wamr/build-scripts/runtime_lib.cmake")

file(GLOB_RECURSE iwasm_headers "${CMAKE_CURRENT_LIST_DIR}/wamr/core/*.h")
add_library(iwasm_static STATIC ${WAMR_RUNTIME_LIB_SOURCE} ${iwasm_headers})
target_include_directories(iwasm_static INTERFACE "wamr/core/iwasm/include")
target_link_libraries(iwasm_static INTERFACE ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS})
target_compile_definitions(iwasm_static PUBLIC WASM_RUNTIME_API_EXTERN= WASM_API_EXTERN= COMPILING_WASM_RUNTIME_API=1)

if(MSVC)
Expand Down
11 changes: 11 additions & 0 deletions sources/libcore/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ namespace cage
return 0;
}();
(void)dummy;
thread_local int dummy2 = []() -> int
{
if (!wasm_runtime_init_thread_env())
CAGE_THROW_ERROR(Exception, "failed to initialize wasm thread env");
return 0;
}();
(void)dummy2;
}

WasmTypeEnum convert(wasm_valkind_t k)
Expand Down Expand Up @@ -188,6 +195,8 @@ namespace cage

WasmInstanceImpl(Holder<WasmModule> &&module_) : module(std::move(module_).cast<WasmModuleImpl>())
{
initialize();

CAGE_ASSERT(module);
String err;
instance = wasm_runtime_instantiate(module->module, 1'000'000, 1'000'000, err.rawData(), err.MaxLength - 100);
Expand Down Expand Up @@ -227,6 +236,8 @@ namespace cage

WasmFunctionImpl(WasmInstanceImpl *instance, const String &name) : name(name), instance(instance)
{
CAGE_ASSERT(wasm_runtime_thread_env_inited());

func = wasm_runtime_lookup_function(instance->instance, name.c_str());
if (!func)
{
Expand Down

0 comments on commit 0b007c2

Please sign in to comment.