-
Notifications
You must be signed in to change notification settings - Fork 803
Create a placeholder for WASI threads implementation #1783
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
Merged
wenyongh
merged 1 commit into
bytecodealliance:dev/wasi_threads
from
loganek:loganek/threads-placeholder
Dec 6, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads.cmake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| set (LIB_WASI_THREADS_DIR ${CMAKE_CURRENT_LIST_DIR}) | ||
|
|
||
| add_definitions (-DWASM_ENABLE_LIB_WASI_THREADS=1) | ||
|
|
||
| include_directories(${LIB_WASI_THREADS_DIR}) | ||
|
|
||
| set (LIB_WASI_THREADS_SOURCE | ||
| ${LIB_WASI_THREADS_DIR}/lib_wasi_threads_wrapper.c) |
35 changes: 35 additions & 0 deletions
35
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| */ | ||
|
|
||
| #include "wasmtime_ssp.h" | ||
|
|
||
| #if WASM_ENABLE_INTERP != 0 | ||
| #include "wasm_runtime.h" | ||
| #endif | ||
|
|
||
| #if WASM_ENABLE_AOT != 0 | ||
| #include "aot_runtime.h" | ||
| #endif | ||
|
|
||
| static __wasi_errno_t | ||
| thread_spawn_wrapper(wasm_exec_env_t exec_env, void *start_arg) | ||
| { | ||
| return __WASI_ENOSYS; | ||
| } | ||
|
|
||
| /* clang-format off */ | ||
| #define REG_NATIVE_FUNC(func_name, signature) \ | ||
| { #func_name, func_name##_wrapper, signature, NULL } | ||
| /* clang-format on */ | ||
|
|
||
| static NativeSymbol native_symbols_lib_wasi_threads[] = { REG_NATIVE_FUNC( | ||
| thread_spawn, "(*)i") }; | ||
|
|
||
| uint32 | ||
| get_lib_wasi_threads_export_apis(NativeSymbol **p_lib_wasi_threads_apis) | ||
| { | ||
| *p_lib_wasi_threads_apis = native_symbols_lib_wasi_threads; | ||
| return sizeof(native_symbols_lib_wasi_threads) / sizeof(NativeSymbol); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| cmake_minimum_required(VERSION 3.14) | ||
|
|
||
| include(CheckPIESupported) | ||
|
|
||
| project(wasi_threads_sample) | ||
|
|
||
| if (NOT DEFINED WASI_SYSROOT) | ||
| message (WARNING "Custom sysroot with threads enabled is required to build wasi threads samples. | ||
| Please note that current wasi-sdk doesn't ship with threads enabled. | ||
| Run cmake command with -DWASI_SYSROOT=/path/to/sysroot/with/threads to compile samples.") | ||
| return () | ||
| endif () | ||
|
|
||
| ################ runtime settings ################ | ||
| string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM) | ||
| if (APPLE) | ||
| add_definitions(-DBH_PLATFORM_DARWIN) | ||
| endif () | ||
|
|
||
| # Resetdefault linker flags | ||
| set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") | ||
| set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") | ||
|
|
||
| # WAMR features switch | ||
|
|
||
| # Set WAMR_BUILD_TARGET, currently values supported: | ||
| # "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]", | ||
| # "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]" | ||
| if (NOT DEFINED WAMR_BUILD_TARGET) | ||
| if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)") | ||
| set (WAMR_BUILD_TARGET "AARCH64") | ||
| elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64") | ||
| set (WAMR_BUILD_TARGET "RISCV64") | ||
| elseif (CMAKE_SIZEOF_VOID_P EQUAL 8) | ||
| # Build as X86_64 by default in 64-bit platform | ||
| set (WAMR_BUILD_TARGET "X86_64") | ||
| elseif (CMAKE_SIZEOF_VOID_P EQUAL 4) | ||
| # Build as X86_32 by default in 32-bit platform | ||
| set (WAMR_BUILD_TARGET "X86_32") | ||
| else () | ||
| message(SEND_ERROR "Unsupported build target platform!") | ||
| endif () | ||
| endif () | ||
|
|
||
| if (NOT CMAKE_BUILD_TYPE) | ||
| set (CMAKE_BUILD_TYPE Release) | ||
| endif () | ||
|
|
||
| set(WAMR_BUILD_INTERP 1) | ||
| set(WAMR_BUILD_AOT 1) | ||
| set(WAMR_BUILD_JIT 0) | ||
| set(WAMR_BUILD_LIBC_BUILTIN 1) | ||
| set(WAMR_BUILD_FAST_INTERP 1) | ||
| set(WAMR_BUILD_LIBC_WASI 1) | ||
| set(WAMR_BUILD_LIB_WASI_THREADS 1) | ||
|
|
||
| # compiling and linking flags | ||
| if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang")) | ||
| set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") | ||
| endif () | ||
| set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security") | ||
|
|
||
| # build out vmlib | ||
| set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..) | ||
| include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) | ||
|
|
||
| add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE}) | ||
| ################################################ | ||
|
|
||
|
|
||
| ################ wasm application ################ | ||
| add_subdirectory(wasm-apps) | ||
|
|
||
| ################ wamr runtime ################ | ||
| include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) | ||
|
|
||
| set (RUNTIME_SOURCE_ALL | ||
| ${CMAKE_CURRENT_LIST_DIR}/../../product-mini/platforms/linux/main.c | ||
| ${UNCOMMON_SHARED_SOURCE} | ||
| ) | ||
| add_executable (iwasm ${RUNTIME_SOURCE_ALL}) | ||
| check_pie_supported() | ||
| set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON) | ||
| target_link_libraries(iwasm vmlib -lpthread -lm -ldl) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # "WASI threads" sample introduction |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
|
||
|
loganek marked this conversation as resolved.
|
||
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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__ | ||
|
loganek marked this conversation as resolved.
|
||
| #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; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.