Spread module custom data to all threads, enable libc-builtin float print (#633)

This commit is contained in:
Xu Jun 2021-05-19 14:57:31 +08:00 committed by GitHub
parent a14a4487bb
commit f637438e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 88 additions and 11 deletions

View File

@ -182,6 +182,8 @@ endif ()
if (WAMR_DISABLE_HW_BOUND_CHECK EQUAL 1) if (WAMR_DISABLE_HW_BOUND_CHECK EQUAL 1)
add_definitions (-DWASM_DISABLE_HW_BOUND_CHECK=1) add_definitions (-DWASM_DISABLE_HW_BOUND_CHECK=1)
message (" Hardware boundary check disabled") message (" Hardware boundary check disabled")
else ()
add_definitions (-DWASM_DISABLE_HW_BOUND_CHECK=0)
endif () endif ()
if (WAMR_BUILD_SIMD EQUAL 1) if (WAMR_BUILD_SIMD EQUAL 1)
add_definitions (-DWASM_ENABLE_SIMD=1) add_definitions (-DWASM_ENABLE_SIMD=1)

View File

@ -1438,8 +1438,8 @@ wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst)
} }
void void
wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst, wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
void *custom_data) void *custom_data)
{ {
#if WASM_ENABLE_INTERP != 0 #if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode) { if (module_inst->module_type == Wasm_Module_Bytecode) {
@ -1455,6 +1455,17 @@ wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
#endif #endif
} }
void
wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
void *custom_data)
{
#if WASM_ENABLE_THREAD_MGR != 0
wasm_cluster_spread_custom_data(module_inst, custom_data);
#else
wasm_runtime_set_custom_data_internal(module_inst, custom_data);
#endif
}
void* void*
wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst) wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst)
{ {

View File

@ -490,6 +490,11 @@ wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
WASM_RUNTIME_API_EXTERN void WASM_RUNTIME_API_EXTERN void
wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst); wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
/* Internal API */
void
wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
void *custom_data);
/* See wasm_export.h for description */ /* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN void WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst, wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,

View File

@ -554,6 +554,9 @@ wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
/** /**
* Set custom data to WASM module instance. * Set custom data to WASM module instance.
* Note:
* If WAMR_BUILD_LIB_PTHREAD is enabled, this API
* will spread the custom data to all threads
* *
* @param module_inst the WASM module instance * @param module_inst the WASM module instance
* @param custom_data the custom data to be set * @param custom_data the custom data to be set

View File

@ -6152,7 +6152,7 @@ wasm_loader_prepare_bytecode(WASMModule *module,
BranchBlock *frame_csp_tmp; BranchBlock *frame_csp_tmp;
#if WASM_ENABLE_FAST_INTERP != 0 #if WASM_ENABLE_FAST_INTERP != 0
uint8 *func_const_end, *func_const = NULL; uint8 *func_const_end, *func_const = NULL;
int16 operand_offset; int16 operand_offset = 0;
uint8 last_op = 0; uint8 last_op = 0;
bool disable_emit, preserve_local = false; bool disable_emit, preserve_local = false;
float32 f32; float32 f32;

View File

@ -4758,7 +4758,7 @@ wasm_loader_prepare_bytecode(WASMModule *module,
#endif #endif
#if WASM_ENABLE_FAST_INTERP != 0 #if WASM_ENABLE_FAST_INTERP != 0
uint8 *func_const_end, *func_const = NULL; uint8 *func_const_end, *func_const = NULL;
int16 operand_offset; int16 operand_offset = 0;
uint8 last_op = 0; uint8 last_op = 0;
bool disable_emit, preserve_local = false; bool disable_emit, preserve_local = false;
float32 f32; float32 f32;

View File

@ -535,13 +535,13 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
void *arg) /* arguments buffer */ void *arg) /* arguments buffer */
{ {
wasm_module_t module = get_module(exec_env); wasm_module_t module = get_module(exec_env);
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasm_module_inst_t new_module_inst = NULL; wasm_module_inst_t new_module_inst = NULL;
ThreadInfoNode *info_node = NULL; ThreadInfoNode *info_node = NULL;
ThreadRoutineArgs *routine_args = NULL; ThreadRoutineArgs *routine_args = NULL;
uint32 thread_handle; uint32 thread_handle;
int32 ret = -1; int32 ret = -1;
#if WASM_ENABLE_LIBC_WASI != 0 #if WASM_ENABLE_LIBC_WASI != 0
wasm_module_inst_t module_inst = get_module_inst(exec_env);
WASIContext *wasi_ctx = get_wasi_ctx(module_inst); WASIContext *wasi_ctx = get_wasi_ctx(module_inst);
#endif #endif
@ -552,6 +552,13 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
NULL, 0))) NULL, 0)))
return -1; return -1;
if (module_inst) {
/* Set custom_data to new module instance */
wasm_runtime_set_custom_data_internal(
new_module_inst,
wasm_runtime_get_custom_data(module_inst));
}
#if WASM_ENABLE_LIBC_WASI != 0 #if WASM_ENABLE_LIBC_WASI != 0
if (wasi_ctx) if (wasi_ctx)
wasm_runtime_set_wasi_ctx(new_module_inst, wasi_ctx); wasm_runtime_set_wasi_ctx(new_module_inst, wasi_ctx);

