Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/iwasm/common/wasm_exec_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,20 @@ void
wasm_exec_env_set_thread_info(WASMExecEnv *exec_env)
{
uint8 *stack_boundary = os_thread_get_stack_boundary();

#if WASM_ENABLE_THREAD_MGR != 0
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
if (cluster)
os_mutex_lock(&cluster->lock);
Comment thread
eloparco marked this conversation as resolved.
Outdated
#endif
exec_env->handle = os_self_thread();
exec_env->native_stack_boundary =
stack_boundary ? stack_boundary + WASM_STACK_GUARD_SIZE : NULL;
exec_env->native_stack_top_min = (void *)UINTPTR_MAX;
#if WASM_ENABLE_THREAD_MGR != 0
if (cluster)
os_mutex_unlock(&cluster->lock);
#endif
}

#if WASM_ENABLE_THREAD_MGR != 0
Expand Down
18 changes: 17 additions & 1 deletion core/iwasm/interpreter/wasm_interp_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
#if WASM_ENABLE_SHARED_MEMORY != 0
#include "../common/wasm_shared_memory.h"
#endif
#if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
#if WASM_ENABLE_THREAD_MGR != 0
#include "../libraries/thread-mgr/thread_manager.h"
#endif
#if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
#include "../libraries/debug-engine/debug_engine.h"
#endif
#if WASM_ENABLE_FAST_JIT != 0
Expand Down Expand Up @@ -1036,27 +1038,41 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst,
#if WASM_ENABLE_DEBUG_INTERP != 0
#define CHECK_SUSPEND_FLAGS() \
do { \
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env); \
if (cluster) \
os_mutex_lock(&cluster->lock); \
Comment thread
eloparco marked this conversation as resolved.
Outdated
if (IS_WAMR_TERM_SIG(exec_env->current_status->signal_flag)) { \
if (cluster) \
os_mutex_unlock(&cluster->lock); \
return; \
} \
if (IS_WAMR_STOP_SIG(exec_env->current_status->signal_flag)) { \
SYNC_ALL_TO_FRAME(); \
wasm_cluster_thread_waiting_run(exec_env); \
} \
if (cluster) \
os_mutex_unlock(&cluster->lock); \
} while (0)
#else
#define CHECK_SUSPEND_FLAGS() \
do { \
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env); \
if (cluster) \
os_mutex_lock(&cluster->lock); \
if (exec_env->suspend_flags.flags != 0) { \
if (exec_env->suspend_flags.flags & 0x01) { \
/* terminate current thread */ \
if (cluster) \
os_mutex_unlock(&cluster->lock); \
return; \
} \
while (exec_env->suspend_flags.flags & 0x02) { \
/* suspend current thread */ \
os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock); \
} \
} \
if (cluster) \
os_mutex_unlock(&cluster->lock); \
} while (0)
#endif /* WASM_ENABLE_DEBUG_INTERP */
#endif /* WASM_ENABLE_THREAD_MGR */
Expand Down
29 changes: 20 additions & 9 deletions core/iwasm/interpreter/wasm_interp_fast.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include "../common/wasm_shared_memory.h"
#endif

#if WASM_ENABLE_THREAD_MGR != 0
#include "../libraries/thread-mgr/thread_manager.h"
#endif

typedef int32 CellType_I32;
typedef int64 CellType_I64;
typedef float32 CellType_F32;
Expand Down Expand Up @@ -1052,15 +1056,22 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst,
#endif

#if WASM_ENABLE_THREAD_MGR != 0
#define CHECK_SUSPEND_FLAGS() \
do { \
if (exec_env->suspend_flags.flags != 0) { \
if (exec_env->suspend_flags.flags & 0x01) { \
/* terminate current thread */ \
return; \
} \
/* TODO: support suspend and breakpoint */ \
} \
#define CHECK_SUSPEND_FLAGS() \
do { \
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env); \
if (cluster) \
os_mutex_lock(&cluster->lock); \
if (exec_env->suspend_flags.flags != 0) { \
if (exec_env->suspend_flags.flags & 0x01) { \
/* terminate current thread */ \
if (cluster) \
os_mutex_unlock(&cluster->lock); \
return; \
} \
/* TODO: support suspend and breakpoint */ \
} \
if (cluster) \
os_mutex_unlock(&cluster->lock); \
} while (0)
#endif

Expand Down
13 changes: 12 additions & 1 deletion core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,18 @@ execute_interruptible_poll_oneoff(
return err;
}

if (wasm_cluster_is_thread_terminated(exec_env)) {
#if WASM_ENABLE_THREAD_MGR != 0
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
if (cluster)
os_mutex_lock(&cluster->lock);
#endif
bool is_thread_terminated = wasm_cluster_is_thread_terminated(exec_env);
Comment thread
eloparco marked this conversation as resolved.
Outdated
#if WASM_ENABLE_THREAD_MGR != 0
if (cluster)
os_mutex_unlock(&cluster->lock);
#endif

if (is_thread_terminated) {
wasm_runtime_free(in_copy);
return EINTR;
}
Expand Down
12 changes: 10 additions & 2 deletions core/iwasm/libraries/thread-mgr/thread_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,16 @@ thread_manager_start_routine(void *arg)
bh_assert(cluster != NULL);
bh_assert(module_inst != NULL);

os_mutex_lock(&cluster->lock);
exec_env->handle = os_self_thread();
os_mutex_unlock(&cluster->lock);
ret = exec_env->thread_start_routine(exec_env);

#ifdef OS_ENABLE_HW_BOUND_CHECK
os_mutex_lock(&cluster->lock);
if (exec_env->suspend_flags.flags & 0x08)
ret = exec_env->thread_ret_value;
os_mutex_unlock(&cluster->lock);
#endif

/* Routine exit */
Expand Down Expand Up @@ -822,15 +826,18 @@ clusters_have_exec_env(WASMExecEnv *exec_env)
WASMExecEnv *node;

while (cluster) {
os_mutex_lock(&cluster->lock);
node = bh_list_first_elem(&cluster->exec_env_list);

while (node) {
if (node == exec_env) {
bh_assert(exec_env->cluster == cluster);
os_mutex_unlock(&cluster->lock);
return true;
}
node = bh_list_elem_next(node);
}
os_mutex_unlock(&cluster->lock);

cluster = bh_list_elem_next(cluster);
}
Expand All @@ -844,7 +851,6 @@ wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val)
korp_tid handle;

os_mutex_lock(&cluster_list_lock);
os_mutex_lock(&exec_env->cluster->lock);

if (!clusters_have_exec_env(exec_env) || exec_env->thread_is_detached) {
/* Invalid thread, thread has exited or thread has been detached */
Expand All @@ -854,10 +860,12 @@ wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val)
os_mutex_unlock(&cluster_list_lock);
return 0;
}

os_mutex_lock(&exec_env->cluster->lock);
exec_env->wait_count++;
handle = exec_env->handle;

os_mutex_unlock(&exec_env->cluster->lock);

os_mutex_unlock(&cluster_list_lock);

return os_thread_join(handle, ret_val);
Expand Down