Skip to content

Commit

Permalink
Use a new flag for enabling on-heap aux stack allocations
Browse files Browse the repository at this point in the history
So it can be used in thread manager even if wasi threads aren't enabled.
  • Loading branch information
loganek committed Dec 14, 2022
1 parent 9440bbd commit 762b501
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
7 changes: 7 additions & 0 deletions core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@
#define WASM_ENABLE_LIB_WASI_THREADS 0
#endif

#ifndef WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION
#define WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION WASM_ENABLE_LIB_WASI_THREADS
#elif WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0 \
&& WASM_ENABLE_LIB_WASI_THREADS == 1
#error "Heap aux stack allocation must be enabled for WASI threads"
#endif

#ifndef WASM_ENABLE_BASE_LIB
#define WASM_ENABLE_BASE_LIB 0
#endif
Expand Down
3 changes: 1 addition & 2 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2444,8 +2444,7 @@ wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size)
(WASMModuleInstance *)exec_env->module_inst;
uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;

/* For WASI threads aux space is allocated on the heap */
#if WASM_ENABLE_LIB_WASI_THREADS != 1
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 1
/* Check the aux stack space */
uint32 data_end = module_inst->module->aux_data_end;
uint32 stack_bottom = module_inst->module->aux_stack_bottom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

set (LIB_WASI_THREADS_DIR ${CMAKE_CURRENT_LIST_DIR})

add_definitions (-DWASM_ENABLE_LIB_WASI_THREADS=1)
add_definitions (-DWASM_ENABLE_LIB_WASI_THREADS=1 -DWASM_ENABLE_HEAP_AUX_STACK_ALLOCATION)

include_directories(${LIB_WASI_THREADS_DIR})

Expand Down
10 changes: 5 additions & 5 deletions core/iwasm/libraries/thread-mgr/thread_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static bool
allocate_aux_stack(WASMExecEnv *exec_env, uint32 *start, uint32 *size)
{
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
#if WASM_ENABLE_LIB_WASI_THREADS == 1
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 1
WASMModuleInstanceCommon *module_inst =
wasm_exec_env_get_module_inst(exec_env);

Expand Down Expand Up @@ -116,7 +116,7 @@ allocate_aux_stack(WASMExecEnv *exec_env, uint32 *start, uint32 *size)
static bool
free_aux_stack(WASMExecEnv *exec_env, uint32 start)
{
#if WASM_ENABLE_LIB_WASI_THREADS == 1
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 1
WASMModuleInstanceCommon *module_inst =
wasm_exec_env_get_module_inst(exec_env);

Expand Down Expand Up @@ -179,7 +179,7 @@ wasm_cluster_create(WASMExecEnv *exec_env)
return cluster;
}

#if WASM_ENABLE_LIB_WASI_THREADS == 1
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 1
cluster->stack_size = aux_stack_size;
#else
cluster->stack_size = aux_stack_size / (cluster_max_thread_num + 1);
Expand All @@ -196,7 +196,7 @@ wasm_cluster_create(WASMExecEnv *exec_env)
cluster->stack_size))
goto fail;

#if WASM_ENABLE_LIB_WASI_THREADS != 1
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 1
if (cluster_max_thread_num != 0) {
uint64 total_size = cluster_max_thread_num * sizeof(uint32);
uint32 i;
Expand Down Expand Up @@ -261,7 +261,7 @@ wasm_cluster_destroy(WASMCluster *cluster)

os_mutex_destroy(&cluster->lock);

#if WASM_ENABLE_LIB_WASI_THREADS != 1
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 1
if (cluster->stack_tops)
wasm_runtime_free(cluster->stack_tops);
if (cluster->stack_segment_occupied)
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/libraries/thread-mgr/thread_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct WASMCluster {
korp_mutex lock;
bh_list exec_env_list;

#if WASM_ENABLE_LIB_WASI_THREADS != 1
#if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 1
/* The aux stack of a module with shared memory will be
divided into several segments. This array store the
stack top of different segments */
Expand Down

0 comments on commit 762b501

Please sign in to comment.