-
Notifications
You must be signed in to change notification settings - Fork 749
Add an API to terminate instance #2538
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
fb063f6
a43afeb
4d41b26
94e8ecc
c09464c
aff793c
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 |
|---|---|---|
|
|
@@ -2342,8 +2342,8 @@ wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst_comm) | |
| return module_inst->exec_env_singleton; | ||
| } | ||
|
|
||
| void | ||
| wasm_set_exception(WASMModuleInstance *module_inst, const char *exception) | ||
| static void | ||
| wasm_set_exception_local(WASMModuleInstance *module_inst, const char *exception) | ||
| { | ||
| exception_lock(module_inst); | ||
| if (exception) { | ||
|
|
@@ -2354,13 +2354,22 @@ wasm_set_exception(WASMModuleInstance *module_inst, const char *exception) | |
| module_inst->cur_exception[0] = '\0'; | ||
| } | ||
| exception_unlock(module_inst); | ||
| } | ||
|
|
||
| void | ||
| wasm_set_exception(WASMModuleInstance *module_inst, const char *exception) | ||
| { | ||
| #if WASM_ENABLE_THREAD_MGR != 0 | ||
| WASMExecEnv *exec_env = | ||
| wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst); | ||
| if (exec_env) { | ||
| wasm_cluster_spread_exception(exec_env, exception); | ||
| wasm_cluster_set_exception(exec_env, exception); | ||
| } | ||
| else { | ||
| wasm_set_exception_local(module_inst, exception); | ||
| } | ||
| #else | ||
| wasm_set_exception_local(module_inst, exception); | ||
| #endif | ||
| } | ||
|
|
||
|
|
@@ -2468,6 +2477,18 @@ wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst_comm) | |
| wasm_runtime_set_exception(module_inst_comm, NULL); | ||
| } | ||
|
|
||
| #if WASM_ENABLE_THREAD_MGR != 0 | ||
| void | ||
| wasm_runtime_terminate(WASMModuleInstanceCommon *module_inst_comm) | ||
| { | ||
| WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm; | ||
|
|
||
| bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode | ||
| || module_inst_comm->module_type == Wasm_Module_AoT); | ||
| wasm_set_exception(module_inst, "terminated by user"); | ||
|
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. Seems our purpose is to terminate all threads, how about call
Contributor
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. wasm_cluster_terminate_all seems unused/untested and broken. (eg. cluster_list_lock vs cluster->lock locking order)
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. @yamt We are implementing the thread suspension/resume mechanism in PR #2320,
Contributor
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.
how it's implemented is not critical for me as far as it works. however, i want to provide a way for the terminated thread (the embedder app calling one of our execute APIs) to notice the fact it was terminated by also, the current
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. Yes, a trap may be a good option for the embedders to check, maybe we can add a flag to indicate whether to throw the exception or not in wasm_runtime_terminate and wasm_cluster_terminate_all. The current wasm_cluster_terminate_all joins all the threads which may include the main thread that initializes the runtime (if embedder calls the API in a another thread created by himself), it means that wasm_cluster_terminate_all waits until the main thread exits (and runtime is destroyed). This is not what embedder expects, I think we can change to wait until the cluster is destroyed in the cluster set. A potential issue of this PR is that wasm_cluster_terminate_all doesn't wait until all threads end their jobs, I am not sure whether it will cause unexpected behavior, it should be better if wasm_cluster_terminate_all can figure it out. |
||
| } | ||
| #endif | ||
|
|
||
| void | ||
| wasm_runtime_set_custom_data_internal( | ||
| WASMModuleInstanceCommon *module_inst_comm, void *custom_data) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1061,6 +1061,15 @@ set_thread_cancel_flags(WASMExecEnv *exec_env) | |
| os_mutex_unlock(&exec_env->wait_lock); | ||
| } | ||
|
|
||
| static void | ||
| clear_thread_cancel_flags(WASMExecEnv *exec_env) | ||
| { | ||
| os_mutex_lock(&exec_env->wait_lock); | ||
| WASM_SUSPEND_FLAGS_FETCH_AND(exec_env->suspend_flags, | ||
| ~WASM_SUSPEND_FLAG_TERMINATE); | ||
| os_mutex_unlock(&exec_env->wait_lock); | ||
| } | ||
|
|
||
| int32 | ||
| wasm_cluster_cancel_thread(WASMExecEnv *exec_env) | ||
| { | ||
|
|
@@ -1266,18 +1275,21 @@ set_exception_visitor(void *node, void *user_data) | |
| if (data->exception != NULL) { | ||
| set_thread_cancel_flags(exec_env); | ||
| } | ||
| else { | ||
| clear_thread_cancel_flags(exec_env); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void | ||
| wasm_cluster_spread_exception(WASMExecEnv *exec_env, const char *exception) | ||
| wasm_cluster_set_exception(WASMExecEnv *exec_env, const char *exception) | ||
| { | ||
| const bool has_exception = exception != NULL; | ||
| WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env); | ||
| bh_assert(cluster); | ||
|
|
||
| struct spread_exception_data data; | ||
| data.skip = exec_env; | ||
| data.skip = NULL; | ||
|
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. If
Contributor
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. yes, it can be simplified if we want. |
||
| data.exception = exception; | ||
|
|
||
| os_mutex_lock(&cluster->lock); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.