add spawn thread API and sample (#333)

This commit is contained in:
Xu Jun 2020-08-04 17:40:45 +08:00 committed by GitHub
parent ed8ddb2cea
commit 2db335c6d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 591 additions and 7 deletions

View File

@ -234,6 +234,10 @@ wasm_runtime_full_init(RuntimeInitArgs *init_args)
return false;
}
#if WASM_ENABLE_THREAD_MGR != 0
wasm_cluster_set_max_thread_num(init_args->max_thread_num);
#endif
return true;
}
@ -2850,3 +2854,77 @@ wasm_runtime_call_indirect(WASMExecEnv *exec_env,
#endif
return false;
}
#if WASM_ENABLE_THREAD_MGR != 0
typedef struct WASMThreadArg {
WASMExecEnv *new_exec_env;
wasm_thread_callback_t callback;
void *arg;
} WASMThreadArg;
WASMExecEnv *
wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
{
return wasm_cluster_spawn_exec_env(exec_env);
}
void
wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
{
wasm_cluster_destroy_spawned_exec_env(exec_env);
}
static void*
wasm_runtime_thread_routine(void *arg)
{
WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
void *ret;
bh_assert(thread_arg->new_exec_env);
ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
wasm_runtime_free(thread_arg);
os_thread_exit(ret);
return ret;
}
int32
wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
wasm_thread_callback_t callback, void *arg)
{
WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
WASMThreadArg *thread_arg;
int32 ret;
if (!new_exec_env)
return -1;
if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
wasm_runtime_destroy_spawned_exec_env(new_exec_env);
return -1;
}
thread_arg->new_exec_env = new_exec_env;
thread_arg->callback = callback;
thread_arg->arg = arg;
ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
if (ret != 0) {
wasm_runtime_destroy_spawned_exec_env(new_exec_env);
wasm_runtime_free(thread_arg);
}
return ret;
}
int32
wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
{
return os_thread_join((korp_tid)tid, retval);
}
#endif

View File

@ -114,6 +114,10 @@ typedef struct RuntimeInitArgs {
const char *native_module_name;
NativeSymbol *native_symbols;
uint32_t n_native_symbols;
/* maximum thread number, only used when
WASM_ENABLE_THREAD_MGR is defined */
uint32_t max_thread_num;
} RuntimeInitArgs;
/**
@ -684,6 +688,11 @@ void *
wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
#if WASM_ENABLE_THREAD_MGR != 0
/* wasm thread callback function type */
typedef void* (*wasm_thread_callback_t)(wasm_exec_env_t, void *);
/* wasm thread type */
typedef uintptr_t wasm_thread_t;
/**
* Set the max thread num per cluster.
*
@ -691,6 +700,50 @@ wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
*/
void
wasm_runtime_set_max_thread_num(uint32_t num);
/**
* spawn a new exec_env, the spawned exec_env
* can be used in other threads
*
* @param num the original exec_env
*
* @return the spawned exec_env if success, NULL otherwise
*/
wasm_exec_env_t
wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
/**
* Destroy the spawned exec_env
*
* @param exec_env the spawned exec_env
*/
void
wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
/**
* spawn a thread from the given exec_env
*
* @param exec_env the original exec_env
* @param tid thread id to be returned to the caller
* @param callback the callback function provided by the user
* @param arg the arguments passed to the callback
*
* @return 0 if success, -1 otherwise
*/
int32_t
wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
wasm_thread_callback_t callback, void *arg);
/**
* waits a spawned thread to terminate
*
* @param tid thread id
* @param retval if not NULL, output the return value of the thread
*
* @return 0 if success, error number otherwise
*/
int32_t
wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
#endif
#ifdef __cplusplus

View File

