Skip to content

Commit 6119dec

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

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

core/iwasm/common/wasm_runtime_common.c

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

21922192
#if WASM_ENABLE_THREAD_MGR != 0
2193+
2194+
#if WASM_ENABLE_SHARED_MEMORY
2195+
if (exception) {
2196+
notify_stale_threads_on_exception(
2197+
(WASMModuleInstanceCommon *)module_inst);
2198+
}
2199+
#endif
21932200
exec_env =
21942201
wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
21952202
if (exec_env) {

core/iwasm/common/wasm_shared_memory.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ typedef struct AtomicWaitNode {
3030
korp_cond wait_cond;
3131
} AtomicWaitNode;
3232

33+
typedef struct AtomicWaitAddressArgs {
34+
uint32 index;
35+
void **addr;
36+
} AtomicWaitAddressArgs;
37+
3338
/* Atomic wait map */
3439
static HashMap *wait_map;
3540

@@ -87,6 +92,48 @@ search_module(WASMModuleCommon *module)
8792
return NULL;
8893
}
8994

95+
static void
96+
wait_map_address_count_callback(void *key, void *value,
97+
void *p_total_elem_count)
98+
{
99+
*(uint32 *)p_total_elem_count = *(uint32 *)p_total_elem_count + 1;
100+
}
101+
102+
static void
103+
create_list_of_waiter_addresses(void *key, void *value, void *user_data)
104+
{
105+
AtomicWaitAddressArgs *data = (AtomicWaitAddressArgs *)user_data;
106+
data->addr[data->index++] = key;
107+
}
108+
109+
void
110+
notify_stale_threads_on_exception(WASMModuleInstanceCommon *module_inst)
111+
{
112+
AtomicWaitAddressArgs args = { 0 };
113+
uint32 i = 0, total_elem_count = 0;
114+
115+
os_mutex_lock(&shared_memory_list_lock);
116+
117+
/* count number of addresses in wait_map */
118+
bh_hash_map_traverse(wait_map, wait_map_address_count_callback,
119+
(void *)&total_elem_count);
120+
121+
/* allocate memory */
122+
args.addr = wasm_runtime_malloc(sizeof(void *) * total_elem_count);
123+
124+
/* set values in list of addresses */
125+
bh_hash_map_traverse(wait_map, create_list_of_waiter_addresses, &args);
126+
os_mutex_unlock(&shared_memory_list_lock);
127+
128+
/* notify */
129+
for (i = 0; i < args.index; i++) {
130+
wasm_runtime_atomic_notify(module_inst, args.addr[i], UINT32_MAX);
131+
}
132+
133+
/* free memory allocated to args data */
134+
wasm_runtime_free(args.addr);
135+
}
136+
90137
WASMSharedMemNode *
91138
wasm_module_get_shared_memory(WASMModuleCommon *module)
92139
{

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)