mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-07-15 08:48:33 +00:00
Threading examples
This commit is contained in:
parent
20f72ebeae
commit
b2c71bdda8
|
@ -10,4 +10,6 @@ void call_wasm_function();
|
|||
|
||||
void deInit_wasm();
|
||||
|
||||
void thread_function();
|
||||
|
||||
#endif
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.100
Normal file
BIN
samples/wasm-lua-comparison/src/core.100
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.135
Normal file
BIN
samples/wasm-lua-comparison/src/core.135
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.172
Normal file
BIN
samples/wasm-lua-comparison/src/core.172
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.191
Normal file
BIN
samples/wasm-lua-comparison/src/core.191
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.249
Normal file
BIN
samples/wasm-lua-comparison/src/core.249
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.268
Normal file
BIN
samples/wasm-lua-comparison/src/core.268
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.276
Normal file
BIN
samples/wasm-lua-comparison/src/core.276
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.315
Normal file
BIN
samples/wasm-lua-comparison/src/core.315
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.319
Normal file
BIN
samples/wasm-lua-comparison/src/core.319
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.352
Normal file
BIN
samples/wasm-lua-comparison/src/core.352
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.357
Normal file
BIN
samples/wasm-lua-comparison/src/core.357
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.394
Normal file
BIN
samples/wasm-lua-comparison/src/core.394
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.395
Normal file
BIN
samples/wasm-lua-comparison/src/core.395
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.433
Normal file
BIN
samples/wasm-lua-comparison/src/core.433
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.468
Normal file
BIN
samples/wasm-lua-comparison/src/core.468
Normal file
Binary file not shown.
BIN
samples/wasm-lua-comparison/src/core.507
Normal file
BIN
samples/wasm-lua-comparison/src/core.507
Normal file
Binary file not shown.
|
@ -45,5 +45,5 @@ void call_lua_function(lua_State* L)
|
|||
lua_pop(L,1);
|
||||
printf("sum: %d\n", sum);
|
||||
total_t=(double)(stop_t-start_t)/ CLOCKS_PER_SEC;
|
||||
printf("Total time = %f\n", total_t);
|
||||
printf("Lua Total time = %f\n", total_t);
|
||||
}
|
|
@ -18,6 +18,11 @@
|
|||
#include "luaModule.h"
|
||||
#include "wasm.h"
|
||||
|
||||
struct arg_struct {
|
||||
int arg1;
|
||||
int arg2;
|
||||
};
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -38,9 +43,34 @@ main(int argc, char *argv[])
|
|||
stop_t= clock();
|
||||
printf("C sum: %d\n", test);
|
||||
total_t=(double)(stop_t-start_t)/ CLOCKS_PER_SEC;
|
||||
printf("Total time = %f\n", total_t);
|
||||
printf("Native total time = %f\n\n", total_t);
|
||||
|
||||
return 0;
|
||||
printf("Starting thread example \n");
|
||||
pthread_t thread1, thread2, thread3;
|
||||
int iret1, iret2, iret3;
|
||||
struct arg_struct args;
|
||||
args.arg1 = 2;
|
||||
args.arg2 = 3;
|
||||
|
||||
/* Create independent threads each of which will execute function */
|
||||
|
||||
iret1 = pthread_create( &thread1, NULL, call_wasm_function, NULL);
|
||||
iret2 = pthread_create( &thread2, NULL, call_lua_function, L);
|
||||
iret3 = pthread_create( &thread3, NULL, sum, (void*) &args);
|
||||
/* Wait till threads are complete before main continues. Unless we */
|
||||
/* wait we run the risk of executing an exit which will terminate */
|
||||
/* the process and all threads before the threads have completed. */
|
||||
|
||||
pthread_join( thread1, NULL);
|
||||
pthread_join( thread2, NULL);
|
||||
start_t= clock();
|
||||
pthread_join( thread3, NULL);
|
||||
stop_t= clock();
|
||||
printf("C sum: %d\n", test);
|
||||
total_t=(double)(stop_t-start_t)/ CLOCKS_PER_SEC;
|
||||
printf("Native Thread Total time = %f\n", total_t);
|
||||
wasm_thread_function();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int sum(int start, int length)
|
||||
|
@ -53,4 +83,4 @@ int sum(int start, int length)
|
|||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
def sum (start, length):
|
||||
for x in range(0,10000000):
|
||||
sum(x for x in range (start,length))
|
||||
sum(2,3)
|
|
@ -20,10 +20,11 @@ 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_thread_t wasm_tid;
|
||||
uint32 result, sum;
|
||||
wasm_function_inst_t func;
|
||||
char error_buf[128] = { 0 };
|
||||
pthread_t wasm_thread;
|
||||
|
||||
void init_wasm()
|
||||
{
|
||||
|
@ -35,7 +36,6 @@ void init_wasm()
|
|||
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)) {
|
||||
|
@ -68,7 +68,6 @@ void init_wasm()
|
|||
printf("failed to create exec_env\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
func = wasm_runtime_lookup_function(wasm_module_inst, "sum", NULL);
|
||||
if (!func) {
|
||||
printf("failed to lookup function sum");
|
||||
|
@ -105,6 +104,23 @@ void call_wasm_function()
|
|||
stop_t= clock();
|
||||
printf("expect result: %d\n", wasm_argv[0]);
|
||||
total_t=(double)(stop_t-start_t)/ CLOCKS_PER_SEC;
|
||||
printf("Total time = %f\n", total_t);
|
||||
printf("WASM Total time = %f\n", total_t);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
void wasm_thread_function()
|
||||
{
|
||||
clock_t start_t, stop_t;
|
||||
double total_t;
|
||||
wasm_runtime_spawn_thread(exec_env,&wasm_tid, call_wasm_function, NULL);
|
||||
start_t= clock();
|
||||
wasm_runtime_join_thread(wasm_tid, NULL);
|
||||
stop_t= clock();
|
||||
total_t=(double)(stop_t-start_t)/ CLOCKS_PER_SEC;
|
||||
printf("WASM Thread Total time = %f\n", total_t);
|
||||
}
|
||||
// void thread_function(){
|
||||
// char *message = "WASM thread";
|
||||
// //printf("test /n");
|
||||
// pthread_create(&wasm_thread, NULL, call_wasm_function, (void*) message);
|
||||
// }
|
Loading…
Reference in New Issue
Block a user