diff --git a/core/iwasm/common/wasm_memory.c b/core/iwasm/common/wasm_memory.c index 1f942ec6c3..ce0d92fc1f 100644 --- a/core/iwasm/common/wasm_memory.c +++ b/core/iwasm/common/wasm_memory.c @@ -30,6 +30,7 @@ static mem_allocator_t pool_allocator = NULL; #if WASM_ENABLE_SHARED_HEAP != 0 static WASMSharedHeap *shared_heap_list = NULL; +// TODO: remove this when runtime-instance-feature is ready static korp_mutex shared_heap_list_lock; #endif @@ -78,14 +79,15 @@ align_as_and_cast(uint64 size, uint64 alignment) } static bool -wasm_memory_init_with_pool(void *mem, unsigned int bytes) +wasm_memory_init_with_pool(void *mem, unsigned int bytes, + WASMRuntimeAllocator *allocator) { - mem_allocator_t allocator = mem_allocator_create(mem, bytes); + mem_allocator_t mem_allocator = mem_allocator_create(mem, bytes); - if (allocator) { - memory_mode = MEMORY_MODE_POOL; - pool_allocator = allocator; - global_pool_size = bytes; + if (mem_allocator) { + allocator->memory_mode = MEMORY_MODE_POOL; + allocator->pool_allocator = mem_allocator; + allocator->pool_size = bytes; return true; } LOG_ERROR("Init memory with pool (%p, %u) failed.\n", mem, bytes); @@ -112,13 +114,14 @@ wasm_memory_init_with_allocator(void *_user_data, void *_malloc_func, #else static bool wasm_memory_init_with_allocator(void *malloc_func_ptr, void *realloc_func_ptr, - void *free_func_ptr) + void *free_func_ptr, + WASMRuntimeAllocator *allocator) { if (malloc_func_ptr && free_func_ptr && malloc_func_ptr != free_func_ptr) { - memory_mode = MEMORY_MODE_ALLOCATOR; - malloc_func = malloc_func_ptr; - realloc_func = realloc_func_ptr; - free_func = free_func_ptr; + allocator->memory_mode = MEMORY_MODE_ALLOCATOR; + allocator->malloc_func = malloc_func_ptr; + allocator->realloc_func = realloc_func_ptr; + allocator->free_func = free_func_ptr; return true; } LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n", @@ -509,6 +512,15 @@ wasm_runtime_shared_heap_free(WASMModuleInstanceCommon *module_inst, uint64 ptr) bool wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type, const MemAllocOption *alloc_option) +{ + DEPRECATED_API(wasm_runtime_memory_init, wasm_runtime_memory_init2); + return false; +} + +bool +wasm_runtime_memory_init2(mem_alloc_type_t mem_alloc_type, + const MemAllocOption *alloc_option, + WASMRuntimeAllocator *allocator) { bool ret = false; @@ -519,8 +531,9 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type, #endif if (mem_alloc_type == Alloc_With_Pool) { - ret = wasm_memory_init_with_pool(alloc_option->pool.heap_buf, - alloc_option->pool.heap_size); + ret = + wasm_memory_init_with_pool(alloc_option->pool.heap_buf, + alloc_option->pool.heap_size, allocator); } else if (mem_alloc_type == Alloc_With_Allocator) { ret = wasm_memory_init_with_allocator( @@ -529,10 +542,10 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type, #endif alloc_option->allocator.malloc_func, alloc_option->allocator.realloc_func, - alloc_option->allocator.free_func); + alloc_option->allocator.free_func, allocator); } else if (mem_alloc_type == Alloc_With_System_Allocator) { - memory_mode = MEMORY_MODE_SYSTEM_ALLOCATOR; + allocator->memory_mode = MEMORY_MODE_SYSTEM_ALLOCATOR; ret = true; } else { @@ -580,23 +593,29 @@ destroy_shared_heaps() void wasm_runtime_memory_destroy(void) +{ + DEPRECATED_API(wasm_runtime_memory_destroy, wasm_runtime_memory_destroy2); +} + +void +wasm_runtime_memory_destroy2(WASMRuntimeAllocator *allocator) { #if WASM_ENABLE_SHARED_HEAP != 0 destroy_shared_heaps(); #endif - if (memory_mode == MEMORY_MODE_POOL) { + if (allocator->memory_mode == MEMORY_MODE_POOL) { #if BH_ENABLE_GC_VERIFY == 0 - (void)mem_allocator_destroy(pool_allocator); + (void)mem_allocator_destroy(allocator->pool_allocator); #else - int ret = mem_allocator_destroy(pool_allocator); + int ret = mem_allocator_destroy(allocator->pool_allocator); if (ret != 0) { /* Memory leak detected */ exit(-1); } #endif } - memory_mode = MEMORY_MODE_UNKNOWN; + allocator->memory_mode = MEMORY_MODE_UNKNOWN; } unsigned @@ -609,18 +628,21 @@ wasm_runtime_memory_pool_size(void) } static inline void * -wasm_runtime_malloc_internal(unsigned int size) +wasm_runtime_malloc_internal(WASMRuntimeAllocator *allocator, int size) { - if (memory_mode == MEMORY_MODE_UNKNOWN) { + if (allocator->memory_mode == MEMORY_MODE_UNKNOWN) { LOG_WARNING( "wasm_runtime_malloc failed: memory hasn't been initialized.\n"); return NULL; } - else if (memory_mode == MEMORY_MODE_POOL) { - return mem_allocator_malloc(pool_allocator, size); + + // TODO: use allocator->malloc_func() when we assign variant malloc_func for + // various memory mode + if (allocator->memory_mode == MEMORY_MODE_POOL) { + return mem_allocator_malloc(allocator->pool_allocator, size); } - else if (memory_mode == MEMORY_MODE_ALLOCATOR) { - return malloc_func( + else if (allocator->memory_mode == MEMORY_MODE_ALLOCATOR) { + return allocator->malloc_func( #if WASM_MEM_ALLOC_WITH_USAGE != 0 Alloc_For_Runtime, #endif @@ -664,7 +686,7 @@ wasm_runtime_realloc_internal(void *ptr, unsigned int size) } static inline void -wasm_runtime_free_internal(void *ptr) +wasm_runtime_free_internal(WASMRuntimeAllocator *allocator, void *ptr) { if (!ptr) { LOG_WARNING("warning: wasm_runtime_free with NULL pointer\n"); @@ -674,15 +696,15 @@ wasm_runtime_free_internal(void *ptr) return; } - if (memory_mode == MEMORY_MODE_UNKNOWN) { + if (allocator->memory_mode == MEMORY_MODE_UNKNOWN) { LOG_WARNING("warning: wasm_runtime_free failed: " "memory hasn't been initialize.\n"); } - else if (memory_mode == MEMORY_MODE_POOL) { - mem_allocator_free(pool_allocator, ptr); + else if (allocator->memory_mode == MEMORY_MODE_POOL) { + mem_allocator_free(allocator->pool_allocator, ptr); } else if (memory_mode == MEMORY_MODE_ALLOCATOR) { - free_func( + allocator->free_func( #if WASM_MEM_ALLOC_WITH_USAGE != 0 Alloc_For_Runtime, #endif @@ -699,15 +721,27 @@ wasm_runtime_free_internal(void *ptr) void * wasm_runtime_malloc(unsigned int size) { + WASMRuntimeAllocator *allocator; + + allocator = wasm_runtime_get_local_allocator(); + return wasm_runtime_malloc2(allocator, size); +} + +void * +wasm_runtime_malloc2(WASMRuntimeAllocator *allocator, unsigned int size) +{ + CHECK_NULL_AND_RETURN(allocator, NULL); + if (size == 0) { - LOG_WARNING("warning: wasm_runtime_malloc with size zero\n"); - /* At lease alloc 1 byte to avoid malloc failed */ + LOG_WARNING("warning: wasm_runtime_malloc failed: " + "size is zero.\n"); size = 1; #if BH_ENABLE_GC_VERIFY != 0 exit(-1); #endif } + // TODO: move this to wasm_runtime_malloc_internal() if possible #if WASM_ENABLE_FUZZ_TEST != 0 if (size >= WASM_MEM_ALLOC_MAX_SIZE) { LOG_WARNING("warning: wasm_runtime_malloc with too large size\n"); @@ -715,7 +749,7 @@ wasm_runtime_malloc(unsigned int size) } #endif - return wasm_runtime_malloc_internal(size); + return wasm_runtime_malloc_internal(allocator, size); } void * @@ -727,7 +761,18 @@ wasm_runtime_realloc(void *ptr, unsigned int size) void wasm_runtime_free(void *ptr) { - wasm_runtime_free_internal(ptr); + WASMRuntimeAllocator *allocator; + + allocator = wasm_runtime_get_local_allocator(); + return wasm_runtime_free2(allocator, ptr); +} + +void +wasm_runtime_free2(WASMRuntimeAllocator *allocator, void *ptr) +{ + CHECK_NULL_AND_RETURN_VOID(allocator); + + wasm_runtime_free_internal(allocator, ptr); } bool diff --git a/core/iwasm/common/wasm_memory.h b/core/iwasm/common/wasm_memory.h index bceea0ee43..7ba052d9bf 100644 --- a/core/iwasm/common/wasm_memory.h +++ b/core/iwasm/common/wasm_memory.h @@ -70,6 +70,7 @@ wasm_runtime_shared_heap_free(WASMModuleInstanceCommon *module_inst, uint64 ptr); #endif +// TODO: remove this when all the code is migrated to use the new API bool wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type, const MemAllocOption *alloc_option); @@ -77,6 +78,27 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type, void wasm_runtime_memory_destroy(void); +typedef struct WASMRuntimeAllocator { + uint8 memory_mode; + // TODO: maybe, we can use a unionto save space + + void *pool_allocator; + unsigned int pool_size; + + // TODO: maybe, we can just save the malloc/realloc/free function pointers + void *(*malloc_func)(unsigned int size); + void *(*realloc_func)(void *ptr, unsigned int size); + void (*free_func)(void *ptr); +} WASMRuntimeAllocator; + +bool +wasm_runtime_memory_init2(mem_alloc_type_t mem_alloc_type, + const MemAllocOption *alloc_option, + WASMRuntimeAllocator *allocator); + +void +wasm_runtime_memory_destroy2(WASMRuntimeAllocator *allocator); + unsigned wasm_runtime_memory_pool_size(void); diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index d33c027273..402b4bd202 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -454,7 +454,7 @@ wasm_runtime_get_exec_env_tls() } #endif /* end of OS_ENABLE_HW_BOUND_CHECK */ -static bool +bool wasm_runtime_env_init(void) { if (bh_platform_init() != 0) @@ -600,45 +600,21 @@ static korp_mutex runtime_lock = OS_THREAD_MUTEX_INITIALIZER; #endif static int32 runtime_ref_count = 0; -static bool -wasm_runtime_init_internal(void) -{ - if (!wasm_runtime_memory_init(Alloc_With_System_Allocator, NULL)) - return false; - - if (!wasm_runtime_env_init()) { - wasm_runtime_memory_destroy(); - return false; - } - - return true; -} - bool wasm_runtime_init() { - bool ret = true; - -#if defined(OS_THREAD_MUTEX_INITIALIZER) - os_mutex_lock(&runtime_lock); -#endif + WASMRuntime *runtime; - bh_assert(runtime_ref_count >= 0); - if (runtime_ref_count == 0) { - ret = wasm_runtime_init_internal(); - } - if (ret) { - runtime_ref_count++; + runtime = wasm_runtime_get_local_runtime(); + if (runtime != NULL) { + return true; } -#if defined(OS_THREAD_MUTEX_INITIALIZER) - os_mutex_unlock(&runtime_lock); -#endif - - return ret; + runtime = wasm_runtime_init2(); + return runtime != NULL; } -static void +void wasm_runtime_destroy_internal(void) { #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 @@ -697,25 +673,18 @@ wasm_runtime_destroy_internal(void) wasm_native_destroy(); bh_platform_destroy(); - wasm_runtime_memory_destroy(); + // TODO: always make sure you will call wasm_runtime_memory_destroyX() after + // wasm_runtime_destroy() to avoid memory leak + // wasm_runtime_memory_destroy(); } void wasm_runtime_destroy() { -#if defined(OS_THREAD_MUTEX_INITIALIZER) - os_mutex_lock(&runtime_lock); -#endif + WASMRuntime *runtime; - bh_assert(runtime_ref_count > 0); - runtime_ref_count--; - if (runtime_ref_count == 0) { - wasm_runtime_destroy_internal(); - } - -#if defined(OS_THREAD_MUTEX_INITIALIZER) - os_mutex_unlock(&runtime_lock); -#endif + runtime = wasm_runtime_get_local_runtime(); + wasm_runtime_destroy2(runtime); } RunningMode @@ -740,13 +709,9 @@ wasm_runtime_get_gc_heap_size_default(void) } #endif -static bool +bool wasm_runtime_full_init_internal(RuntimeInitArgs *init_args) { - if (!wasm_runtime_memory_init(init_args->mem_alloc_type, - &init_args->mem_alloc_option)) - return false; - if (!wasm_runtime_set_default_running_mode(init_args->running_mode)) { wasm_runtime_memory_destroy(); return false; @@ -809,25 +774,17 @@ wasm_runtime_full_init_internal(RuntimeInitArgs *init_args) bool wasm_runtime_full_init(RuntimeInitArgs *init_args) { - bool ret = true; + WASMRuntime *runtime; -#if defined(OS_THREAD_MUTEX_INITIALIZER) - os_mutex_lock(&runtime_lock); -#endif + CHECK_NULL_AND_RETURN(init_args, false); - bh_assert(runtime_ref_count >= 0); - if (runtime_ref_count == 0) { - ret = wasm_runtime_full_init_internal(init_args); - } - if (ret) { - runtime_ref_count++; + runtime = wasm_runtime_get_local_runtime(); + if (runtime != NULL) { + return true; } -#if defined(OS_THREAD_MUTEX_INITIALIZER) - os_mutex_unlock(&runtime_lock); -#endif - - return ret; + runtime = wasm_runtime_full_init2(init_args); + return runtime != NULL; } void diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index 8ac032bf8a..76d544d007 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -11,6 +11,7 @@ #include "wasm_exec_env.h" #include "wasm_native.h" #include "../include/wasm_export.h" +#include "../include/wasm_export2.h" #include "../interpreter/wasm.h" #if WASM_ENABLE_GC != 0 #include "gc/gc_object.h" @@ -24,6 +25,10 @@ #endif #endif +#ifndef WASM_RUNTIME_API_INTERN +#define WASM_RUNTIME_API_INTERN +#endif /* WASM_RUNTIME_API_INTERN*/ + #ifdef __cplusplus extern "C" { #endif @@ -484,6 +489,8 @@ LOAD_I16(void *addr) #define CLAMP_U64_TO_U32(value) \ ((value) > UINT32_MAX ? UINT32_MAX : (uint32)(value)) +typedef struct WASMRuntime WASMRuntime; + typedef struct WASMModuleCommon { /* Module type, for module loaded from WASM bytecode binary, this field is Wasm_Module_Bytecode, and this structure should @@ -1328,6 +1335,54 @@ void wasm_runtime_set_linux_perf(bool flag); #endif +/* ============================================================ */ + +// TODO: need to be rearranged after the new runtime API is ready + +#define DEPRECATED_API(old, new) \ + do { \ + bh_assert(false \ + && "Warning:" #old \ + "is deprecated, please use " #new " instead"); \ + } while (0) + +#define CHECK_AND_RETURN_VOID(cond) \ + do { \ + if (!(cond)) { \ + return; \ + } \ + } while (0) + +#define CHECK_AND_RETURN(cond, ret) \ + do { \ + if (!(cond)) { \ + return (ret); \ + } \ + } while (0) + +#define CHECK_NULL_AND_RETURN_VOID(var) CHECK_AND_RETURN_VOID((var) != NULL) +#define CHECK_NULL_AND_RETURN(var, ret) CHECK_AND_RETURN((var) != NULL, ret) + +typedef struct WASMRuntimeAllocator WASMRuntimeAllocator; + +WASM_RUNTIME_API_INTERN WASMRuntime * +wasm_runtime_get_local_runtime(void); + +WASM_RUNTIME_API_INTERN +bool +wasm_runtime_env_init(void); + +WASM_RUNTIME_API_INTERN void +wasm_runtime_destroy_internal(void); + +WASM_RUNTIME_API_INTERN +bool +wasm_runtime_full_init_internal(RuntimeInitArgs *init_args); + +WASM_RUNTIME_API_INTERN +WASMRuntimeAllocator * +wasm_runtime_get_local_allocator(); + #ifdef __cplusplus } #endif diff --git a/core/iwasm/common/wasm_runtime_impl.c b/core/iwasm/common/wasm_runtime_impl.c new file mode 100644 index 0000000000..eb1b120585 --- /dev/null +++ b/core/iwasm/common/wasm_runtime_impl.c @@ -0,0 +1,207 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "wasm_runtime_common.h" +#include "wasm_memory.h" + +typedef struct WASMRuntime { + // TODO: do we really need this? + int32 ref_count; + WASMRuntimeAllocator allocator; +} WASMRuntime; + +/* SHOULD BE the only global variable */ +static korp_key runtime_key; + +static WASMRuntime * +wasm_runtime_init_prologue(void) +{ + WASMRuntimeAllocator allocator; + bool ret; + int ret_i; + WASMRuntime *runtime; + + LOG_DEBUG("WASMRuntime init prologue"); + + // TODO: Enable loging system + LOG_DEBUG("WASMRuntime init setup log"); + + /* Initialize memory allocator firstly */ + LOG_DEBUG("WASMRuntime init setup memory allocator"); + ret = wasm_runtime_memory_init2(Alloc_With_Pool, NULL, &allocator); + if (!ret) { + /* Failed to initialize memory allocator */ + LOG_ERROR("Failed to initialize memory allocator"); + return NULL; + } + + /* Now it is able to use allocation */ + LOG_DEBUG("WASMRuntime init allocate memory for runtime"); + runtime = wasm_runtime_malloc2(&allocator, sizeof(WASMRuntime)); + if (!runtime) { + /* Failed to allocate memory for runtime */ + LOG_ERROR("Failed to allocate memory for runtime"); + wasm_runtime_memory_destroy2(&allocator); + return NULL; + } + + // TODO: remove me if not needed like wasm_runtime_malloc2() will do this + LOG_DEBUG("WASMRuntime init initialize runtime"); + memset(runtime, 0, sizeof(WASMRuntime)); + runtime->allocator = allocator; + + LOG_DEBUG("WASMRuntime init set thread local key"); + ret_i = os_thread_setspecific(runtime_key, runtime); + if (ret_i != 0) { + /* Failed to set thread local key */ + LOG_ERROR("Failed to set thread local key"); + wasm_runtime_free2(&allocator, runtime); + wasm_runtime_memory_destroy2(&allocator); + return NULL; + } + + return runtime; +} + +static void +wasm_runtime_destroy_epilogue(WASMRuntime *runtime) +{ + WASMRuntimeAllocator allocator; + + LOG_DEBUG("WASMRuntime destroy epilogue(%d)", runtime->ref_count); + bh_assert(runtime->ref_count == 0); + + allocator = runtime->allocator; + + /* Destroy thread local key */ + LOG_DEBUG("WASMRuntime destroy thread local key"); + os_thread_key_delete(runtime_key); + + /* Free the runtime */ + LOG_DEBUG("WASMRuntime destroy memory for runtime"); + wasm_runtime_free2(&allocator, runtime); + wasm_runtime_memory_destroy2(&allocator); +} + +/* =========================== Exported Functions =========================== */ + +/* + * make sure you call this function in the main thread at the beginning + * of the program + */ +int +wasm_runtime_create_runtime_key() +{ + return os_thread_key_create(&runtime_key, wasm_runtime_destroy2); +} + +WASMRuntime * +wasm_runtime_get_local_runtime() +{ + WASMRuntime *runtime; + + runtime = os_thread_getspecific(runtime_key); + if (!runtime) { + return NULL; + } + + LOG_DEBUG("WASMRuntime ref_count(%d)", runtime->ref_count); + runtime->ref_count++; + return runtime; +} + +WASMRuntime * +wasm_runtime_init2(void) +{ + WASMRuntime *runtime; + bool ret; + + LOG_DEBUG("WASMRuntime init"); + + runtime = wasm_runtime_init_prologue(); + if (!runtime) { + /* Failed to initialize runtime */ + LOG_ERROR("Failed to prepare runtime"); + return NULL; + } + + /*TODO: goes to wasm_runtime_full_init_internal(RuntimeInitArgs*) with + * default args*/ + ret = wasm_runtime_env_init(); + if (!ret) { + /* Failed to initialize runtime environment */ + LOG_ERROR("Failed to initialize runtime environment"); + wasm_runtime_destroy_epilogue(runtime); + return NULL; + } + + return runtime; +} + +WASMRuntime * +wasm_runtime_full_init2(RuntimeInitArgs *init_args) +{ + WASMRuntime *runtime; + bool ret; + + LOG_DEBUG("WASMRuntime full init"); + + runtime = wasm_runtime_init_prologue(); + if (!runtime) { + /* Failed to initialize runtime */ + LOG_ERROR("Failed to prepare runtime"); + return NULL; + } + + ret = wasm_runtime_full_init_internal(init_args); + if (!ret) { + /* Failed to fully initialize runtime */ + LOG_ERROR("Failed to fully initialize runtime with args"); + wasm_runtime_destroy_epilogue(runtime); + return NULL; + } + + return runtime; +} + +void +wasm_runtime_destroy2(void *data) +{ + CHECK_NULL_AND_RETURN_VOID(data); + + int ret; + WASMRuntime *runtime; + WASMRuntimeAllocator allocator; + + runtime = (WASMRuntime *)data; + allocator = runtime->allocator; + + LOG_DEBUG("WASMRuntime destroy(%d)", runtime->ref_count); + + if (runtime->ref_count > 0) { + /* Not the last one, just return */ + LOG_DEBUG("WASMRuntime ref_count(%d) > 0", runtime->ref_count); + runtime->ref_count--; + return; + } + + if (runtime->ref_count < 0) { + /* This is an error, should not happen */ + LOG_WARNING("WASMRuntime ref_count < 0"); + return; + } + + LOG_DEBUG("WASMRuntime ref_count = 0, free it"); + + wasm_runtime_destroy_internal(); + wasm_runtime_destroy_epilogue(runtime); +} + +WASMRuntimeAllocator * +wasm_runtime_get_local_allocator(WASMRuntime *runtime) +{ + CHECK_NULL_AND_RETURN(runtime, NULL); + return &runtime->allocator; +} diff --git a/core/iwasm/include/wasm_export2.h b/core/iwasm/include/wasm_export2.h index 2b6c553b1b..94333c8da4 100644 --- a/core/iwasm/include/wasm_export2.h +++ b/core/iwasm/include/wasm_export2.h @@ -6,16 +6,17 @@ #ifndef _WASM_EXPORT_2_H #define _WASM_EXPORT_2_H -#include "wasm_export.h" - #ifdef __cplusplus extern "C" { #endif -/* Runtime structure */ +/* Forward Declarations */ struct WASMRuntime; typedef struct WASMRuntime WASMRuntime; +struct WASMRuntimeAllocator; +typedef struct WASMRuntimeAllocator WASMRuntimeAllocator; + /** * Initialize the WASM runtime environment, and also initialize * the memory allocator with system allocator, which calls os_malloc @@ -24,8 +25,8 @@ typedef struct WASMRuntime WASMRuntime; * @param runtime the runtime context * @return true if success, false otherwise */ -WASM_RUNTIME_API_EXTERN bool -wasm_runtime_init2(WASMRuntime *runtime); +WASM_RUNTIME_API_EXTERN WASMRuntime * +wasm_runtime_init2(void); /** * Initialize the WASM runtime environment, WASM running mode, @@ -37,8 +38,8 @@ wasm_runtime_init2(WASMRuntime *runtime); * * @return return true if success, false otherwise */ -WASM_RUNTIME_API_EXTERN bool -wasm_runtime_full_init2(WASMRuntime *runtime, RuntimeInitArgs *init_args); +WASM_RUNTIME_API_EXTERN WASMRuntime * +wasm_runtime_full_init2(RuntimeInitArgs *init_args); /** * Set the log level. To be called after the runtime is initialized. @@ -81,7 +82,7 @@ wasm_runtime_set_default_running_mode2(WASMRuntime *runtime, * @param runtime the runtime context */ WASM_RUNTIME_API_EXTERN void -wasm_runtime_destroy2(WASMRuntime *runtime); +wasm_runtime_destroy2(void *runtime); /** * Allocate memory from runtime memory environment. @@ -92,7 +93,7 @@ wasm_runtime_destroy2(WASMRuntime *runtime); * @return the pointer to memory allocated */ WASM_RUNTIME_API_EXTERN void * -wasm_runtime_malloc2(WASMRuntime *runtime, unsigned int size); +wasm_runtime_malloc2(WASMRuntimeAllocator *allocator, unsigned int size); /** * Reallocate memory from runtime memory environment @@ -113,7 +114,7 @@ wasm_runtime_realloc2(WASMRuntime *runtime, void *ptr, unsigned int size); * @param ptr the pointer to memory */ WASM_RUNTIME_API_EXTERN void -wasm_runtime_free2(WASMRuntime *runtime, void *ptr); +wasm_runtime_free2(WASMRuntimeAllocator *allocator, void *ptr); /** * Get memory info, only pool mode is supported now. diff --git a/core/shared/platform/common/posix/posix_thread.c b/core/shared/platform/common/posix/posix_thread.c index 80b7d6545e..7496e3c5c5 100644 --- a/core/shared/platform/common/posix/posix_thread.c +++ b/core/shared/platform/common/posix/posix_thread.c @@ -781,4 +781,28 @@ os_sigreturn() #endif #endif } + +int +os_thread_key_create(korp_key *key, void (*destructor)(void *)) +{ + return pthread_key_create(key, destructor); +} + +int +os_thread_key_delete(korp_key key) +{ + return pthread_key_delete(key); +} + +void * +os_thread_getspecific(korp_key key) +{ + return pthread_getspecific(key); +} + +int +os_thread_setspecific(korp_key key, const void *value) +{ + return pthread_setspecific(key, value); +} #endif /* end of OS_ENABLE_HW_BOUND_CHECK */ diff --git a/core/shared/platform/include/platform_api_extension.h b/core/shared/platform/include/platform_api_extension.h index d57d0eac7c..ad2dd85937 100644 --- a/core/shared/platform/include/platform_api_extension.h +++ b/core/shared/platform/include/platform_api_extension.h @@ -403,6 +403,18 @@ os_end_blocking_op(void); int os_wakeup_blocking_op(korp_tid tid); +int +os_thread_key_create(korp_key *key, void (*destructor)(void *)); + +int +os_thread_key_delete(korp_key key); + +void * +os_thread_getspecific(korp_key key); + +int +os_thread_setspecific(korp_key key, const void *value); + /**************************************************** * Section 2 * * Socket support * diff --git a/core/shared/platform/linux/platform_internal.h b/core/shared/platform/linux/platform_internal.h index 865180273e..4776c8c664 100644 --- a/core/shared/platform/linux/platform_internal.h +++ b/core/shared/platform/linux/platform_internal.h @@ -57,6 +57,7 @@ typedef pthread_cond_t korp_cond; typedef pthread_t korp_thread; typedef pthread_rwlock_t korp_rwlock; typedef sem_t korp_sem; +typedef pthread_key_t korp_key; #define OS_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER