Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thread spawn test #11

Merged
merged 1 commit into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}