Skip to content

Commit

Permalink
wasm2c: add multithreaded example
Browse files Browse the repository at this point in the history
  • Loading branch information
keithw committed Nov 19, 2023
1 parent ae7de1d commit 4cc0763
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions wasm2c/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
wasm-rt-impl.o
wasm-rt-exceptions-impl.o
examples/**/*.o
examples/fac/fac
examples/rot13/rot13
Expand All @@ -9,3 +10,7 @@ examples/callback/callback
examples/callback/callback.c
examples/callback/callback.h
examples/callback/callback.wasm
examples/threads/threads
examples/threads/sample.c
examples/threads/sample.h
examples/threads/sample.wasm
20 changes: 20 additions & 0 deletions wasm2c/examples/threads/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#SANITIZERS=-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all
CFLAGS=-I../.. -g -O2 -Wall -Wextra -Wno-unused -Wno-unused-parameter -Wno-array-bounds -Wno-ignored-optimization-argument -Wno-tautological-constant-out-of-range-compare -Wno-infinite-recursion -fno-optimize-sibling-calls -frounding-math -fsignaling-nans ${SANITIZERS} -pthread
LDFLAGS=${SANITIZERS} -pthread

all: threads

threads: threads.o sample.o ../../wasm-rt-impl.o ../../wasm-rt-exceptions-impl.o -lm

clean:
rm -rf threads sample.wasm sample.c sample.h *.o ../../*.o

sample.wasm: sample.wat ../../../bin/wat2wasm
../../../bin/wat2wasm --debug-names $< -o $@

sample.c: sample.wasm ../../../bin/wasm2c
../../../bin/wasm2c $< -o $@

threads.o: sample.c

.PHONY: all clean
6 changes: 6 additions & 0 deletions wasm2c/examples/threads/sample.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
;; Module for demonstrating multi-threaded runtime

(func (export "multiplyby3") (param i32) (result i32) (i32.mul (local.get 0) (i32.const 3)))

(func $stackoverflow (export "stackoverflow")
(call $stackoverflow))
75 changes: 75 additions & 0 deletions wasm2c/examples/threads/threads.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <pthread.h>
#include <stdio.h>

#include "sample.h"
#include "wasm-rt-exceptions.h"
#include "wasm-rt-impl.h"

#define NUM_THREADS 1024

void* do_thread(void* arg) {
int param;
memcpy(&param, arg, sizeof(int));

void* context = wasm_rt_init_thread();

w2c_sample inst;
wasm2c_sample_instantiate(&inst);

wasm_rt_trap_t code = wasm_rt_impl_try();
if (code != 0) {
if (code == WASM_RT_TRAP_OOB || code == WASM_RT_TRAP_EXHAUSTION) {
int returnval = w2c_sample_multiplyby3(&inst, param);
memcpy(arg, &returnval, sizeof(int));

wasm2c_sample_free(&inst);
wasm_rt_free_thread(context);

return arg;
} else {
fprintf(stderr, "Expected OOB or exhaustion trap but got %s\n",
wasm_rt_strerror(code));
return NULL;
}
}

w2c_sample_stackoverflow(&inst);

fprintf(stderr, "Expected trap but did not get one\n");
return NULL;
}

int main(int argc, char** argv) {
pthread_t threads[NUM_THREADS];
int arguments[NUM_THREADS];

/* Initialize the Wasm runtime. */
wasm_rt_init();

for (int i = 0; i < NUM_THREADS; ++i) {
arguments[i] = i;
if (pthread_create(&threads[i], NULL, do_thread, &arguments[i])) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}

for (int i = 0; i < NUM_THREADS; ++i) {
void* retval;
if (pthread_join(threads[i], &retval)) {
perror("pthread_join");
exit(EXIT_FAILURE);
}

if ((retval != &arguments[i]) || (arguments[i] != 3 * i)) {
fprintf(stderr, "Unexpected return value from thread.\n");
exit(EXIT_FAILURE);
}
}

wasm_rt_free();

printf("%d/%d threads trapped successfully.\n", NUM_THREADS, NUM_THREADS);

return EXIT_SUCCESS;
}

0 comments on commit 4cc0763

Please sign in to comment.