-
Notifications
You must be signed in to change notification settings - Fork 768
Fix multi-threading issues #2013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
616beaf
d1fe8ed
c495f14
d16ae8a
11c3781
783474c
213bcb9
966129e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had better add comment for this function to ask the caller to ensure thread safety
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the code here since clusters_have_exec_env will lock again, which leads to hang. |
||
|
|
||
| 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; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this lock required by
wasm_cluster_del_exec_env?Had better update the comment for
wasm_cluster_del_exec_env:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this lock here is to lock
wasm_cluster_del_exec_env only needs to lock cluster->lock