Skip to content

Commit 73ec790

Browse files
committed
wasm_cluster_spread_exception: simplify logic and fix minor locking error
1 parent 3004ee6 commit 73ec790

File tree

3 files changed

+34
-46
lines changed

3 files changed

+34
-46
lines changed

core/iwasm/common/wasm_runtime_common.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,8 +2377,6 @@ wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst_comm)
23772377
void
23782378
wasm_set_exception(WASMModuleInstance *module_inst, const char *exception)
23792379
{
2380-
WASMExecEnv *exec_env = NULL;
2381-
23822380
exception_lock(module_inst);
23832381
if (exception) {
23842382
snprintf(module_inst->cur_exception, sizeof(module_inst->cur_exception),
@@ -2390,13 +2388,11 @@ wasm_set_exception(WASMModuleInstance *module_inst, const char *exception)
23902388
exception_unlock(module_inst);
23912389

23922390
#if WASM_ENABLE_THREAD_MGR != 0
2393-
exec_env =
2391+
WASMExecEnv *exec_env =
23942392
wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
23952393
if (exec_env) {
2396-
wasm_cluster_spread_exception(exec_env, exception ? false : true);
2394+
wasm_cluster_spread_exception(exec_env, exception);
23972395
}
2398-
#else
2399-
(void)exec_env;
24002396
#endif
24012397
}
24022398

core/iwasm/libraries/thread-mgr/thread_manager.c

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,60 +1243,52 @@ wasm_cluster_resume_all(WASMCluster *cluster)
12431243
os_mutex_unlock(&cluster->lock);
12441244
}
12451245

1246+
struct spread_exception_data {
1247+
WASMExecEnv *skip;
1248+
const char *exception;
1249+
};
1250+
12461251
static void
12471252
set_exception_visitor(void *node, void *user_data)
12481253
{
1249-
WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
1250-
WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
1251-
WASMModuleInstanceCommon *module_inst = get_module_inst(exec_env);
1252-
WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst;
1253-
1254-
if (curr_exec_env != exec_env) {
1255-
WASMModuleInstance *curr_wasm_inst =
1256-
(WASMModuleInstance *)get_module_inst(curr_exec_env);
1257-
1258-
/* Only spread non "wasi proc exit" exception */
1259-
exception_lock(curr_wasm_inst);
1260-
if (!strstr(wasm_inst->cur_exception, "wasi proc exit")) {
1261-
bh_memcpy_s(curr_wasm_inst->cur_exception,
1262-
sizeof(curr_wasm_inst->cur_exception),
1263-
wasm_inst->cur_exception,
1264-
sizeof(wasm_inst->cur_exception));
1265-
}
1266-
exception_unlock(curr_wasm_inst);
1254+
const struct spread_exception_data *data = user_data;
1255+
WASMExecEnv *exec_env = (WASMExecEnv *)node;
12671256

1268-
/* Terminate the thread so it can exit from dead loops */
1269-
set_thread_cancel_flags(curr_exec_env);
1270-
}
1271-
}
1257+
if (exec_env != data->skip) {
1258+
WASMModuleInstance *wasm_inst =
1259+
(WASMModuleInstance *)get_module_inst(exec_env);
12721260

1273-
static void
1274-
clear_exception_visitor(void *node, void *user_data)
1275-
{
1276-
WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
1277-
WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
1278-
1279-
if (curr_exec_env != exec_env) {
1280-
WASMModuleInstance *curr_wasm_inst =
1281-
(WASMModuleInstance *)get_module_inst(curr_exec_env);
1261+
exception_lock(wasm_inst);
1262+
if (data->exception != NULL) {
1263+
snprintf(wasm_inst->cur_exception, sizeof(wasm_inst->cur_exception),
1264+
"Exception: %s", data->exception);
1265+
}
1266+
else {
1267+
wasm_inst->cur_exception[0] = '\0';
1268+
}
1269+
exception_unlock(wasm_inst);
12821270

1283-
exception_lock(curr_wasm_inst);
1284-
curr_wasm_inst->cur_exception[0] = '\0';
1285-
exception_unlock(curr_wasm_inst);
1271+
/* Terminate the thread so it can exit from dead loops */
1272+
if (data->exception != NULL) {
1273+
set_thread_cancel_flags(exec_env);
1274+
}
12861275
}
12871276
}
12881277

12891278
void
1290-
wasm_cluster_spread_exception(WASMExecEnv *exec_env, bool clear)
1279+
wasm_cluster_spread_exception(WASMExecEnv *exec_env, const char *exception)
12911280
{
1281+
const bool has_exception = exception != NULL;
12921282
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
12931283
bh_assert(cluster);
12941284

1285+
struct spread_exception_data data;
1286+
data.skip = exec_env;
1287+
data.exception = exception;
1288+
12951289
os_mutex_lock(&cluster->lock);
1296-
cluster->has_exception = !clear;
1297-
traverse_list(&cluster->exec_env_list,
1298-
clear ? clear_exception_visitor : set_exception_visitor,
1299-
exec_env);
1290+
cluster->has_exception = has_exception;
1291+
traverse_list(&cluster->exec_env_list, set_exception_visitor, &data);
13001292
os_mutex_unlock(&cluster->lock);
13011293
}
13021294

core/iwasm/libraries/thread-mgr/thread_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ WASMExecEnv *
139139
wasm_clusters_search_exec_env(WASMModuleInstanceCommon *module_inst);
140140

141141
void
142-
wasm_cluster_spread_exception(WASMExecEnv *exec_env, bool clear);
142+
wasm_cluster_spread_exception(WASMExecEnv *exec_env, const char *exception);
143143

144144
WASMExecEnv *
145145
wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env);

0 commit comments

Comments
 (0)