1616#include "debug_engine.h"
1717#endif
1818
19- #if WASM_ENABLE_SHARED_MEMORY != 0
20- #include "wasm_shared_memory.h"
21- #endif
22-
2319typedef struct {
2420 bh_list_link l ;
2521 void (* destroy_cb )(WASMCluster * );
@@ -32,6 +28,8 @@ static bh_list cluster_list_head;
3228static bh_list * const cluster_list = & cluster_list_head ;
3329static korp_mutex cluster_list_lock ;
3430
31+ static korp_mutex _exception_lock ;
32+
3533typedef void (* list_visitor )(void * , void * );
3634
3735static uint32 cluster_max_thread_num = CLUSTER_MAX_THREAD_NUM ;
@@ -52,6 +50,10 @@ thread_manager_init()
5250 return false;
5351 if (os_mutex_init (& cluster_list_lock ) != 0 )
5452 return false;
53+ if (os_mutex_init (& _exception_lock ) != 0 ) {
54+ os_mutex_destroy (& cluster_list_lock );
55+ return false;
56+ }
5557 return true;
5658}
5759
@@ -66,6 +68,7 @@ thread_manager_destroy()
6668 cluster = next ;
6769 }
6870 wasm_cluster_cancel_all_callbacks ();
71+ os_mutex_destroy (& _exception_lock );
6972 os_mutex_destroy (& cluster_list_lock );
7073}
7174
@@ -1253,20 +1256,14 @@ set_exception_visitor(void *node, void *user_data)
12531256 (WASMModuleInstance * )get_module_inst (curr_exec_env );
12541257
12551258 /* Only spread non "wasi proc exit" exception */
1256- #if WASM_ENABLE_SHARED_MEMORY != 0
1257- if (curr_wasm_inst -> memory_count > 0 )
1258- shared_memory_lock (curr_wasm_inst -> memories [0 ]);
1259- #endif
1259+ exception_lock (curr_wasm_inst );
12601260 if (!strstr (wasm_inst -> cur_exception , "wasi proc exit" )) {
12611261 bh_memcpy_s (curr_wasm_inst -> cur_exception ,
12621262 sizeof (curr_wasm_inst -> cur_exception ),
12631263 wasm_inst -> cur_exception ,
12641264 sizeof (wasm_inst -> cur_exception ));
12651265 }
1266- #if WASM_ENABLE_SHARED_MEMORY != 0
1267- if (curr_wasm_inst -> memory_count > 0 )
1268- shared_memory_unlock (curr_wasm_inst -> memories [0 ]);
1269- #endif
1266+ exception_unlock (curr_wasm_inst );
12701267
12711268 /* Terminate the thread so it can exit from dead loops */
12721269 set_thread_cancel_flags (curr_exec_env );
@@ -1283,15 +1280,9 @@ clear_exception_visitor(void *node, void *user_data)
12831280 WASMModuleInstance * curr_wasm_inst =
12841281 (WASMModuleInstance * )get_module_inst (curr_exec_env );
12851282
1286- #if WASM_ENABLE_SHARED_MEMORY != 0
1287- if (curr_wasm_inst -> memory_count > 0 )
1288- shared_memory_lock (curr_wasm_inst -> memories [0 ]);
1289- #endif
1283+ exception_lock (curr_wasm_inst );
12901284 curr_wasm_inst -> cur_exception [0 ] = '\0' ;
1291- #if WASM_ENABLE_SHARED_MEMORY != 0
1292- if (curr_wasm_inst -> memory_count > 0 )
1293- shared_memory_unlock (curr_wasm_inst -> memories [0 ]);
1294- #endif
1285+ exception_unlock (curr_wasm_inst );
12951286 }
12961287}
12971288
@@ -1353,3 +1344,21 @@ wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env)
13531344
13541345 return is_thread_terminated ;
13551346}
1347+
1348+ void
1349+ exception_lock (WASMModuleInstance * module_inst )
1350+ {
1351+ /*
1352+ * Note: this lock could be per module instance if desirable.
1353+ * We can revisit on AOT version bump.
1354+ * It probably doesn't matter though because the exception handling
1355+ * logic should not be executed too frequently anyway.
1356+ */
1357+ os_mutex_lock (& _exception_lock );
1358+ }
1359+
1360+ void
1361+ exception_unlock (WASMModuleInstance * module_inst )
1362+ {
1363+ os_mutex_unlock (& _exception_lock );
1364+ }
0 commit comments