@ -27,7 +27,8 @@ static uint32 cluster_max_thread_num = CLUSTER_MAX_THREAD_NUM;
void
wasm_cluster_set_max_thread_num(uint32 num)
{
cluster_max_thread_num = num;
if (num > 0)
cluster_max_thread_num = num;
}
bool
@ -278,6 +279,70 @@ wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
return ret;
}
WASMExecEnv *
wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
{
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
wasm_module_t module = wasm_exec_env_get_module(exec_env);
wasm_module_inst_t new_module_inst;
WASMExecEnv *new_exec_env;
uint32 aux_stack_start, aux_stack_size;
if (!(new_module_inst =
wasm_runtime_instantiate_internal(module, true, 8192,
0, NULL, 0))) {
return NULL;
}
new_exec_env = wasm_exec_env_create_internal(
new_module_inst, exec_env->wasm_stack_size);
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;
}
/* Set aux stack for current thread */
if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
aux_stack_size)) {
goto fail3;
}
if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
goto fail3;
return new_exec_env;
fail3:
/* free the allocated aux stack space */
free_aux_stack(cluster, aux_stack_start);
fail2:
wasm_exec_env_destroy(new_exec_env);
fail1:
wasm_runtime_deinstantiate_internal(new_module_inst, true);
return NULL;
}
void
wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
{
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
wasm_module_inst_t module_inst = wasm_runtime_get_module_inst(exec_env);
bh_assert(cluster != NULL);
/* Free aux stack space */
free_aux_stack(cluster,
exec_env->aux_stack_boundary + cluster->stack_size);
wasm_cluster_del_exec_env(cluster, exec_env);
wasm_exec_env_destroy_internal(exec_env);
wasm_runtime_deinstantiate_internal(module_inst, true);
}
/* start routine of thread manager */
static void*
thread_manager_start_routine(void *arg)

View File

@ -109,6 +109,12 @@ wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env);
void
wasm_cluster_spread_exception(WASMExecEnv *exec_env);
WASMExecEnv *
wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env);
void
wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env);
#ifdef __cplusplus
}
#endif

View File

@ -22,7 +22,7 @@ Embedding WAMR guideline
// read WASM file into a memory buffer
buffer = read_wasm_binary_to_buffer(…, &size);
// Add it below if runtime needs to export native functions to WASM APP
// Add it below if runtime needs to export native functions to WASM APP
// wasm_runtime_register_natives(...)
// parse the WASM file from buffer and create a WASM module
@ -81,7 +81,7 @@ After a module is instantiated, the runtime native can lookup WASM functions by
```c
unit32 argv[2];
// lookup a WASM function by its name.
// lookup a WASM function by its name.
// The function signature can NULL here
func = wasm_runtime_lookup_function(module_inst, "fib", NULL);
@ -128,7 +128,7 @@ The parameters are transferred in an array of 32 bits elements. For parameters t
// arg3 and arg4 each takes 2 elements
//
wasm_runtime_call_wasm(exec_env, func, 6, argv);
// if the return value is type of 8 bytes, it takes
// the first two array elements
memcpy(&ret, &argv[0], sizeof(ret));
@ -193,7 +193,7 @@ if(buffer_for_wasm != 0)
argv[0] = buffer_for_wasm; // pass the buffer address for WASM space.
argv[1] = 100; // the size of buffer
wasm_runtime_call_wasm(exec_env, func, 2, argv);
// it is runtime responsibility to release the memory,
// unless the WASM app will free the passed pointer in its code
wasm_runtime_module_free(module_inst, buffer_for_wasm);
@ -209,6 +209,73 @@ We can't pass structure data or class objects through the pointer since the memo
## Execute wasm functions in multiple threads
The `exec_env` is not thread safety, it will cause unexpected behavior if the same `exec_env` is used in multiple threads. However, we've provided two ways to execute wasm functions concurrently:
- You can use `pthread` APIs in your wasm application, see [pthread library](./pthread_library.md) for more details.
- The `spawn exec_env` and `spawn thread` APIs are available, you can use these APIs to manage the threads in native:
*spawn exec_env:*
`spawn exec_env` API spawn a `new_exec_env` base on the original `exec_env`, use can use it in other threads:
```C
new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
/* Then you can use new_exec_env in your new thread */
module_inst = wasm_runtime_get_module_inst(new_exec_env);
func_inst = wasm_runtime_lookup_function(module_inst, ...);
wasm_runtime_call_wasm(new_exec_env, func_inst, ...);
/* you need to use this API to manually destroy the spawned exec_env */
wasm_runtime_destroy_spawned_exec_env(new_exec_env);
```
*spawn thread:*
You can also use `spawn thread` API to avoid manually manage the spawned exec_env:
```C
wasm_thread_t wasm_tid;
void *wamr_thread_cb(wasm_exec_env_t exec_env, void *arg)
{
module_inst = wasm_runtime_get_module_inst(exec_env);
func_inst = wasm_runtime_lookup_function(module_inst, ...);
wasm_runtime_call_wasm(exec_env, func_inst, ...);
}
wasm_runtime_spawn_thread(exec_env, &wasm_tid, wamr_thread_cb, NULL);
/* Use wasm_runtime_join_thread to join the spawned thread */
wasm_runtime_join_thread(wasm_tid, NULL);
```
**Note1: You can manage the maximum number of threads can be created:**
```C
init_args.max_thread_num = THREAD_NUM;
/* If this init argument is not set, the default maximum thread number is 4 */
```
**Note2: The wasm application should be built with `--shared-memory` and `-pthread` enabled:**
```bash
/opt/wasi-sdk/bin/clang -o test.wasm test.c -nostdlib -pthread \
-Wl,--shared-memory,--max-memory=131072 \
-Wl,--no-entry,--export=__heap_base,--export=__data_end \
-Wl,--export=__wasm_call_ctors,--export=${your_func_name}
```
**Note3: The pthread library feature should be enabled while building the runtime:**
```bash
cmake .. -DWAMR_BUILD_LIB_PTHREAD=1
```
[Here](../samples/spawn-thread) is a sample to show how to use these APIs.
## The deinitialization procedure
```

