mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-03-12 08:55:28 +00:00

For now this implementation uses thread manager. Not sure whether thread manager is needed in that case. In the future there'll be likely another syscall added (for pthread_exit) and for that we might need some kind of thread management - with that in mind, we keep thread manager for now and will refactor this later if needed.
62 lines
1.4 KiB
C
62 lines
1.4 KiB
C
/*
|
|
* Copyright (C) 2022 Amazon.com Inc. or its affiliates. All rights reserved.
|
|
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
*/
|
|
#ifndef __wasi__
|
|
#error This example only compiles to WASM/WASI target
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <assert.h>
|
|
#include <wasi/api.h>
|
|
|
|
static const int64_t SECOND = 1000 * 1000 * 1000;
|
|
|
|
typedef struct {
|
|
int th_ready;
|
|
int value;
|
|
int thread_id;
|
|
} 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;
|
|
|
|
printf("New thread ID: %d, starting parameter: %d\n", thread_id,
|
|
data->value);
|
|
|
|
data->thread_id = thread_id;
|
|
data->value += 8;
|
|
printf("Updated value: %d\n", data->value);
|
|
|
|
data->th_ready = 1;
|
|
__builtin_wasm_memory_atomic_notify(&data->th_ready, 1);
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
shared_t data = { 0, 52, -1 };
|
|
int thread_id;
|
|
|
|
thread_id = __wasi_thread_spawn(&data);
|
|
if (thread_id < 0) {
|
|
printf("Failed to create thread: %d\n", thread_id);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
if (__builtin_wasm_memory_atomic_wait32(&data.th_ready, 0, SECOND) == 2) {
|
|
printf("Timeout\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
printf("Thread completed, new value: %d, thread id: %d\n", data.value,
|
|
data.thread_id);
|
|
|
|
assert(thread_id == data.thread_id);
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|