add spawn thread API and sample (#333)

This commit is contained in:
Xu Jun
2020-08-04 17:40:45 +08:00
committed by GitHub
parent ed8ddb2cea
commit 2db335c6d4
11 changed files with 591 additions and 7 deletions

View File

@ -234,6 +234,10 @@ wasm_runtime_full_init(RuntimeInitArgs *init_args)
return false;
}
#if WASM_ENABLE_THREAD_MGR != 0
wasm_cluster_set_max_thread_num(init_args->max_thread_num);
#endif
return true;
}
@ -2850,3 +2854,77 @@ wasm_runtime_call_indirect(WASMExecEnv *exec_env,
#endif
return false;
}
#if WASM_ENABLE_THREAD_MGR != 0
typedef struct WASMThreadArg {
WASMExecEnv *new_exec_env;
wasm_thread_callback_t callback;
void *arg;
} WASMThreadArg;
WASMExecEnv *
wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
{
return wasm_cluster_spawn_exec_env(exec_env);
}
void
wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
{
wasm_cluster_destroy_spawned_exec_env(exec_env);
}
static void*
wasm_runtime_thread_routine(void *arg)
{
WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
void *ret;
bh_assert(thread_arg->new_exec_env);
ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
wasm_runtime_free(thread_arg);
os_thread_exit(ret);
return ret;
}
int32
wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
wasm_thread_callback_t callback, void *arg)
{
WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
WASMThreadArg *thread_arg;
int32 ret;
if (!new_exec_env)
return -1;
if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
wasm_runtime_destroy_spawned_exec_env(new_exec_env);
return -1;
}
thread_arg->new_exec_env = new_exec_env;
thread_arg->callback = callback;
thread_arg->arg = arg;
ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
if (ret != 0) {
wasm_runtime_destroy_spawned_exec_env(new_exec_env);
wasm_runtime_free(thread_arg);
}
return ret;
}
int32
wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
{
return os_thread_join((korp_tid)tid, retval);
}
#endif

View File

@ -114,6 +114,10 @@ typedef struct RuntimeInitArgs {
const char *native_module_name;
NativeSymbol *native_symbols;
uint32_t n_native_symbols;
/* maximum thread number, only used when
WASM_ENABLE_THREAD_MGR is defined */
uint32_t max_thread_num;
} RuntimeInitArgs;
/**
@ -684,6 +688,11 @@ void *
wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
#if WASM_ENABLE_THREAD_MGR != 0
/* wasm thread callback function type */
typedef void* (*wasm_thread_callback_t)(wasm_exec_env_t, void *);
/* wasm thread type */
typedef uintptr_t wasm_thread_t;
/**
* Set the max thread num per cluster.
*
@ -691,6 +700,50 @@ wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
*/
void
wasm_runtime_set_max_thread_num(uint32_t num);
/**
* spawn a new exec_env, the spawned exec_env
* can be used in other threads
*
* @param num the original exec_env
*
* @return the spawned exec_env if success, NULL otherwise
*/
wasm_exec_env_t
wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
/**
* Destroy the spawned exec_env
*
* @param exec_env the spawned exec_env
*/
void
wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
/**
* spawn a thread from the given exec_env
*
* @param exec_env the original exec_env
* @param tid thread id to be returned to the caller
* @param callback the callback function provided by the user
* @param arg the arguments passed to the callback
*
* @return 0 if success, -1 otherwise
*/
int32_t
wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
wasm_thread_callback_t callback, void *arg);
/**
* waits a spawned thread to terminate
*
* @param tid thread id
* @param retval if not NULL, output the return value of the thread
*
* @return 0 if success, error number otherwise
*/
int32_t
wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
#endif
#ifdef __cplusplus

View File

@ -27,7 +27,8 @@ static uint32 cluster_max_thread_num = CLUSTER_MAX_THREAD_NUM;
void
wasm_cluster_set_max_thread_num(uint32 num)
{
cluster_max_thread_num = num;
if (num > 0)
cluster_max_thread_num = num;
}
bool
@ -278,6 +279,70 @@ wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
return ret;
}
WASMExecEnv *
wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
{
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
wasm_module_t module = wasm_exec_env_get_module(exec_env);
wasm_module_inst_t new_module_inst;
WASMExecEnv *new_exec_env;
uint32 aux_stack_start, aux_stack_size;
if (!(new_module_inst =
wasm_runtime_instantiate_internal(module, true, 8192,
0, NULL, 0))) {
return NULL;
}
new_exec_env = wasm_exec_env_create_internal(
new_module_inst, exec_env->wasm_stack_size);
if (!new_exec_env)
goto fail1;
if (!allocate_aux_stack(cluster, &aux_stack_start, &aux_stack_size)) {
LOG_ERROR("thread manager error: "
"failed to allocate aux stack space for new thread");
goto fail2;
}
/* Set aux stack for current thread */
if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
aux_stack_size)) {
goto fail3;
}
if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
goto fail3;
return new_exec_env;
fail3:
/* free the allocated aux stack space */
free_aux_stack(cluster, aux_stack_start);
fail2:
wasm_exec_env_destroy(new_exec_env);
fail1:
wasm_runtime_deinstantiate_internal(new_module_inst, true);
return NULL;
}
void
wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
{
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
wasm_module_inst_t module_inst = wasm_runtime_get_module_inst(exec_env);
bh_assert(cluster != NULL);
/* Free aux stack space */
free_aux_stack(cluster,
exec_env->aux_stack_boundary + cluster->stack_size);
wasm_cluster_del_exec_env(cluster, exec_env);
wasm_exec_env_destroy_internal(exec_env);
wasm_runtime_deinstantiate_internal(module_inst, true);
}
/* start routine of thread manager */
static void*
thread_manager_start_routine(void *arg)

View File

@ -109,6 +109,12 @@ wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env);
void
wasm_cluster_spread_exception(WASMExecEnv *exec_env);
WASMExecEnv *
wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env);
void
wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env);
#ifdef __cplusplus
}
#endif