Add wasm-c-api nested function calls sample (#652)

And enable to copy back the return value of wasm main function when calling wasm_application_execute_main, add license headers in wasm-c-api samples, fix several issues reported by klocwork.
This commit is contained in:
Wenyong Huang
2021-06-16 15:26:28 +08:00
committed by GitHub
parent 1a4aa5ac2f
commit 77c71e559a
15 changed files with 357 additions and 9 deletions

View File

@ -26,6 +26,7 @@ void *thread(void* arg)
func = wasm_runtime_lookup_function(module_inst, "sum", NULL);
if (!func) {
printf("failed to lookup function sum");
return NULL;
}
argv[0] = thread_arg->start;
argv[1] = thread_arg->length;
@ -49,6 +50,7 @@ void *wamr_thread_cb(wasm_exec_env_t exec_env, void *arg)
func = wasm_runtime_lookup_function(module_inst, "sum", NULL);
if (!func) {
printf("failed to lookup function sum");
return NULL;
}
argv[0] = thread_arg->start;
argv[1] = thread_arg->length;
@ -66,7 +68,7 @@ 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 wasm_file_size, wasm_argv[2], i, threads_created;
uint32 stack_size = 16 * 1024, heap_size = 16 * 1024;
wasm_module_t wasm_module = NULL;
wasm_module_inst_t wasm_module_inst = NULL;
@ -123,6 +125,7 @@ int main(int argc, char *argv[])
func = wasm_runtime_lookup_function(wasm_module_inst, "sum", NULL);
if (!func) {
printf("failed to lookup function sum");
goto fail5;
}
wasm_argv[0] = 0;
wasm_argv[1] = THREAD_NUM * 10;
@ -159,17 +162,20 @@ int main(int argc, char *argv[])
if (0 != pthread_create(&tid[i], NULL, thread, &thread_arg[i])) {
printf("failed to create thread.\n");
wasm_runtime_destroy_spawned_exec_env(new_exec_env);
break;
}
}
threads_created = i;
sum = 0;
memset(result, 0, sizeof(uint32) * THREAD_NUM);
for (i = 0; i < THREAD_NUM; i++) {
for (i = 0; i < threads_created; i++) {
pthread_join(tid[i], (void **)&result[i]);
sum += result[i];
/* destroy the spawned exec_env */
if (thread_arg[0].exec_env)
if (thread_arg[i].exec_env)
wasm_runtime_destroy_spawned_exec_env(thread_arg[i].exec_env);
}
@ -191,15 +197,18 @@ int main(int argc, char *argv[])
}
}
threads_created = i;
sum = 0;
memset(result, 0, sizeof(uint32) * THREAD_NUM);
for (i = 0; i < THREAD_NUM; i++) {
for (i = 0; i < threads_created; 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);
fail5:
wasm_runtime_destroy_exec_env(exec_env);
fail4:
@ -217,4 +226,5 @@ fail2:
fail1:
/* destroy runtime environment */
wasm_runtime_destroy();
}
return 0;
}