Skip to content

Commit

Permalink
fixed terminating stale threads on trap/proc_exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hritik Gupta committed Feb 6, 2023
1 parent 2eed50b commit 22ba795
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2190,6 +2190,13 @@ wasm_set_exception(WASMModuleInstance *module_inst, const char *exception)
}

#if WASM_ENABLE_THREAD_MGR != 0

#if WASM_ENABLE_SHARED_MEMORY
if (exception) {
notify_stale_threads_on_exception(
(WASMModuleInstanceCommon *)module_inst);
}
#endif
exec_env =
wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
if (exec_env) {
Expand Down
52 changes: 52 additions & 0 deletions core/iwasm/common/wasm_shared_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ typedef struct AtomicWaitNode {
korp_cond wait_cond;
} AtomicWaitNode;

typedef struct AtomicWaitAddressArgs {
uint32 len;
void **addr;
} AtomicWaitAddressArgs;

/* Atomic wait map */
static HashMap *wait_map;

Expand Down Expand Up @@ -87,6 +92,53 @@ search_module(WASMModuleCommon *module)
return NULL;
}

static void
create_list_of_waiter_addresses(void *key, void *value, void *user_data)
{
AtomicWaitAddressArgs *data = (AtomicWaitAddressArgs *)user_data;
if (data->len > 0) {
if (!(data->addr = wasm_runtime_realloc(data->addr,
data->len * sizeof(uint32)))) {
LOG_ERROR("failed to realloc atomic wait address list during "
"wait_map traversal");
return;
}
}
else {
if (!(data->addr = wasm_runtime_malloc(sizeof(data->len) + 1))) {
LOG_ERROR("failed to malloc atomic wait address list during "
"wait_map traversal");
return;
}
}
data->len++;
data->addr[data->len - 1] = key;
}

void
notify_stale_threads_on_exception(WASMModuleInstanceCommon *module_inst)
{
AtomicWaitAddressArgs *args = NULL;
if (!(args = wasm_runtime_malloc(sizeof(AtomicWaitAddressArgs)))) {
LOG_ERROR("failed to create atomic wait address args node");
return;
}

memset(args, 0, sizeof(*args));
os_mutex_lock(&shared_memory_list_lock);
// create list of addresses
bh_hash_map_traverse(wait_map, create_list_of_waiter_addresses, args);
os_mutex_unlock(&shared_memory_list_lock);

// notify
for (uint32 i = 0; i < args->len; i++) {
wasm_runtime_atomic_notify(module_inst, args->addr[i], UINT32_MAX);
}

// free memory allocated to args data
wasm_runtime_free(args);
}

WASMSharedMemNode *
wasm_module_get_shared_memory(WASMModuleCommon *module)
{
Expand Down
3 changes: 3 additions & 0 deletions core/iwasm/common/wasm_shared_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ wasm_shared_memory_init();
void
wasm_shared_memory_destroy();

void
notify_stale_threads_on_exception(WASMModuleInstanceCommon *module);

WASMSharedMemNode *
wasm_module_get_shared_memory(WASMModuleCommon *module);

Expand Down

0 comments on commit 22ba795

Please sign in to comment.