From a0421f85c7f39cb5f08706e227722cbf0281e922 Mon Sep 17 00:00:00 2001 From: Marcin Kolny Date: Wed, 30 Nov 2022 23:04:32 +0000 Subject: [PATCH] Add thread spawn test --- test/build.sh | 12 +++++ test/testsuite/.gitignore | 1 + test/testsuite/manifest.json | 3 ++ test/testsuite/thread_spawn-simple.c | 71 ++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100755 test/build.sh create mode 100644 test/testsuite/.gitignore create mode 100644 test/testsuite/manifest.json create mode 100644 test/testsuite/thread_spawn-simple.c diff --git a/test/build.sh b/test/build.sh new file mode 100755 index 0000000..01188f5 --- /dev/null +++ b/test/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +CC=${CC:=clang} + +for input in testsuite/*.c; do + output="testsuite/$(basename $input .c).wasm" + + if [ "$input" -nt "$output" ]; then + echo "Compiling $input" + $CC "$input" -o "$output" + fi +done diff --git a/test/testsuite/.gitignore b/test/testsuite/.gitignore new file mode 100644 index 0000000..917660a --- /dev/null +++ b/test/testsuite/.gitignore @@ -0,0 +1 @@ +*.wasm \ No newline at end of file diff --git a/test/testsuite/manifest.json b/test/testsuite/manifest.json new file mode 100644 index 0000000..10451d0 --- /dev/null +++ b/test/testsuite/manifest.json @@ -0,0 +1,3 @@ +{ + "name": "WASI threads proposal" +} diff --git a/test/testsuite/thread_spawn-simple.c b/test/testsuite/thread_spawn-simple.c new file mode 100644 index 0000000..671f95b --- /dev/null +++ b/test/testsuite/thread_spawn-simple.c @@ -0,0 +1,71 @@ +#include +#include + +static const int64_t SECOND = 1000 * 1000 * 1000; + +typedef struct { + int th_ready; + int th_continue; + int th_done; + int failed; + int tid; + int value; +} shared_t; + +__attribute__((export_name("wasi_thread_start"))) void +wasi_thread_start(int thread_id, int *start_arg) +{ + shared_t *data = (shared_t *)start_arg; + + data->th_ready = 1; + __builtin_wasm_memory_atomic_notify(&data->th_ready, 1); + + // so we can have all the threads alive at the same time + if (__builtin_wasm_memory_atomic_wait32(&data->th_continue, 0, SECOND) == 2) { + data->failed = 1; + return; + } + + assert(data->value == 52); + + data->value += 8; + data->tid = thread_id; + + data->th_done = 1; + __builtin_wasm_memory_atomic_notify(&data->th_done, 1); +} + +int +main(int argc, char **argv) +{ + shared_t data[3] = { 0 }; + int data_count = sizeof(data) / sizeof(data[0]); + int i, j; + + for (i = 0; i < data_count; i++) { + data[i].value = 52; + assert(__wasi_thread_spawn(&data[i]) == 0); + assert(__builtin_wasm_memory_atomic_wait32(&data[i].th_ready, 0, + SECOND) + != 2); // not a timeout + } + + for (i = 0; i < data_count; i++) { + __builtin_wasm_memory_atomic_notify(&data[i].th_continue, 1); + } + + for (i = 0; i < data_count; i++) { + assert(__builtin_wasm_memory_atomic_wait32(&data[i].th_done, 0, + SECOND) + != 2); // not a timeout + assert(data[i].value == 60); + + for (j = i + 1; j < data_count; j++) { + assert(data[i].tid != data[j].tid); + } + + assert(data[i].failed == 0); + } + + return 0; +} \ No newline at end of file