@@ -77,8 +77,18 @@ traverse_list(bh_list *l, list_visitor visitor, void *user_data)
7777}
7878
7979static bool
80- allocate_aux_stack (WASMCluster * cluster , uint32 * start , uint32 * size )
80+ allocate_aux_stack (WASMExecEnv * exec_env , uint32 * start , uint32 * size )
8181{
82+ WASMCluster * cluster = wasm_exec_env_get_cluster (exec_env );
83+ #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
84+ WASMModuleInstanceCommon * module_inst =
85+ wasm_exec_env_get_module_inst (exec_env );
86+
87+ * start = wasm_runtime_module_malloc (module_inst , cluster -> stack_size , NULL );
88+ * size = cluster -> stack_size ;
89+
90+ return * start != 0 ;
91+ #else
8292 uint32 i ;
8393
8494 /* If the module doesn't have aux stack info,
@@ -100,11 +110,21 @@ allocate_aux_stack(WASMCluster *cluster, uint32 *start, uint32 *size)
100110 }
101111 os_mutex_unlock (& cluster -> lock );
102112 return false;
113+ #endif
103114}
104115
105116static bool
106- free_aux_stack (WASMCluster * cluster , uint32 start )
117+ free_aux_stack (WASMExecEnv * exec_env , uint32 start )
107118{
119+ #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
120+ WASMModuleInstanceCommon * module_inst =
121+ wasm_exec_env_get_module_inst (exec_env );
122+
123+ wasm_runtime_module_free (module_inst , start );
124+
125+ return true;
126+ #else
127+ WASMCluster * cluster = wasm_exec_env_get_cluster (exec_env );
108128 uint32 i ;
109129
110130 for (i = 0 ; i < cluster_max_thread_num ; i ++ ) {
@@ -116,14 +136,14 @@ free_aux_stack(WASMCluster *cluster, uint32 start)
116136 }
117137 }
118138 return false;
139+ #endif
119140}
120141
121142WASMCluster *
122143wasm_cluster_create (WASMExecEnv * exec_env )
123144{
124145 WASMCluster * cluster ;
125- uint64 total_size ;
126- uint32 aux_stack_start , aux_stack_size , i ;
146+ uint32 aux_stack_start , aux_stack_size ;
127147
128148 bh_assert (exec_env -> cluster == NULL );
129149 if (!(cluster = wasm_runtime_malloc (sizeof (WASMCluster )))) {
@@ -159,21 +179,27 @@ wasm_cluster_create(WASMExecEnv *exec_env)
159179 return cluster ;
160180 }
161181
182+ #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
183+ cluster -> stack_size = aux_stack_size ;
184+ #else
162185 cluster -> stack_size = aux_stack_size / (cluster_max_thread_num + 1 );
163186 if (cluster -> stack_size < WASM_THREAD_AUX_STACK_SIZE_MIN ) {
164187 goto fail ;
165188 }
166189 /* Make stack size 16-byte aligned */
167190 cluster -> stack_size = cluster -> stack_size & (~15 );
191+ #endif
168192
169193 /* Set initial aux stack top to the instance and
170194 aux stack boundary to the main exec_env */
171195 if (!wasm_exec_env_set_aux_stack (exec_env , aux_stack_start ,
172196 cluster -> stack_size ))
173197 goto fail ;
174198
199+ #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
175200 if (cluster_max_thread_num != 0 ) {
176- total_size = cluster_max_thread_num * sizeof (uint32 );
201+ uint64 total_size = cluster_max_thread_num * sizeof (uint32 );
202+ uint32 i ;
177203 if (total_size >= UINT32_MAX
178204 || !(cluster -> stack_tops =
179205 wasm_runtime_malloc ((uint32 )total_size ))) {
@@ -195,6 +221,7 @@ wasm_cluster_create(WASMExecEnv *exec_env)
195221 cluster -> stack_tops [i ] = aux_stack_start - cluster -> stack_size * i ;
196222 }
197223 }
224+ #endif
198225
199226 os_mutex_lock (& cluster_list_lock );
200227 if (bh_list_insert (cluster_list , cluster ) != 0 ) {
@@ -234,10 +261,12 @@ wasm_cluster_destroy(WASMCluster *cluster)
234261
235262 os_mutex_destroy (& cluster -> lock );
236263
264+ #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
237265 if (cluster -> stack_tops )
238266 wasm_runtime_free (cluster -> stack_tops );
239267 if (cluster -> stack_segment_occupied )
240268 wasm_runtime_free (cluster -> stack_segment_occupied );
269+ #endif
241270
242271#if WASM_ENABLE_DEBUG_INTERP != 0
243272 wasm_debug_instance_destroy (cluster );
@@ -273,7 +302,13 @@ wasm_cluster_add_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
273302 exec_env -> cluster = cluster ;
274303
275304 os_mutex_lock (& cluster -> lock );
276- if (bh_list_insert (& cluster -> exec_env_list , exec_env ) != 0 )
305+ if (cluster -> exec_env_list .len == cluster_max_thread_num + 1 ) {
306+ LOG_ERROR ("thread manager error: "
307+ "maximum number of threads exceeded" );
308+ ret = false;
309+ }
310+
311+ if (ret && bh_list_insert (& cluster -> exec_env_list , exec_env ) != 0 )
277312 ret = false;
278313 os_mutex_unlock (& cluster -> lock );
279314 return ret ;
@@ -407,7 +442,7 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
407442 if (!new_exec_env )
408443 goto fail1 ;
409444
410- if (!allocate_aux_stack (cluster , & aux_stack_start , & aux_stack_size )) {
445+ if (!allocate_aux_stack (exec_env , & aux_stack_start , & aux_stack_size )) {
411446 LOG_ERROR ("thread manager error: "
412447 "failed to allocate aux stack space for new thread" );
413448 goto fail2 ;
@@ -426,7 +461,7 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
426461
427462fail3 :
428463 /* free the allocated aux stack space */
429- free_aux_stack (cluster , aux_stack_start );
464+ free_aux_stack (exec_env , aux_stack_start );
430465fail2 :
431466 wasm_exec_env_destroy (new_exec_env );
432467fail1 :
@@ -443,7 +478,7 @@ wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
443478 bh_assert (cluster != NULL );
444479
445480 /* Free aux stack space */
446- free_aux_stack (cluster , exec_env -> aux_stack_bottom .bottom );
481+ free_aux_stack (exec_env , exec_env -> aux_stack_bottom .bottom );
447482 wasm_cluster_del_exec_env (cluster , exec_env );
448483 wasm_exec_env_destroy_internal (exec_env );
449484
@@ -457,7 +492,11 @@ thread_manager_start_routine(void *arg)
457492 void * ret ;
458493 WASMExecEnv * exec_env = (WASMExecEnv * )arg ;
459494 WASMCluster * cluster = wasm_exec_env_get_cluster (exec_env );
495+ WASMModuleInstanceCommon * module_inst =
496+ wasm_exec_env_get_module_inst (exec_env );
497+
460498 bh_assert (cluster != NULL );
499+ bh_assert (module_inst != NULL );
461500
462501 exec_env -> handle = os_self_thread ();
463502 ret = exec_env -> thread_start_routine (exec_env );
@@ -469,7 +508,11 @@ thread_manager_start_routine(void *arg)
469508
470509 /* Routine exit */
471510 /* Free aux stack space */
472- free_aux_stack (cluster , exec_env -> aux_stack_bottom .bottom );
511+ free_aux_stack (exec_env , exec_env -> aux_stack_bottom .bottom );
512+
513+ /* routine exit, destroy instance */
514+ wasm_runtime_deinstantiate_internal (module_inst , true);
515+
473516 /* Detach the native thread here to ensure the resources are freed */
474517 wasm_cluster_detach_thread (exec_env );
475518#if WASM_ENABLE_DEBUG_INTERP != 0
@@ -501,7 +544,7 @@ wasm_cluster_create_thread(WASMExecEnv *exec_env,
501544 if (!new_exec_env )
502545 return -1 ;
503546
504- if (!allocate_aux_stack (cluster , & aux_stack_start , & aux_stack_size )) {
547+ if (!allocate_aux_stack (exec_env , & aux_stack_start , & aux_stack_size )) {
505548 LOG_ERROR ("thread manager error: "
506549 "failed to allocate aux stack space for new thread" );
507550 goto fail1 ;
@@ -532,7 +575,7 @@ wasm_cluster_create_thread(WASMExecEnv *exec_env,
532575 wasm_cluster_del_exec_env (cluster , new_exec_env );
533576fail2 :
534577 /* free the allocated aux stack space */
535- free_aux_stack (cluster , aux_stack_start );
578+ free_aux_stack (exec_env , aux_stack_start );
536579fail1 :
537580 wasm_exec_env_destroy (new_exec_env );
538581 return -1 ;
@@ -762,7 +805,7 @@ wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
762805#endif
763806 /* App exit the thread, free the resources before exit native thread */
764807 /* Free aux stack space */
765- free_aux_stack (cluster , exec_env -> aux_stack_bottom .bottom );
808+ free_aux_stack (exec_env , exec_env -> aux_stack_bottom .bottom );
766809 /* Detach the native thread here to ensure the resources are freed */
767810 wasm_cluster_detach_thread (exec_env );
768811 /* Remove and destroy exec_env */
0 commit comments