Skip to content

Commit

Permalink
Add thread spawn test
Browse files Browse the repository at this point in the history
  • Loading branch information
loganek committed Dec 21, 2022
1 parent 3005bf1 commit a4f92e1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
12 changes: 12 additions & 0 deletions test/build.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/testsuite/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.wasm
3 changes: 3 additions & 0 deletions test/testsuite/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "WASI threads proposal"
}
71 changes: 71 additions & 0 deletions test/testsuite/thread_spawn-simple.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <assert.h>
#include <wasi/api.h>

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;
}

0 comments on commit a4f92e1

Please sign in to comment.