Create a placeholder for WASI threads implementation (#1783)
This a simpler version of the PR: https://github.com/bytecodealliance/wasm-micro-runtime/pull/1638
This commit is contained in:
29
samples/wasi-threads/wasm-apps/CMakeLists.txt
Normal file
29
samples/wasi-threads/wasm-apps/CMakeLists.txt
Normal file
@ -0,0 +1,29 @@
|
||||
# Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
if (NOT DEFINED WASI_SDK_DIR)
|
||||
set (WASI_SDK_DIR "/opt/wasi-sdk")
|
||||
endif ()
|
||||
|
||||
set (CMAKE_SYSROOT "${WASI_SYSROOT}")
|
||||
set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
|
||||
set (CMAKE_C_COMPILER_TARGET "wasm32-wasi")
|
||||
|
||||
function (compile_sample SOURCE_FILE)
|
||||
get_filename_component (FILE_NAME ${SOURCE_FILE} NAME_WLE)
|
||||
set (WASM_MODULE ${FILE_NAME}.wasm)
|
||||
add_executable (${WASM_MODULE} ${SOURCE_FILE})
|
||||
|
||||
target_compile_options (${WASM_MODULE} PRIVATE
|
||||
-pthread -ftls-model=local-exec)
|
||||
|
||||
target_link_options (${WASM_MODULE} PRIVATE
|
||||
-z stack-size=32768
|
||||
LINKER:--export=__heap_base
|
||||
LINKER:--export=__data_end
|
||||
LINKER:--shared-memory,--max-memory=1966080
|
||||
LINKER:--export=wasi_thread_start
|
||||
)
|
||||
endfunction ()
|
||||
|
||||
compile_sample(no_pthread.c)
|
||||
55
samples/wasi-threads/wasm-apps/no_pthread.c
Normal file
55
samples/wasi-threads/wasm-apps/no_pthread.c
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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 <wasi/api.h>
|
||||
|
||||
static const int64_t SECOND = 1000 * 1000 * 1000;
|
||||
|
||||
typedef struct {
|
||||
int th_ready;
|
||||
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;
|
||||
|
||||
printf("New thread ID: %d, starting parameter: %d\n", thread_id,
|
||||
data->value);
|
||||
|
||||
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 };
|
||||
__wasi_errno_t err;
|
||||
|
||||
err = __wasi_thread_spawn(&data);
|
||||
if (err != __WASI_ERRNO_SUCCESS) {
|
||||
printf("Failed to create thread: %d\n", err);
|
||||
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\n", data.value);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user