@@ -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+
12461251static void
12471252set_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
12891278void
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
0 commit comments