View File

@ -361,6 +361,19 @@ handle_1_to_9:
break; break;
} }
case 'f': {
float64 f64;
char buf[16], *s;
CHECK_VA_ARG(ap, float64);
f64 = _va_arg(ap, float64);
snprintf(buf, sizeof(buf), "%f", f64);
s = buf;
while (*s)
out((int) (*s++), ctx);
break;
}
default: default:
out((int) '%', ctx); out((int) '%', ctx);
out((int) *fmt, ctx); out((int) *fmt, ctx);

View File

@ -328,6 +328,7 @@ WASMExecEnv *
wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
{ {
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env); WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasm_module_t module = wasm_exec_env_get_module(exec_env); wasm_module_t module = wasm_exec_env_get_module(exec_env);
wasm_module_inst_t new_module_inst; wasm_module_inst_t new_module_inst;
WASMExecEnv *new_exec_env; WASMExecEnv *new_exec_env;
@ -343,6 +344,13 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
return NULL; return NULL;
} }
if (module_inst) {
/* Set custom_data to new module instance */
wasm_runtime_set_custom_data_internal(
new_module_inst,
wasm_runtime_get_custom_data(module_inst));
}
new_exec_env = wasm_exec_env_create_internal( new_exec_env = wasm_exec_env_create_internal(
new_module_inst, exec_env->wasm_stack_size); new_module_inst, exec_env->wasm_stack_size);
if (!new_exec_env) if (!new_exec_env)
@ -654,3 +662,24 @@ wasm_cluster_spread_exception(WASMExecEnv *exec_env)
traverse_list(&cluster->exec_env_list, set_exception_visitor, exec_env); traverse_list(&cluster->exec_env_list, set_exception_visitor, exec_env);
} }
static void
set_custom_data_visitor(void *node, void *user_data)
{
WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
wasm_runtime_set_custom_data_internal(module_inst, user_data);
}
void
wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
void *custom_data)
{
WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
traverse_list(&cluster->exec_env_list,
set_custom_data_visitor,
custom_data);
}

View File

@ -118,6 +118,10 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env);
void void
wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env); wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env);
void
wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
void *custom_data);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -10,8 +10,8 @@
extern "C" { extern "C" {
#endif #endif
#include "../../../config.h"
#include "platform_internal.h" #include "platform_internal.h"
#include "../../../config.h"
#define BH_MAX_THREAD 32 #define BH_MAX_THREAD 32

View File

@ -231,7 +231,7 @@ int os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
unsigned int stack_size) unsigned int stack_size)
{ {
return os_thread_create_with_prio(p_tid, start, arg, stack_size, return os_thread_create_with_prio(p_tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY); BH_THREAD_DEFAULT_PRIORITY);
} }
int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
@ -253,6 +253,9 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
memset(tid, 0, sizeof(os_thread_obj)); memset(tid, 0, sizeof(os_thread_obj));
if (stack_size < APP_THREAD_STACK_SIZE_MIN)
stack_size = APP_THREAD_STACK_SIZE_MIN;
/* Create and initialize thread data */ /* Create and initialize thread data */
thread_data_size = offsetof(os_thread_data, stack) + stack_size; thread_data_size = offsetof(os_thread_data, stack) + stack_size;
if (!(thread_data = BH_MALLOC(thread_data_size))) { if (!(thread_data = BH_MALLOC(thread_data_size))) {
@ -266,9 +269,9 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
thread_data->tid = tid; thread_data->tid = tid;
/* Create the thread */ /* Create the thread */
if (!((tid = k_thread_create(tid, (k_thread_stack_t *) thread_data->stack, if (!((tid = k_thread_create(tid, (k_thread_stack_t *)thread_data->stack,
stack_size, os_thread_wrapper, start, arg, thread_data, prio, 0, stack_size, os_thread_wrapper, start, arg,
K_NO_WAIT)))) { thread_data, prio, 0, K_NO_WAIT)))) {
BH_FREE(tid); BH_FREE(tid);
BH_FREE(thread_data); BH_FREE(thread_data);
return BHT_ERROR; return BHT_ERROR;
@ -285,7 +288,7 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
korp_tid os_self_thread() korp_tid os_self_thread()
{ {
return (korp_tid) k_current_get(); return (korp_tid)k_current_get();
} }
int os_thread_join(korp_tid thread, void **value_ptr) int os_thread_join(korp_tid thread, void **value_ptr)