Skip to content

Commit 684ae65

Browse files
authored
Create a placeholder for WASI threads implementation (#1783)
This a simpler version of the PR: #1638
1 parent d974452 commit 684ae65

11 files changed

Lines changed: 256 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ if (NOT DEFINED WAMR_BUILD_LIB_PTHREAD)
8383
set (WAMR_BUILD_LIB_PTHREAD 0)
8484
endif ()
8585

86+
if (NOT DEFINED WAMR_BUILD_LIB_WASI_THREADS)
87+
# Disable wasi threads library by default
88+
set (WAMR_BUILD_LIB_WASI_THREADS 0)
89+
endif ()
90+
8691
if (NOT DEFINED WAMR_BUILD_MINI_LOADER)
8792
# Disable wasm mini loader by default
8893
set (WAMR_BUILD_MINI_LOADER 0)

build-scripts/runtime_lib.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ if (WAMR_BUILD_LIB_PTHREAD EQUAL 1)
124124
set (WAMR_BUILD_SHARED_MEMORY 1)
125125
endif ()
126126

127+
if (WAMR_BUILD_LIB_WASI_THREADS EQUAL 1)
128+
include (${IWASM_DIR}/libraries/lib-wasi-threads/lib_wasi_threads.cmake)
129+
# Enable the dependent feature if lib wasi threads is enabled
130+
set (WAMR_BUILD_BULK_MEMORY 1)
131+
set (WAMR_BUILD_SHARED_MEMORY 1)
132+
endif ()
133+
127134
if (WAMR_BUILD_DEBUG_INTERP EQUAL 1)
128135
set (WAMR_BUILD_THREAD_MGR 1)
129136
include (${IWASM_DIR}/libraries/debug-engine/debug_engine.cmake)
@@ -186,6 +193,7 @@ set (source_all
186193
${WASM_APP_LIB_SOURCE_ALL}
187194
${NATIVE_INTERFACE_SOURCE}
188195
${APP_MGR_SOURCE}
196+
${LIB_WASI_THREADS_SOURCE}
189197
${LIB_PTHREAD_SOURCE}
190198
${THREAD_MGR_SOURCE}
191199
${LIBC_EMCC_SOURCE}

core/config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@
161161
#define WASM_ENABLE_LIB_PTHREAD_SEMAPHORE 0
162162
#endif
163163

164+
#ifndef WASM_ENABLE_LIB_WASI_THREADS
165+
#define WASM_ENABLE_LIB_WASI_THREADS 0
166+
#endif
167+
164168
#ifndef WASM_ENABLE_BASE_LIB
165169
#define WASM_ENABLE_BASE_LIB 0
166170
#endif

core/iwasm/common/wasm_native.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ uint32
5353
get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis);
5454
#endif
5555

56+
#if WASM_ENABLE_LIB_WASI_THREADS != 0
57+
uint32
58+
get_lib_wasi_threads_export_apis(NativeSymbol **p_lib_wasi_threads_apis);
59+
#endif
60+
5661
uint32
5762
get_libc_emcc_export_apis(NativeSymbol **p_libc_emcc_apis);
5863

@@ -383,7 +388,7 @@ wasm_native_init()
383388
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
384389
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
385390
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
386-
|| WASM_ENABLE_LIB_PTHREAD != 0
391+
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
387392
NativeSymbol *native_symbols;
388393
uint32 n_native_symbols;
389394
#endif
@@ -438,6 +443,14 @@ wasm_native_init()
438443
goto fail;
439444
#endif
440445

446+
#if WASM_ENABLE_LIB_WASI_THREADS != 0
447+
n_native_symbols = get_lib_wasi_threads_export_apis(&native_symbols);
448+
if (n_native_symbols > 0
449+
&& !wasm_native_register_natives("wasi", native_symbols,
450+
n_native_symbols))
451+
goto fail;
452+
#endif
453+
441454
#if WASM_ENABLE_LIBC_EMCC != 0
442455
n_native_symbols = get_libc_emcc_export_apis(&native_symbols);
443456
if (n_native_symbols > 0
@@ -466,7 +479,7 @@ wasm_native_init()
466479
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
467480
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
468481
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
469-
|| WASM_ENABLE_LIB_PTHREAD != 0
482+
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
470483
fail:
471484
wasm_native_destroy();
472485
return false;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
set (LIB_WASI_THREADS_DIR ${CMAKE_CURRENT_LIST_DIR})
5+
6+
add_definitions (-DWASM_ENABLE_LIB_WASI_THREADS=1)
7+
8+
include_directories(${LIB_WASI_THREADS_DIR})
9+
10+
set (LIB_WASI_THREADS_SOURCE
11+
${LIB_WASI_THREADS_DIR}/lib_wasi_threads_wrapper.c)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include "wasmtime_ssp.h"
7+
8+
#if WASM_ENABLE_INTERP != 0
9+
#include "wasm_runtime.h"
10+
#endif
11+
12+
#if WASM_ENABLE_AOT != 0
13+
#include "aot_runtime.h"
14+
#endif
15+
16+
static __wasi_errno_t
17+
thread_spawn_wrapper(wasm_exec_env_t exec_env, void *start_arg)
18+
{
19+
return __WASI_ENOSYS;
20+
}
21+
22+
/* clang-format off */
23+
#define REG_NATIVE_FUNC(func_name, signature) \
24+
{ #func_name, func_name##_wrapper, signature, NULL }
25+
/* clang-format on */
26+
27+
static NativeSymbol native_symbols_lib_wasi_threads[] = { REG_NATIVE_FUNC(
28+
thread_spawn, "(*)i") };
29+
30+
uint32
31+
get_lib_wasi_threads_export_apis(NativeSymbol **p_lib_wasi_threads_apis)
32+
{
33+
*p_lib_wasi_threads_apis = native_symbols_lib_wasi_threads;
34+
return sizeof(native_symbols_lib_wasi_threads) / sizeof(NativeSymbol);
35+
}

product-mini/platforms/linux/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ if (NOT DEFINED WAMR_BUILD_LIB_PTHREAD)
8585
set (WAMR_BUILD_LIB_PTHREAD 0)
8686
endif ()
8787

88+
if (NOT DEFINED WAMR_BUILD_LIB_WASI_THREADS)
89+
# Disable wasi threads library by default
90+
set (WAMR_BUILD_LIB_WASI_THREADS 0)
91+
endif()
92+
93+
8894
if (NOT DEFINED WAMR_BUILD_MINI_LOADER)
8995
# Disable wasm mini loader by default
9096
set (WAMR_BUILD_MINI_LOADER 0)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
cmake_minimum_required(VERSION 3.14)
5+
6+
include(CheckPIESupported)
7+
8+
project(wasi_threads_sample)
9+
10+
if (NOT DEFINED WASI_SYSROOT)
11+
message (WARNING "Custom sysroot with threads enabled is required to build wasi threads samples.
12+
Please note that current wasi-sdk doesn't ship with threads enabled.
13+
Run cmake command with -DWASI_SYSROOT=/path/to/sysroot/with/threads to compile samples.")
14+
return ()
15+
endif ()
16+
17+
################ runtime settings ################
18+
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
19+
if (APPLE)
20+
add_definitions(-DBH_PLATFORM_DARWIN)
21+
endif ()
22+
23+
# Resetdefault linker flags
24+
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
25+
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
26+
27+
# WAMR features switch
28+
29+
# Set WAMR_BUILD_TARGET, currently values supported:
30+
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
31+
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
32+
if (NOT DEFINED WAMR_BUILD_TARGET)
33+
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
34+
set (WAMR_BUILD_TARGET "AARCH64")
35+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
36+
set (WAMR_BUILD_TARGET "RISCV64")
37+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
38+
# Build as X86_64 by default in 64-bit platform
39+
set (WAMR_BUILD_TARGET "X86_64")
40+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
41+
# Build as X86_32 by default in 32-bit platform
42+
set (WAMR_BUILD_TARGET "X86_32")
43+
else ()
44+
message(SEND_ERROR "Unsupported build target platform!")
45+
endif ()
46+
endif ()
47+
48+
if (NOT CMAKE_BUILD_TYPE)
49+
set (CMAKE_BUILD_TYPE Release)
50+
endif ()
51+
52+
set(WAMR_BUILD_INTERP 1)
53+
set(WAMR_BUILD_AOT 1)
54+
set(WAMR_BUILD_JIT 0)
55+
set(WAMR_BUILD_LIBC_BUILTIN 1)
56+
set(WAMR_BUILD_FAST_INTERP 1)
57+
set(WAMR_BUILD_LIBC_WASI 1)
58+
set(WAMR_BUILD_LIB_WASI_THREADS 1)
59+
60+
# compiling and linking flags
61+
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
62+
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
63+
endif ()
64+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
65+
66+
# build out vmlib
67+
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
68+
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
69+
70+
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
71+
################################################
72+
73+
74+
################ wasm application ################
75+
add_subdirectory(wasm-apps)
76+
77+
################ wamr runtime ################
78+
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
79+
80+
set (RUNTIME_SOURCE_ALL
81+
${CMAKE_CURRENT_LIST_DIR}/../../product-mini/platforms/linux/main.c
82+
${UNCOMMON_SHARED_SOURCE}
83+
)
84+
add_executable (iwasm ${RUNTIME_SOURCE_ALL})
85+
check_pie_supported()
86+
set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON)
87+
target_link_libraries(iwasm vmlib -lpthread -lm -ldl)

samples/wasi-threads/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# "WASI threads" sample introduction
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
if (NOT DEFINED WASI_SDK_DIR)
5+
set (WASI_SDK_DIR "/opt/wasi-sdk")
6+
endif ()
7+
8+
set (CMAKE_SYSROOT "${WASI_SYSROOT}")
9+
set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
10+
set (CMAKE_C_COMPILER_TARGET "wasm32-wasi")
11+
12+
function (compile_sample SOURCE_FILE)
13+
get_filename_component (FILE_NAME ${SOURCE_FILE} NAME_WLE)
14+
set (WASM_MODULE ${FILE_NAME}.wasm)
15+
add_executable (${WASM_MODULE} ${SOURCE_FILE})
16+
17+
target_compile_options (${WASM_MODULE} PRIVATE
18+
-pthread -ftls-model=local-exec)
19+
20+
target_link_options (${WASM_MODULE} PRIVATE
21+
-z stack-size=32768
22+
LINKER:--export=__heap_base
23+
LINKER:--export=__data_end
24+
LINKER:--shared-memory,--max-memory=1966080
25+
LINKER:--export=wasi_thread_start
26+
)
27+
endfunction ()
28+
29+
compile_sample(no_pthread.c)

0 commit comments

Comments
 (0)