Fix execute_main not wait for other threads (#1137)

Fix wasm_application_execute_main/wasm_application_execute_func not waiting for
other threads to terminate in multi-thread mode, which causes that the exception
thrown by other threads may haven't been spreaded to current main thread, and
cannot be detected by the caller, as reported in #1131.
This commit is contained in:
Wenyong Huang
2022-04-29 15:47:43 +08:00
committed by GitHub
parent 2e27d506d8
commit c6997aa68a
4 changed files with 92 additions and 10 deletions

View File

@ -681,8 +681,8 @@ wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val)
korp_tid handle;
os_mutex_lock(&cluster_list_lock);
if (!clusters_have_exec_env(exec_env)) {
/* Invalid thread or the thread has exited */
if (!clusters_have_exec_env(exec_env) || exec_env->thread_is_detached) {
/* Invalid thread, thread has exited or thread has been detached */
if (ret_val)
*ret_val = NULL;
os_mutex_unlock(&cluster_list_lock);
@ -710,6 +710,7 @@ wasm_cluster_detach_thread(WASMExecEnv *exec_env)
joining it, otherwise let the system resources for the
thread be released after joining */
ret = os_thread_detach(exec_env->handle);
exec_env->thread_is_detached = true;
}
os_mutex_unlock(&cluster_list_lock);
return ret;
@ -802,6 +803,33 @@ wasm_cluster_terminate_all_except_self(WASMCluster *cluster,
(void *)exec_env);
}
static void
wait_for_thread_visitor(void *node, void *user_data)
{
WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
korp_tid handle;
if (curr_exec_env == exec_env)
return;
wasm_cluster_join_thread(curr_exec_env, NULL);
}
void
wams_cluster_wait_for_all(WASMCluster *cluster)
{
traverse_list(&cluster->exec_env_list, wait_for_thread_visitor, NULL);
}
void
wasm_cluster_wait_for_all_except_self(WASMCluster *cluster,
WASMExecEnv *exec_env)
{
traverse_list(&cluster->exec_env_list, wait_for_thread_visitor,
(void *)exec_env);
}
bool
wasm_cluster_register_destroy_callback(void (*callback)(WASMCluster *))
{