View File

@ -116,7 +116,7 @@ The default value of N is 4, which means you can create 4 threads at most. This
``` bash
./iwasm --max-threads=n test.wasm
```
If you are going to develop your own runtime product, you can use the API `wasm_runtime_set_max_thread_num` to set the value, or you can change the macro `CLUSTER_MAX_THREAD_NUM` in [config.h](../core/config.h),
If you are going to develop your own runtime product, you can use the API `wasm_runtime_set_max_thread_num` or init arg `init_args.max_thread_num` to set the value, or you can change the macro `CLUSTER_MAX_THREAD_NUM` in [config.h](../core/config.h).
> Note: the total size of aux stack reserved by compiler can be set with `-z stack-size` option during compilation. If you need to create more threads, please set a larger value, otherwise it is easy to cause aux stack overflow.

View File

@ -14,7 +14,7 @@ set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
# WAMR features switch
set(WAMR_BUILD_TARGET "X86_64")
set(WAMR_BUILD_INTERP 1)
set(WAMR_BUILD_AOT 0)
set(WAMR_BUILD_AOT 1)
set(WAMR_BUILD_JIT 0)
set(WAMR_BUILD_LIBC_BUILTIN 1)
set(WAMR_BUILD_FAST_INTERP 1)

View File

@ -0,0 +1,46 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required(VERSION 2.8)
project(spawn_thread)
################ runtime settings ################
set(WAMR_BUILD_PLATFORM "linux")
# Resetdefault linker flags
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
# WAMR features switch
set(WAMR_BUILD_TARGET "X86_64")
set(WAMR_BUILD_INTERP 1)
set(WAMR_BUILD_AOT 1)
set(WAMR_BUILD_JIT 0)
set(WAMR_BUILD_LIBC_BUILTIN 1)
set(WAMR_BUILD_FAST_INTERP 1)
set(WAMR_BUILD_LIB_PTHREAD 1)
# compiling and linking flags
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -pie -fPIE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
# build out vmlib
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
################################################
################ wasm application ################
add_subdirectory(wasm-apps)
################ wamr runtime ################
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
set (RUNTIME_SOURCE_ALL
${CMAKE_CURRENT_LIST_DIR}/src/main.c
${UNCOMMON_SHARED_SOURCE}
)
add_executable (spawn_thread ${RUNTIME_SOURCE_ALL})
target_link_libraries(spawn_thread vmlib -lpthread -lm)

