add spawn thread API and sample (#333)
This commit is contained in:
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user