diff --git a/core/config.h b/core/config.h index cb5db1d0cf..31404deb95 100644 --- a/core/config.h +++ b/core/config.h @@ -84,7 +84,7 @@ #endif #define AOT_MAGIC_NUMBER 0x746f6100 -#define AOT_CURRENT_VERSION 5 +#define AOT_CURRENT_VERSION 6 #ifndef WASM_ENABLE_JIT #define WASM_ENABLE_JIT 0 diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index cfbd5f8e6e..fa3212dfa9 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1963,6 +1963,12 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent, module_inst->e = (WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset); extra = (AOTModuleInstanceExtra *)module_inst->e; +#if WASM_ENABLE_THREAD_MGR != 0 + if (os_mutex_init(&extra->common.exception_lock) != 0) { + wasm_runtime_free(module_inst); + return NULL; + } +#endif #if WASM_ENABLE_GC != 0 /* Initialize gc heap first since it may be used when initializing @@ -2353,6 +2359,10 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst) wasm_exec_env_destroy((WASMExecEnv *)module_inst->exec_env_singleton); } +#if WASM_ENABLE_THREAD_MGR != 0 + os_mutex_destroy(&extra->common.exception_lock); +#endif + #if WASM_ENABLE_PERF_PROFILING != 0 if (module_inst->func_perf_profilings) wasm_runtime_free(module_inst->func_perf_profilings); diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index d0bd0b35ad..98686c3f42 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -8136,3 +8136,18 @@ wasm_runtime_check_and_update_last_used_shared_heap( return false; } #endif + +WASMModuleInstanceExtraCommon * +GetModuleInstanceExtraCommon(WASMModuleInstance *module_inst) +{ +#if WASM_ENABLE_AOT != 0 + if (module_inst->module_type == Wasm_Module_AoT) { + return &((AOTModuleInstanceExtra *)module_inst->e)->common; + } + else { + return &module_inst->e->common; + } +#else + return &module_inst->e->common; +#endif +} diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index 88f23485e8..0477387ed0 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -1427,6 +1427,9 @@ wasm_runtime_check_and_update_last_used_shared_heap( uint8 **shared_heap_base_addr_adj_p, bool is_memory64); #endif +struct WASMModuleInstanceExtraCommon * +GetModuleInstanceExtraCommon(struct WASMModuleInstance *module_inst); + #ifdef __cplusplus } #endif diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index cac3730bfe..e41cb4d8e8 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -2512,6 +2512,12 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, module_inst->module = module; module_inst->e = (WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset); +#if WASM_ENABLE_THREAD_MGR != 0 + if (os_mutex_init(&module_inst->e->common.exception_lock) != 0) { + wasm_runtime_free(module_inst); + return NULL; + } +#endif #if WASM_ENABLE_MULTI_MODULE != 0 module_inst->e->sub_module_inst_list = @@ -3501,6 +3507,9 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) bh_bitmap_delete(module_inst->e->common.elem_dropped); #endif +#if WASM_ENABLE_THREAD_MGR != 0 + os_mutex_destroy(&module_inst->e->common.exception_lock); +#endif wasm_runtime_free(module_inst); } diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index 0ba2049af9..98913e9fee 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -334,6 +334,9 @@ typedef struct WASMModuleInstanceExtraCommon { /* The gc heap created */ void *gc_heap_handle; #endif +#if WASM_ENABLE_THREAD_MGR != 0 + korp_mutex exception_lock; +#endif } WASMModuleInstanceExtraCommon; /* Extra info of WASM module instance for interpreter/jit mode */ diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index 5a647a9b64..563a9098e1 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -29,8 +29,6 @@ static bh_list cluster_list_head; static bh_list *const cluster_list = &cluster_list_head; static korp_mutex cluster_list_lock; -static korp_mutex _exception_lock; - typedef void (*list_visitor)(void *, void *); static uint32 cluster_max_thread_num = CLUSTER_MAX_THREAD_NUM; @@ -51,10 +49,6 @@ thread_manager_init() return false; if (os_mutex_init(&cluster_list_lock) != 0) return false; - if (os_mutex_init(&_exception_lock) != 0) { - os_mutex_destroy(&cluster_list_lock); - return false; - } return true; } @@ -69,7 +63,6 @@ thread_manager_destroy() cluster = next; } wasm_cluster_cancel_all_callbacks(); - os_mutex_destroy(&_exception_lock); os_mutex_destroy(&cluster_list_lock); } @@ -1540,19 +1533,13 @@ wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env) void exception_lock(WASMModuleInstance *module_inst) { - /* - * Note: this lock could be per module instance if desirable. - * We can revisit on AOT version bump. - * It probably doesn't matter though because the exception handling - * logic should not be executed too frequently anyway. - */ - os_mutex_lock(&_exception_lock); + os_mutex_lock(&GetModuleInstanceExtraCommon(module_inst)->exception_lock); } void exception_unlock(WASMModuleInstance *module_inst) { - os_mutex_unlock(&_exception_lock); + os_mutex_unlock(&GetModuleInstanceExtraCommon(module_inst)->exception_lock); } void