View File

@ -0,0 +1,220 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_export.h"
#include "bh_read_file.h"
#include "pthread.h"
#define THREAD_NUM 10
typedef struct ThreadArgs {
wasm_exec_env_t exec_env;
int start;
int length;
} ThreadArgs;
void *thread(void* arg)
{
ThreadArgs *thread_arg = (ThreadArgs *)arg;
wasm_exec_env_t exec_env = thread_arg->exec_env;
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasm_function_inst_t func;
uint32 argv[2];
func = wasm_runtime_lookup_function(module_inst, "sum", NULL);
if (!func) {
printf("failed to lookup function sum");
}
argv[0] = thread_arg->start;
argv[1] = thread_arg->length;
/* call the WASM function */
if (!wasm_runtime_call_wasm(exec_env, func, 2, argv)) {
printf("%s\n", wasm_runtime_get_exception(module_inst));
return NULL;
}
return (void *)(uintptr_t)argv[0];
}
void *wamr_thread_cb(wasm_exec_env_t exec_env, void *arg)
{
ThreadArgs *thread_arg = (ThreadArgs *)arg;
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasm_function_inst_t func;
uint32 argv[2];
func = wasm_runtime_lookup_function(module_inst, "sum", NULL);
if (!func) {
printf("failed to lookup function sum");
}
argv[0] = thread_arg->start;
argv[1] = thread_arg->length;
/* call the WASM function */
if (!wasm_runtime_call_wasm(exec_env, func, 2, argv)) {
printf("%s\n", wasm_runtime_get_exception(module_inst));
return NULL;
}
return (void *)(uintptr_t)argv[0];
}
int main(int argc, char *argv[])
{
char *wasm_file = "wasm-apps/test.wasm";
uint8 *wasm_file_buf = NULL;
uint32 wasm_file_size, wasm_argv[2], i;
uint32 stack_size = 16 * 1024, heap_size = 16 * 1024;
wasm_module_t wasm_module = NULL;
wasm_module_inst_t wasm_module_inst = NULL;
wasm_exec_env_t exec_env = NULL;
RuntimeInitArgs init_args;
ThreadArgs thread_arg[THREAD_NUM];
pthread_t tid[THREAD_NUM];
wasm_thread_t wasm_tid[THREAD_NUM];
uint32 result[THREAD_NUM], sum;
wasm_function_inst_t func;
char error_buf[128] = { 0 };
memset(thread_arg, 0, sizeof(ThreadArgs) * THREAD_NUM);
memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = malloc;
init_args.mem_alloc_option.allocator.realloc_func = realloc;
init_args.mem_alloc_option.allocator.free_func = free;
init_args.max_thread_num = THREAD_NUM;
/* initialize runtime environment */
if (!wasm_runtime_full_init(&init_args)) {
printf("Init runtime environment failed.\n");
return -1;
}
/* load WASM byte buffer from WASM bin file */
if (!(wasm_file_buf =
(uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
goto fail1;
/* load WASM module */
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
error_buf, sizeof(error_buf)))) {
printf("%s\n", error_buf);
goto fail2;
}
/* instantiate the module */
if (!(wasm_module_inst =
wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
error_buf, sizeof(error_buf)))) {
printf("%s\n", error_buf);
goto fail3;
}
/* Create the first exec_env */
if (!(exec_env =
wasm_runtime_create_exec_env(wasm_module_inst, stack_size))) {
printf("failed to create exec_env\n");
goto fail4;
}
func = wasm_runtime_lookup_function(wasm_module_inst, "sum", NULL);
if (!func) {
printf("failed to lookup function sum");
}
wasm_argv[0] = 0;
wasm_argv[1] = THREAD_NUM * 10;
/*
* Execute the wasm function in current thread, get the expect result
*/
if (!wasm_runtime_call_wasm(exec_env, func, 2, wasm_argv)) {
printf("%s\n", wasm_runtime_get_exception(wasm_module_inst));
}
printf("expect result: %d\n", wasm_argv[0]);
/*
* Run wasm function in multiple thread created by pthread_create
*/
memset(thread_arg, 0, sizeof(ThreadArgs) * THREAD_NUM);
for (i = 0; i < THREAD_NUM; i++) {
wasm_exec_env_t new_exec_env;
thread_arg[i].start = 10 * i;
thread_arg[i].length = 10;
/* spawn a new exec_env to be executed in other threads */
new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
if (new_exec_env)
thread_arg[i].exec_env = new_exec_env;
else {
printf("failed to spawn exec_env\n");
break;
}
/* If we use:
thread_arg[i].exec_env = exec_env,
we may get wrong result */
if (0 != pthread_create(&tid[i], NULL, thread, &thread_arg[i])) {
printf("failed to create thread.\n");
break;
}
}
sum = 0;
memset(result, 0, sizeof(uint32) * THREAD_NUM);
for (i = 0; i < THREAD_NUM; i++) {
pthread_join(tid[i], (void **)&result[i]);
sum += result[i];
/* destroy the spawned exec_env */
if (thread_arg[0].exec_env)
wasm_runtime_destroy_spawned_exec_env(thread_arg[i].exec_env);
}
printf("[pthread]sum result: %d\n", sum);
/*
* Run wasm function in multiple thread created by wamr spawn API
*/
memset(thread_arg, 0, sizeof(ThreadArgs) * THREAD_NUM);
for (i = 0; i < THREAD_NUM; i++) {
thread_arg[i].start = 10 * i;
thread_arg[i].length = 10;
/* No need to spawn exec_env manually */
if (0 != wasm_runtime_spawn_thread(exec_env, &wasm_tid[i],
wamr_thread_cb, &thread_arg[i])) {
printf("failed to spawn thread.\n");
break;
}
}
sum = 0;
memset(result, 0, sizeof(uint32) * THREAD_NUM);
for (i = 0; i < THREAD_NUM; i++) {
wasm_runtime_join_thread(wasm_tid[i], (void**)&result[i]);
sum += result[i];
/* No need to destroy the spawned exec_env */
}
printf("[spwan_thread]sum result: %d\n", sum);
wasm_runtime_destroy_exec_env(exec_env);
fail4:
/* destroy the module instance */
wasm_runtime_deinstantiate(wasm_module_inst);
fail3:
/* unload the module */
wasm_runtime_unload(wasm_module);
fail2:
/* free the file buffer */
wasm_runtime_free(wasm_file_buf);
fail1:
/* destroy runtime environment */
wasm_runtime_destroy();
}

View File

@ -0,0 +1,34 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required(VERSION 2.8)
project(wasm-apps)
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR wasm32)
set (CMAKE_SYSROOT ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot)
if (NOT DEFINED WASI_SDK_DIR)
set (WASI_SDK_DIR "/opt/wasi-sdk")
endif ()
set (CMAKE_C_FLAGS "-nostdlib -pthread -Qunused-arguments")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -z stack-size=32768")
set (CMAKE_C_COMPILER_TARGET "wasm32")
set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
set (DEFINED_SYMBOLS
"${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt")
set (CMAKE_EXE_LINKER_FLAGS
"-Wl,--shared-memory,--max-memory=131072, \
-Wl,--no-entry,--strip-all,--export=sum, \
-Wl,--export=__heap_base,--export=__data_end \
-Wl,--export=__wasm_call_ctors \
-Wl,--allow-undefined-file=${DEFINED_SYMBOLS}"
)
add_executable(test.wasm sum.c)
target_link_libraries(test.wasm)

View File

@ -0,0 +1,15 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
int sum(int start, int length)
{
int sum = 0, i;
for (i = start; i < start + length; i++) {
sum += i;
}
return sum;
}