Skip to content

Commit 64abe09

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

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "bh_log.h"
77
#include "wasm_shared_memory.h"
8+
#define WAIT_MAP_SIZE 32
89

910
static bh_list shared_memory_list_head;
1011
static bh_list *const shared_memory_list = &shared_memory_list_head;
@@ -30,6 +31,11 @@ typedef struct AtomicWaitNode {
3031
korp_cond wait_cond;
3132
} AtomicWaitNode;
3233

34+
typedef struct AtomicWaitAddressArgs {
35+
uint32 index;
36+
void *addr[WAIT_MAP_SIZE];
37+
} AtomicWaitAddressArgs;
38+
3339
/* Atomic wait map */
3440
static HashMap *wait_map;
3541

@@ -87,6 +93,37 @@ search_module(WASMModuleCommon *module)
8793
return NULL;
8894
}
8995

96+
static void
97+
create_list_of_waiter_addresses(void *key, void *value, void *user_data)
98+
{
99+
AtomicWaitAddressArgs *data = (AtomicWaitAddressArgs *)user_data;
100+
data->addr[(data->index++) % WAIT_MAP_SIZE] = key;
101+
}
102+
103+
void
104+
notify_stale_threads_on_exception(WASMModuleInstanceCommon *module_inst)
105+
{
106+
AtomicWaitAddressArgs *args = NULL;
107+
if (!(args = wasm_runtime_malloc(sizeof(AtomicWaitAddressArgs)))) {
108+
LOG_ERROR("failed to create atomic wait address args node");
109+
return;
110+
}
111+
112+
memset(args, 0, sizeof(*args));
113+
os_mutex_lock(&shared_memory_list_lock);
114+
// create list of addresses
115+
bh_hash_map_traverse(wait_map, create_list_of_waiter_addresses, args);
116+
os_mutex_unlock(&shared_memory_list_lock);
117+
118+
// notify
119+
for (uint32 i = 0; i < (args->index % WAIT_MAP_SIZE); i++) {
120+
wasm_runtime_atomic_notify(module_inst, args->addr[i], UINT32_MAX);
121+
}
122+
123+
// free memory allocated to args data
124+
wasm_runtime_free(args);
125+
}
126+
90127
WASMSharedMemNode *
91128
wasm_module_get_shared_memory(WASMModuleCommon *module)
92129
{

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)