Fix multi-threading issues (#2013)
- Implement atomic.fence to ensure a proper memory synchronization order - Destroy exec_env_singleton first in wasm/aot deinstantiation - Change terminate other threads to wait for other threads in wasm_exec_env_destroy - Fix detach thread in thread_manager_start_routine - Fix duplicated lock cluster->lock in wasm_cluster_cancel_thread - Add lib-pthread and lib-wasi-threads compilation to Windows CI
This commit is contained in:
@ -1226,6 +1226,15 @@ fail:
|
||||
void
|
||||
aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
|
||||
{
|
||||
if (module_inst->exec_env_singleton) {
|
||||
/* wasm_exec_env_destroy will call
|
||||
wasm_cluster_wait_for_all_except_self to wait for other
|
||||
threads, so as to destroy their exec_envs and module
|
||||
instances first, and avoid accessing the shared resources
|
||||
of current module instance after it is deinstantiated. */
|
||||
wasm_exec_env_destroy((WASMExecEnv *)module_inst->exec_env_singleton);
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
/* Destroy wasi resource before freeing app heap, since some fields of
|
||||
wasi contex are allocated from app heap, and if app heap is freed,
|
||||
@ -1264,9 +1273,6 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
|
||||
if (module_inst->func_type_indexes)
|
||||
wasm_runtime_free(module_inst->func_type_indexes);
|
||||
|
||||
if (module_inst->exec_env_singleton)
|
||||
wasm_exec_env_destroy((WASMExecEnv *)module_inst->exec_env_singleton);
|
||||
|
||||
if (((AOTModuleInstanceExtra *)module_inst->e)->c_api_func_imports)
|
||||
wasm_runtime_free(
|
||||
((AOTModuleInstanceExtra *)module_inst->e)->c_api_func_imports);
|
||||
|
||||
@ -203,22 +203,12 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
|
||||
char *argv[])
|
||||
{
|
||||
bool ret;
|
||||
#if WASM_ENABLE_THREAD_MGR != 0
|
||||
WASMCluster *cluster;
|
||||
#endif
|
||||
#if WASM_ENABLE_THREAD_MGR != 0 || WASM_ENABLE_MEMORY_PROFILING != 0
|
||||
#if WASM_ENABLE_MEMORY_PROFILING != 0
|
||||
WASMExecEnv *exec_env;
|
||||
#endif
|
||||
|
||||
ret = execute_main(module_inst, argc, argv);
|
||||
|
||||
#if WASM_ENABLE_THREAD_MGR != 0
|
||||
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
|
||||
if (exec_env && (cluster = wasm_exec_env_get_cluster(exec_env))) {
|
||||
wasm_cluster_wait_for_all_except_self(cluster, exec_env);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_MEMORY_PROFILING != 0
|
||||
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
|
||||
if (exec_env) {
|
||||
@ -622,22 +612,12 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
|
||||
const char *name, int32 argc, char *argv[])
|
||||
{
|
||||
bool ret;
|
||||
#if WASM_ENABLE_THREAD_MGR != 0
|
||||
WASMCluster *cluster;
|
||||
#endif
|
||||
#if WASM_ENABLE_THREAD_MGR != 0 || WASM_ENABLE_MEMORY_PROFILING != 0
|
||||
#if WASM_ENABLE_MEMORY_PROFILING != 0
|
||||
WASMExecEnv *exec_env;
|
||||
#endif
|
||||
|
||||
ret = execute_func(module_inst, name, argc, argv);
|
||||
|
||||
#if WASM_ENABLE_THREAD_MGR != 0
|
||||
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
|
||||
if (exec_env && (cluster = wasm_exec_env_get_cluster(exec_env))) {
|
||||
wasm_cluster_wait_for_all_except_self(cluster, exec_env);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_MEMORY_PROFILING != 0
|
||||
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
|
||||
if (exec_env) {
|
||||
|
||||
@ -172,16 +172,16 @@ void
|
||||
wasm_exec_env_destroy(WASMExecEnv *exec_env)
|
||||
{
|
||||
#if WASM_ENABLE_THREAD_MGR != 0
|
||||
/* Terminate all sub-threads */
|
||||
/* Wait for all sub-threads */
|
||||
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
|
||||
if (cluster) {
|
||||
wasm_cluster_terminate_all_except_self(cluster, exec_env);
|
||||
wasm_cluster_wait_for_all_except_self(cluster, exec_env);
|
||||
#if WASM_ENABLE_DEBUG_INTERP != 0
|
||||
/* Must fire exit event after other threads exits, otherwise
|
||||
the stopped thread will be overrided by other threads */
|
||||
wasm_cluster_thread_exited(exec_env);
|
||||
#endif
|
||||
/* We have terminated other threads, this is the only alive thread, so
|
||||
/* We have waited for other threads, this is the only alive thread, so
|
||||
* we don't acquire cluster->lock because the cluster will be destroyed
|
||||
* inside this function */
|
||||
wasm_cluster_del_exec_env(cluster, exec_env);
|
||||
|
||||
@ -1240,6 +1240,8 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
|
||||
case WASM_OP_ATOMIC_FENCE:
|
||||
/* Skip memory index */
|
||||
frame_ip++;
|
||||
if (!aot_compiler_op_atomic_fence(comp_ctx, func_ctx))
|
||||
return false;
|
||||
break;
|
||||
case WASM_OP_ATOMIC_I32_LOAD:
|
||||
bytes = 4;
|
||||
|
||||
@ -1423,4 +1423,13 @@ fail:
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
aot_compiler_op_atomic_fence(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
|
||||
{
|
||||
return LLVMBuildFence(comp_ctx->builder,
|
||||
LLVMAtomicOrderingSequentiallyConsistent, false, "")
|
||||
? true
|
||||
: false;
|
||||
}
|
||||
|
||||
#endif /* end of WASM_ENABLE_SHARED_MEMORY */
|
||||
|
||||
@ -97,6 +97,10 @@ bool
|
||||
aot_compiler_op_atomic_notify(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx, uint32 align,
|
||||
uint32 offset, uint32 bytes);
|
||||
|
||||
bool
|
||||
aot_compiler_op_atomic_fence(AOTCompContext *comp_ctx,
|
||||
AOTFuncContext *func_ctx);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@ -3467,6 +3467,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
||||
{
|
||||
/* Skip the memory index */
|
||||
frame_ip++;
|
||||
os_atomic_thread_fence(os_memory_order_release);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -4006,7 +4007,8 @@ fast_jit_call_func_bytecode(WASMModuleInstance *module_inst,
|
||||
module_inst->fast_jit_func_ptrs[func_idx_non_import]);
|
||||
bh_assert(action == JIT_INTERP_ACTION_NORMAL
|
||||
|| (action == JIT_INTERP_ACTION_THROWN
|
||||
&& wasm_runtime_copy_exception(exec_env->module_inst, NULL)));
|
||||
&& wasm_copy_exception(
|
||||
(WASMModuleInstance *)exec_env->module_inst, NULL)));
|
||||
|
||||
/* Get the return values form info.out.ret */
|
||||
if (func_type->result_count) {
|
||||
|
||||
@ -3305,6 +3305,11 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
||||
PUSH_I32(ret);
|
||||
break;
|
||||
}
|
||||
case WASM_OP_ATOMIC_FENCE:
|
||||
{
|
||||
os_atomic_thread_fence(os_memory_order_release);
|
||||
break;
|
||||
}
|
||||
|
||||
case WASM_OP_ATOMIC_I32_LOAD:
|
||||
case WASM_OP_ATOMIC_I32_LOAD8_U:
|
||||
|
||||
@ -2073,6 +2073,15 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
|
||||
if (!module_inst)
|
||||
return;
|
||||
|
||||
if (module_inst->exec_env_singleton) {
|
||||
/* wasm_exec_env_destroy will call
|
||||
wasm_cluster_wait_for_all_except_self to wait for other
|
||||
threads, so as to destroy their exec_envs and module
|
||||
instances first, and avoid accessing the shared resources
|
||||
of current module instance after it is deinstantiated. */
|
||||
wasm_exec_env_destroy(module_inst->exec_env_singleton);
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_DEBUG_INTERP != 0 \
|
||||
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
|
||||
&& WASM_ENABLE_LAZY_JIT != 0)
|
||||
@ -2155,9 +2164,6 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
|
||||
wasm_externref_cleanup((WASMModuleInstanceCommon *)module_inst);
|
||||
#endif
|
||||
|
||||
if (module_inst->exec_env_singleton)
|
||||
wasm_exec_env_destroy(module_inst->exec_env_singleton);
|
||||
|
||||
#if WASM_ENABLE_DUMP_CALL_STACK != 0
|
||||
if (module_inst->frames) {
|
||||
bh_vector_destroy(module_inst->frames);
|
||||
@ -2241,7 +2247,6 @@ call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
|
||||
WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
|
||||
uint8 *prev_top = exec_env->wasm_stack.s.top;
|
||||
#ifdef BH_PLATFORM_WINDOWS
|
||||
const char *exce;
|
||||
int result;
|
||||
bool has_exception;
|
||||
char exception[EXCEPTION_BUF_LEN];
|
||||
|
||||
@ -100,7 +100,8 @@ safe_traverse_exec_env_list(WASMCluster *cluster, list_visitor visitor,
|
||||
while (node) {
|
||||
bool already_processed = false;
|
||||
void *proc_node;
|
||||
for (size_t i = 0; i < bh_vector_size(&proc_nodes); i++) {
|
||||
uint32 i;
|
||||
for (i = 0; i < (uint32)bh_vector_size(&proc_nodes); i++) {
|
||||
if (!bh_vector_get(&proc_nodes, i, &proc_node)) {
|
||||
ret = false;
|
||||
goto final;
|
||||
@ -595,14 +596,24 @@ thread_manager_start_routine(void *arg)
|
||||
|
||||
/* Routine exit */
|
||||
|
||||
/* Detach the native thread here to ensure the resources are freed */
|
||||
wasm_cluster_detach_thread(exec_env);
|
||||
#if WASM_ENABLE_DEBUG_INTERP != 0
|
||||
wasm_cluster_thread_exited(exec_env);
|
||||
#endif
|
||||
|
||||
os_mutex_lock(&cluster_list_lock);
|
||||
|
||||
os_mutex_lock(&cluster->lock);
|
||||
|
||||
/* Detach the native thread here to ensure the resources are freed */
|
||||
if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
|
||||
/* Only detach current thread when there is no other thread
|
||||
joining it, otherwise let the system resources for the
|
||||
thread be released after joining */
|
||||
os_thread_detach(exec_env->handle);
|
||||
/* No need to set exec_env->thread_is_detached to true here
|
||||
since we will exit soon */
|
||||
}
|
||||
|
||||
/* Free aux stack space */
|
||||
free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
|
||||
/* Remove exec_env */
|
||||
@ -614,6 +625,8 @@ thread_manager_start_routine(void *arg)
|
||||
|
||||
os_mutex_unlock(&cluster->lock);
|
||||
|
||||
os_mutex_unlock(&cluster_list_lock);
|
||||
|
||||
os_thread_exit(ret);
|
||||
return ret;
|
||||
}
|
||||
@ -936,12 +949,23 @@ wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
|
||||
wasm_cluster_clear_thread_signal(exec_env);
|
||||
wasm_cluster_thread_exited(exec_env);
|
||||
#endif
|
||||
|
||||
/* App exit the thread, free the resources before exit native thread */
|
||||
/* Detach the native thread here to ensure the resources are freed */
|
||||
wasm_cluster_detach_thread(exec_env);
|
||||
|
||||
os_mutex_lock(&cluster_list_lock);
|
||||
|
||||
os_mutex_lock(&cluster->lock);
|
||||
|
||||
/* Detach the native thread here to ensure the resources are freed */
|
||||
if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
|
||||
/* Only detach current thread when there is no other thread
|
||||
joining it, otherwise let the system resources for the
|
||||
thread be released after joining */
|
||||
os_thread_detach(exec_env->handle);
|
||||
/* No need to set exec_env->thread_is_detached to true here
|
||||
since we will exit soon */
|
||||
}
|
||||
|
||||
module_inst = exec_env->module_inst;
|
||||
|
||||
/* Free aux stack space */
|
||||
@ -955,6 +979,8 @@ wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
|
||||
|
||||
os_mutex_unlock(&cluster->lock);
|
||||
|
||||
os_mutex_unlock(&cluster_list_lock);
|
||||
|
||||
os_thread_exit(retval);
|
||||
}
|
||||
|
||||
@ -981,8 +1007,6 @@ wasm_cluster_cancel_thread(WASMExecEnv *exec_env)
|
||||
return 0;
|
||||
}
|
||||
|
||||
os_mutex_lock(&exec_env->cluster->lock);
|
||||
|
||||
if (!clusters_have_exec_env(exec_env)) {
|
||||
/* Invalid thread or the thread has exited */
|
||||
goto final;
|
||||
@ -991,7 +1015,6 @@ wasm_cluster_cancel_thread(WASMExecEnv *exec_env)
|
||||
set_thread_cancel_flags(exec_env);
|
||||
|
||||
final:
|
||||
os_mutex_unlock(&exec_env->cluster->lock);
|
||||
os_mutex_unlock(&cluster_list_lock);
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user