|
|
@@ -130,8 +130,21 @@ final:
|
|
|
|
|
|
/* The caller must lock cluster->lock */
|
|
|
static bool
|
|
|
-allocate_aux_stack(WASMCluster *cluster, uint32 *start, uint32 *size)
|
|
|
+allocate_aux_stack(WASMExecEnv *exec_env, uint32 *start, uint32 *size)
|
|
|
{
|
|
|
+ WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
|
|
|
+#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
|
|
|
+ WASMModuleInstanceCommon *module_inst =
|
|
|
+ wasm_exec_env_get_module_inst(exec_env);
|
|
|
+ uint32 stack_end;
|
|
|
+
|
|
|
+ stack_end =
|
|
|
+ wasm_runtime_module_malloc(module_inst, cluster->stack_size, NULL);
|
|
|
+ *start = stack_end + cluster->stack_size;
|
|
|
+ *size = cluster->stack_size;
|
|
|
+
|
|
|
+ return stack_end != 0;
|
|
|
+#else
|
|
|
uint32 i;
|
|
|
|
|
|
/* If the module doesn't have aux stack info,
|
|
|
@@ -151,12 +164,29 @@ allocate_aux_stack(WASMCluster *cluster, uint32 *start, uint32 *size)
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
/* The caller must lock cluster->lock */
|
|
|
static bool
|
|
|
-free_aux_stack(WASMCluster *cluster, uint32 start)
|
|
|
+free_aux_stack(WASMExecEnv *exec_env, uint32 start)
|
|
|
{
|
|
|
+ WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
|
|
|
+
|
|
|
+#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
|
|
|
+ WASMModuleInstanceCommon *module_inst =
|
|
|
+ wasm_exec_env_get_module_inst(exec_env);
|
|
|
+
|
|
|
+ if (!wasm_exec_env_is_aux_stack_managed_by_runtime(exec_env)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ bh_assert(start >= cluster->stack_size);
|
|
|
+
|
|
|
+ wasm_runtime_module_free(module_inst, start - cluster->stack_size);
|
|
|
+
|
|
|
+ return true;
|
|
|
+#else
|
|
|
uint32 i;
|
|
|
|
|
|
for (i = 0; i < cluster_max_thread_num; i++) {
|
|
|
@@ -166,14 +196,14 @@ free_aux_stack(WASMCluster *cluster, uint32 start)
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
WASMCluster *
|
|
|
wasm_cluster_create(WASMExecEnv *exec_env)
|
|
|
{
|
|
|
WASMCluster *cluster;
|
|
|
- uint64 total_size;
|
|
|
- uint32 aux_stack_start, aux_stack_size, i;
|
|
|
+ uint32 aux_stack_start, aux_stack_size;
|
|
|
|
|
|
bh_assert(exec_env->cluster == NULL);
|
|
|
if (!(cluster = wasm_runtime_malloc(sizeof(WASMCluster)))) {
|
|
|
@@ -209,12 +239,16 @@ wasm_cluster_create(WASMExecEnv *exec_env)
|
|
|
return cluster;
|
|
|
}
|
|
|
|
|
|
+#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
|
|
|
+ cluster->stack_size = aux_stack_size;
|
|
|
+#else
|
|
|
cluster->stack_size = aux_stack_size / (cluster_max_thread_num + 1);
|
|
|
if (cluster->stack_size < WASM_THREAD_AUX_STACK_SIZE_MIN) {
|
|
|
goto fail;
|
|
|
}
|
|
|
/* Make stack size 16-byte aligned */
|
|
|
cluster->stack_size = cluster->stack_size & (~15);
|
|
|
+#endif
|
|
|
|
|
|
/* Set initial aux stack top to the instance and
|
|
|
aux stack boundary to the main exec_env */
|
|
|
@@ -222,8 +256,10 @@ wasm_cluster_create(WASMExecEnv *exec_env)
|
|
|
cluster->stack_size))
|
|
|
goto fail;
|
|
|
|
|
|
+#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
|
|
|
if (cluster_max_thread_num != 0) {
|
|
|
- total_size = cluster_max_thread_num * sizeof(uint32);
|
|
|
+ uint64 total_size = cluster_max_thread_num * sizeof(uint32);
|
|
|
+ uint32 i;
|
|
|
if (total_size >= UINT32_MAX
|
|
|
|| !(cluster->stack_tops =
|
|
|
wasm_runtime_malloc((uint32)total_size))) {
|
|
|
@@ -245,6 +281,7 @@ wasm_cluster_create(WASMExecEnv *exec_env)
|
|
|
cluster->stack_tops[i] = aux_stack_start - cluster->stack_size * i;
|
|
|
}
|
|
|
}
|
|
|
+#endif
|
|
|
|
|
|
os_mutex_lock(&cluster_list_lock);
|
|
|
if (bh_list_insert(cluster_list, cluster) != 0) {
|
|
|
@@ -284,10 +321,12 @@ wasm_cluster_destroy(WASMCluster *cluster)
|
|
|
|
|
|
os_mutex_destroy(&cluster->lock);
|
|
|
|
|
|
+#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
|
|
|
if (cluster->stack_tops)
|
|
|
wasm_runtime_free(cluster->stack_tops);
|
|
|
if (cluster->stack_segment_occupied)
|
|
|
wasm_runtime_free(cluster->stack_segment_occupied);
|
|
|
+#endif
|
|
|
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
|
|
wasm_debug_instance_destroy(cluster);
|
|
|
@@ -323,7 +362,13 @@ wasm_cluster_add_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
|
|
|
|
|
|
exec_env->cluster = cluster;
|
|
|
|
|
|
- if (bh_list_insert(&cluster->exec_env_list, exec_env) != 0)
|
|
|
+ if (cluster->exec_env_list.len == cluster_max_thread_num + 1) {
|
|
|
+ LOG_ERROR("thread manager error: "
|
|
|
+ "maximum number of threads exceeded");
|
|
|
+ ret = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ret && bh_list_insert(&cluster->exec_env_list, exec_env) != 0)
|
|
|
ret = false;
|
|
|
|
|
|
return ret;
|
|
|
@@ -461,7 +506,7 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
|
|
|
if (!new_exec_env)
|
|
|
goto fail2;
|
|
|
|
|
|
- if (!allocate_aux_stack(cluster, &aux_stack_start, &aux_stack_size)) {
|
|
|
+ if (!allocate_aux_stack(exec_env, &aux_stack_start, &aux_stack_size)) {
|
|
|
LOG_ERROR("thread manager error: "
|
|
|
"failed to allocate aux stack space for new thread");
|
|
|
goto fail3;
|
|
|
@@ -482,7 +527,7 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
|
|
|
|
|
|
fail4:
|
|
|
/* free the allocated aux stack space */
|
|
|
- free_aux_stack(cluster, aux_stack_start);
|
|
|
+ free_aux_stack(exec_env, aux_stack_start);
|
|
|
fail3:
|
|
|
wasm_exec_env_destroy_internal(new_exec_env);
|
|
|
fail2:
|
|
|
@@ -502,7 +547,7 @@ wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
|
|
|
|
|
|
/* Free aux stack space */
|
|
|
os_mutex_lock(&cluster->lock);
|
|
|
- free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
|
|
|
+ free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
|
|
|
wasm_cluster_del_exec_env(cluster, exec_env);
|
|
|
os_mutex_unlock(&cluster->lock);
|
|
|
wasm_exec_env_destroy_internal(exec_env);
|
|
|
@@ -517,7 +562,11 @@ thread_manager_start_routine(void *arg)
|
|
|
void *ret;
|
|
|
WASMExecEnv *exec_env = (WASMExecEnv *)arg;
|
|
|
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
|
|
|
+ WASMModuleInstanceCommon *module_inst =
|
|
|
+ wasm_exec_env_get_module_inst(exec_env);
|
|
|
+
|
|
|
bh_assert(cluster != NULL);
|
|
|
+ bh_assert(module_inst != NULL);
|
|
|
|
|
|
exec_env->handle = os_self_thread();
|
|
|
ret = exec_env->thread_start_routine(exec_env);
|
|
|
@@ -528,17 +577,22 @@ thread_manager_start_routine(void *arg)
|
|
|
#endif
|
|
|
|
|
|
/* Routine exit */
|
|
|
+
|
|
|
/* Detach the native thread here to ensure the resources are freed */
|
|
|
wasm_cluster_detach_thread(exec_env);
|
|
|
#if WASM_ENABLE_DEBUG_INTERP != 0
|
|
|
wasm_cluster_thread_exited(exec_env);
|
|
|
#endif
|
|
|
+
|
|
|
os_mutex_lock(&cluster->lock);
|
|
|
/* Free aux stack space */
|
|
|
- free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
|
|
|
+ free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
|
|
|
+ /* routine exit, destroy instance */
|
|
|
+ wasm_runtime_deinstantiate_internal(module_inst, true);
|
|
|
/* Remove and exec_env */
|
|
|
wasm_cluster_del_exec_env(cluster, exec_env);
|
|
|
os_mutex_unlock(&cluster->lock);
|
|
|
+
|
|
|
/* destroy exec_env */
|
|
|
wasm_exec_env_destroy_internal(exec_env);
|
|
|
|
|
|
@@ -548,12 +602,12 @@ thread_manager_start_routine(void *arg)
|
|
|
|
|
|
int32
|
|
|
wasm_cluster_create_thread(WASMExecEnv *exec_env,
|
|
|
- wasm_module_inst_t module_inst,
|
|
|
+ wasm_module_inst_t module_inst, bool alloc_aux_stack,
|
|
|
void *(*thread_routine)(void *), void *arg)
|
|
|
{
|
|
|
WASMCluster *cluster;
|
|
|
WASMExecEnv *new_exec_env;
|
|
|
- uint32 aux_stack_start, aux_stack_size;
|
|
|
+ uint32 aux_stack_start = 0, aux_stack_size;
|
|
|
korp_tid tid;
|
|
|
|
|
|
cluster = wasm_exec_env_get_cluster(exec_env);
|
|
|
@@ -570,16 +624,23 @@ wasm_cluster_create_thread(WASMExecEnv *exec_env,
|
|
|
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;
|
|
|
- }
|
|
|
+ if (alloc_aux_stack) {
|
|
|
+ if (!allocate_aux_stack(exec_env, &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;
|
|
|
+ /* Set aux stack for current thread */
|
|
|
+ if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
|
|
|
+ aux_stack_size)) {
|
|
|
+ goto fail3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ /* Disable aux stack */
|
|
|
+ new_exec_env->aux_stack_boundary.boundary = 0;
|
|
|
+ new_exec_env->aux_stack_bottom.bottom = UINT32_MAX;
|
|
|
}
|
|
|
|
|
|
if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
|
|
|
@@ -603,7 +664,8 @@ fail4:
|
|
|
wasm_cluster_del_exec_env(cluster, new_exec_env);
|
|
|
fail3:
|
|
|
/* free the allocated aux stack space */
|
|
|
- free_aux_stack(cluster, aux_stack_start);
|
|
|
+ if (alloc_aux_stack)
|
|
|
+ free_aux_stack(exec_env, aux_stack_start);
|
|
|
fail2:
|
|
|
wasm_exec_env_destroy_internal(new_exec_env);
|
|
|
fail1:
|
|
|
@@ -849,7 +911,7 @@ wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
|
|
|
wasm_cluster_detach_thread(exec_env);
|
|
|
os_mutex_lock(&cluster->lock);
|
|
|
/* Free aux stack space */
|
|
|
- free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
|
|
|
+ free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
|
|
|
/* Remove and destroy exec_env */
|
|
|
wasm_cluster_del_exec_env(cluster, exec_env);
|
|
|
os_mutex_unlock(&cluster->lock);
|