Skip to content

Commit 92b3d04

Browse files
author
Hritik Gupta
committed
fixed terminating stale threads on trap/proc_exit
1 parent 2eed50b commit 92b3d04

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

core/iwasm/common/wasm_runtime_common.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,6 +2190,10 @@ wasm_set_exception(WASMModuleInstance *module_inst, const char *exception)
21902190
}
21912191

21922192
#if WASM_ENABLE_THREAD_MGR != 0
2193+
if (exception) {
2194+
notify_stale_threads_on_exception(
2195+
(WASMModuleInstanceCommon *)module_inst);
2196+
}
21932197
exec_env =
21942198
wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
21952199
if (exec_env) {

core/iwasm/common/wasm_shared_memory.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef struct AtomicWaitNode {
3232

3333
/* Atomic wait map */
3434
static HashMap *wait_map;
35+
static HashMap *wait_map_without_lock;
3536

3637
static uint32
3738
wait_address_hash(void *address);
@@ -87,6 +88,41 @@ search_module(WASMModuleCommon *module)
8788
return NULL;
8889
}
8990

91+
static void
92+
notify_stale_threads(void *key, void *value, void *user_data)
93+
{
94+
WASMModuleInstanceCommon *module_inst =
95+
(WASMModuleInstanceCommon *)user_data;
96+
wasm_runtime_atomic_notify(module_inst, key, UINT32_MAX);
97+
bh_hash_map_remove(wait_map, key, NULL, NULL);
98+
}
99+
100+
static void
101+
create_copy_of_wait_map(void *key, void *value, void *map_copy)
102+
{
103+
bh_hash_map_insert((HashMap *)map_copy, key, value);
104+
}
105+
106+
void
107+
notify_stale_threads_on_exception(WASMModuleInstanceCommon *module_inst)
108+
{
109+
110+
wait_map_without_lock = bh_hash_map_create(
111+
32, false, (HashFunc)wait_address_hash,
112+
(KeyEqualFunc)wait_address_equal, NULL, destroy_wait_info);
113+
/**
114+
* create a copy of wait_map with no lock
115+
* this allows invoking methods in the callback
116+
* which require acquiring lock on the original map
117+
*/
118+
bh_hash_map_traverse(wait_map, create_copy_of_wait_map,
119+
wait_map_without_lock);
120+
121+
// terminate stale threads
122+
bh_hash_map_traverse(wait_map_without_lock, notify_stale_threads,
123+
module_inst);
124+
}
125+
90126
WASMSharedMemNode *
91127
wasm_module_get_shared_memory(WASMModuleCommon *module)
92128
{

core/iwasm/common/wasm_shared_memory.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ wasm_shared_memory_init();
3737
void
3838
wasm_shared_memory_destroy();
3939

40+
void
41+
notify_stale_threads_on_exception(WASMModuleInstanceCommon *module);
42+
4043
WASMSharedMemNode *
4144
wasm_module_get_shared_memory(WASMModuleCommon *module);
4245

0 commit comments

Comments
 (0)