diff --git a/eng/native/gen-buildsys.cmd b/eng/native/gen-buildsys.cmd
index f67f17b8d47942..7b42fba9a08a21 100644
--- a/eng/native/gen-buildsys.cmd
+++ b/eng/native/gen-buildsys.cmd
@@ -22,6 +22,7 @@ set __Arch=%4
set __Os=%5
set __CmakeGenerator=Visual Studio
set __UseEmcmake=0
+set __Pthreads=0
if /i "%__Ninja%" == "1" (
set __CmakeGenerator=Ninja
) else (
@@ -37,6 +38,14 @@ if /i "%__Ninja%" == "1" (
)
)
+:loop
+if [%6] == [] goto end_loop
+if ""%6"" == """-DCMAKE_USE_PTHREADS=1""" set __Pthreads=1
+set __ExtraCmakeParams=%__ExtraCmakeParams% %6
+shift
+goto loop
+:end_loop
+
if /i "%__Arch%" == "wasm" (
if "%__Os%" == "" (
@@ -72,19 +81,16 @@ if /i "%__Arch%" == "wasm" (
set "WASI_SDK_PATH=!WASI_SDK_PATH:\=/!"
if not "!WASI_SDK_PATH:~-1!" == "/" set "WASI_SDK_PATH=!WASI_SDK_PATH!/"
set __CmakeGenerator=Ninja
- set __ExtraCmakeParams=%__ExtraCmakeParams% -DCLR_CMAKE_TARGET_OS=wasi -DCLR_CMAKE_TARGET_ARCH=wasm "-DWASI_SDK_PREFIX=!WASI_SDK_PATH!" "-DCMAKE_TOOLCHAIN_FILE=!WASI_SDK_PATH!/share/cmake/wasi-sdk.cmake" "-DCMAKE_SYSROOT=!WASI_SDK_PATH!/share/wasi-sysroot"
+ if "%__Pthreads%" == "1" (
+ set __ExtraCmakeParams=%__ExtraCmakeParams% -DCLR_CMAKE_TARGET_OS=wasi -DCLR_CMAKE_TARGET_ARCH=wasm "-DWASI_SDK_PREFIX=!WASI_SDK_PATH!" "-DCMAKE_TOOLCHAIN_FILE=!WASI_SDK_PATH!/share/cmake/wasi-sdk-pthread.cmake" "-DCMAKE_SYSROOT=!WASI_SDK_PATH!/share/wasi-sysroot"
+ ) else (
+ set __ExtraCmakeParams=%__ExtraCmakeParams% -DCLR_CMAKE_TARGET_OS=wasi -DCLR_CMAKE_TARGET_ARCH=wasm "-DWASI_SDK_PREFIX=!WASI_SDK_PATH!" "-DCMAKE_TOOLCHAIN_FILE=!WASI_SDK_PATH!/share/cmake/wasi-sdk.cmake" "-DCMAKE_SYSROOT=!WASI_SDK_PATH!/share/wasi-sysroot"
+ )
)
) else (
set __ExtraCmakeParams=%__ExtraCmakeParams% "-DCMAKE_SYSTEM_VERSION=10.0"
)
-:loop
-if [%6] == [] goto end_loop
-set __ExtraCmakeParams=%__ExtraCmakeParams% %6
-shift
-goto loop
-:end_loop
-
set __ExtraCmakeParams="-DCMAKE_INSTALL_PREFIX=%__CMakeBinDir%" "-DCLR_CMAKE_HOST_ARCH=%__Arch%" %__ExtraCmakeParams%
set __CmdLineOptionsUpToDateFile=%__IntermediatesDir%\cmake_cmd_line.txt
diff --git a/src/mono/CMakeLists.txt b/src/mono/CMakeLists.txt
index d46df3a77a3b87..5566aa18308cf6 100644
--- a/src/mono/CMakeLists.txt
+++ b/src/mono/CMakeLists.txt
@@ -153,6 +153,10 @@ if(NOT AOT_TARGET_TRIPLE STREQUAL "")
elseif(AOT_TARGET_TRIPLE STREQUAL "wasm32-unknown-wasi")
set(TARGET_SYSTEM_NAME "wasi")
set(TARGET_ARCH "wasm")
+ elseif(AOT_TARGET_TRIPLE STREQUAL "wasm32-unknown-wasi-threads")
+ set(TARGET_SYSTEM_NAME "wasi")
+ set(TARGET_SYSTEM_VARIANT "threads")
+ set(TARGET_ARCH "wasm")
elseif(AOT_TARGET_TRIPLE STREQUAL "x86_64-none-linux-android")
set(TARGET_SYSTEM_NAME "android")
set(TARGET_ARCH "x86_64")
@@ -180,11 +184,17 @@ endif()
# HOST OS CHECKS
######################################
+if("${triple}" STREQUAL "wasm32-wasi-threads")
+ set(CMAKE_SYSTEM_VARIANT "threads")
+endif()
+
message ("CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}")
message ("CMAKE_SYSTEM_VARIANT=${CMAKE_SYSTEM_VARIANT}")
set(CLR_CMAKE_HOST_OS ${CMAKE_SYSTEM_NAME})
-string(TOLOWER ${CLR_CMAKE_HOST_OS} CLR_CMAKE_HOST_OS)
+set(CLR_CMAKE_HOST_VARIANT ${CMAKE_SYSTEM_VARIANT})
+string(TOLOWER "${CLR_CMAKE_HOST_OS}" CLR_CMAKE_HOST_OS)
+string(TOLOWER "${CLR_CMAKE_HOST_VARIANT}" CLR_CMAKE_HOST_VARIANT)
# TODO: check if we can enable this on more platforms
# set(MONO_KEYWORD_THREAD "__thread")
@@ -195,7 +205,7 @@ if(CLR_CMAKE_HOST_OS STREQUAL "darwin")
set(HOST_OSX 1)
set(PTHREAD_POINTER_ID 1)
set(USE_MACH_SEMA 1)
- if(CMAKE_SYSTEM_VARIANT STREQUAL "maccatalyst")
+ if(CLR_CMAKE_HOST_VARIANT STREQUAL "maccatalyst")
set(HOST_MACCAT 1)
endif()
elseif(CLR_CMAKE_HOST_OS STREQUAL "ios" OR CLR_CMAKE_HOST_OS STREQUAL "tvos")
@@ -310,6 +320,7 @@ endif()
if(NOT TARGET_SYSTEM_NAME)
set(TARGET_SYSTEM_NAME "${CLR_CMAKE_HOST_OS}")
+ set(TARGET_SYSTEM_VARIANT "${CLR_CMAKE_HOST_VARIANT}")
endif()
if(TARGET_SYSTEM_NAME STREQUAL "darwin")
@@ -317,7 +328,7 @@ if(TARGET_SYSTEM_NAME STREQUAL "darwin")
set(TARGET_MACH 1)
set(TARGET_OSX 1)
set(TARGET_DARWIN 1)
- if(CMAKE_SYSTEM_VARIANT STREQUAL "maccatalyst")
+ if(TARGET_SYSTEM_VARIANT STREQUAL "maccatalyst")
set(TARGET_MACCAT 1)
endif()
elseif(TARGET_SYSTEM_NAME STREQUAL "ios" OR TARGET_SYSTEM_NAME STREQUAL "tvos")
@@ -351,7 +362,9 @@ elseif(TARGET_SYSTEM_NAME STREQUAL "emscripten")
endif()
elseif(TARGET_SYSTEM_NAME STREQUAL "wasi")
set(TARGET_WASI 1)
- set(DISABLE_THREADS 1)
+ if (NOT (TARGET_SYSTEM_VARIANT STREQUAL "threads"))
+ set(DISABLE_THREADS 1)
+ endif()
if (CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_options(-Os)
endif()
diff --git a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj
index c273e6ba4172f7..f44122c527bc53 100644
--- a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj
+++ b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj
@@ -126,7 +126,7 @@
true
true
true
- true
+ true
true
@@ -274,7 +274,7 @@
-
+
@@ -288,7 +288,7 @@
-
+
diff --git a/src/mono/mono.proj b/src/mono/mono.proj
index 8b57badb1a2e38..3f4be2458baea1 100644
--- a/src/mono/mono.proj
+++ b/src/mono/mono.proj
@@ -59,6 +59,7 @@
coop
coop
+ coop
preemptive
@@ -602,7 +603,8 @@
<_MonoCMakeConfigureCommand>cmake @(_MonoCMakeArgs, ' ') $(MonoCMakeExtraArgs) "$(MonoProjectRoot.TrimEnd('\/'))"
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(TargetsWasi)' != 'true' and '$(_MonoSkipInitCompiler)' != 'true' and '$(HostOS)' != 'windows'">sh -c 'build_arch="$(_CompilerTargetArch)" compiler="$(MonoCCompiler)" . "$(RepositoryEngineeringCommonDir)native/init-compiler.sh" && @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)'
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(TargetsWasi)' != 'true' and '$(_MonoSkipInitCompiler)' == 'true' and '$(HostOS)' != 'windows'">$(_MonoCCOption) $(_MonoCXXOption) @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)
- <_MonoCMakeConfigureCommand Condition="'$(TargetsWasi)' == 'true'">$(_MonoCMakeConfigureCommand) -DWASI_SDK_PREFIX=$(WASI_SDK_PATH) -DCMAKE_SYSROOT=$(WASI_SDK_PATH)share/wasi-sysroot -DCMAKE_TOOLCHAIN_FILE=$(WASI_SDK_PATH)share/cmake/wasi-sdk.cmake -DCMAKE_CXX_FLAGS="--sysroot=$(WASI_SDK_PATH)share/wasi-sysroot"
+ <_MonoCMakeConfigureCommand Condition="'$(TargetsWasi)' == 'true' and '$(WasmEnableThreads)' != 'true'">$(_MonoCMakeConfigureCommand) -DWASI_SDK_PREFIX=$(WASI_SDK_PATH) -DCMAKE_SYSROOT=$(WASI_SDK_PATH)share/wasi-sysroot -DCMAKE_TOOLCHAIN_FILE=$(WASI_SDK_PATH)share/cmake/wasi-sdk.cmake -DCMAKE_CXX_FLAGS="--sysroot=$(WASI_SDK_PATH)share/wasi-sysroot"
+ <_MonoCMakeConfigureCommand Condition="'$(TargetsWasi)' == 'true' and '$(WasmEnableThreads)' == 'true'">$(_MonoCMakeConfigureCommand) -DWASI_SDK_PREFIX=$(WASI_SDK_PATH) -DCMAKE_SYSROOT=$(WASI_SDK_PATH)share/wasi-sysroot -DCMAKE_TOOLCHAIN_FILE=$(WASI_SDK_PATH)share/cmake/wasi-sdk-pthread.cmake -DCMAKE_CXX_FLAGS="--sysroot=$(WASI_SDK_PATH)share/wasi-sysroot"
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(TargetsWasi)' != 'true' and '$(HostOS)' == 'windows'">call "$(RepositoryEngineeringDir)native\init-vs-env.cmd" $(_CompilerTargetArch) && cd /D "$(MonoObjDir)" && @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' == 'true' and '$(HostOS)' != 'windows'">bash -c 'source $(EMSDK_PATH)/emsdk_env.sh 2>&1 && emcmake $(_MonoCMakeConfigureCommand)'
@@ -724,7 +726,8 @@
true
wasm32-unknown-none
- wasm32-unknown-wasi
+ wasm32-unknown-wasi
+ wasm32-unknown-wasi-threads
$(MonoObjCrossDir)offsets-wasm32-unknown-none.h
$(MonoLLVMDir)/$(BuildArchitecture)/lib/libclang.dylib
$(MonoLLVMDir)/$(BuildArchitecture)/lib/libclang.so
diff --git a/src/mono/mono/component/diagnostics_server.c b/src/mono/mono/component/diagnostics_server.c
index 02179f785eb352..1276f0c4886cf3 100644
--- a/src/mono/mono/component/diagnostics_server.c
+++ b/src/mono/mono/component/diagnostics_server.c
@@ -7,7 +7,7 @@
#include
#include
#include
-#if defined (HOST_WASM) && !defined(HOST_WASI)
+#if defined(HOST_BROWSER)
#include
#include
#include
@@ -19,7 +19,7 @@
static bool
diagnostics_server_available (void);
-#if !defined (HOST_WASM) || defined (DISABLE_THREADS)
+#if !defined(HOST_BROWSER) || defined (DISABLE_THREADS)
static MonoComponentDiagnosticsServer fn_table = {
{ MONO_COMPONENT_ITF_VERSION, &diagnostics_server_available },
&ds_server_init,
@@ -350,7 +350,7 @@ wasm_ipc_stream_close (void *self)
return r == 0;
}
-#endif /* !defined (HOST_WASM) || defined (DISABLE_THREADS) */
+#endif /* !defined (HOST_BROWSER) || defined (DISABLE_THREADS) */
static bool
diagnostics_server_available (void)
diff --git a/src/mono/mono/metadata/gc.c b/src/mono/mono/metadata/gc.c
index 49fd0c2553dfef..f0fcc0f9ada614 100644
--- a/src/mono/mono/metadata/gc.c
+++ b/src/mono/mono/metadata/gc.c
@@ -691,7 +691,7 @@ mono_gc_finalize_notify (void)
if (mono_gc_is_null ())
return;
-#if defined(HOST_WASI)
+#if defined(HOST_WASI) && defined(DISABLE_THREADS)
// TODO: Schedule the background job on WASI. Threads aren't yet supported in this build.
#elif defined(HOST_WASM) && defined(DISABLE_THREADS)
mono_main_thread_schedule_background_job (mono_runtime_do_background_work);
diff --git a/src/mono/mono/metadata/sgen-mono.c b/src/mono/mono/metadata/sgen-mono.c
index a650acb6ed7253..d515a32d829a86 100644
--- a/src/mono/mono/metadata/sgen-mono.c
+++ b/src/mono/mono/metadata/sgen-mono.c
@@ -2893,7 +2893,7 @@ sgen_client_binary_protocol_collection_end (int minor_gc_count, int generation,
MONO_PROFILER_RAISE (gc_event, (MONO_GC_EVENT_END, generation, generation == GENERATION_OLD && sgen_concurrent_collection_in_progress));
}
-#if defined(HOST_WASM) && defined(DISABLE_THREADS)
+#if defined(HOST_BROWSER) && defined(DISABLE_THREADS)
void
sgen_client_schedule_background_job (void (*cb)(void))
{
diff --git a/src/mono/mono/metadata/threads.c b/src/mono/mono/metadata/threads.c
index 74f1db84f0518c..c170c08ab7f3ff 100644
--- a/src/mono/mono/metadata/threads.c
+++ b/src/mono/mono/metadata/threads.c
@@ -1286,8 +1286,11 @@ start_wrapper (gpointer data)
}
mono_thread_info_exit (res);
-
+#ifdef HOST_WASI
+ return (mono_thread_start_return_t)res;
+#else
g_assert_not_reached ();
+#endif
}
static void
@@ -1754,6 +1757,8 @@ mono_thread_exit (void)
exit (mono_environment_exitcode_get ());
mono_thread_info_exit (0);
+
+ g_assert_not_reached ();
}
MonoThread *
@@ -2889,6 +2894,8 @@ mono_threads_set_shutting_down (void)
/* Wake up other threads potentially waiting for us */
mono_thread_info_exit (0);
+
+ g_assert_not_reached ();
} else {
shutting_down = TRUE;
diff --git a/src/mono/mono/mini/mini-wasm.h b/src/mono/mono/mini/mini-wasm.h
index 77c7f3a78fa5cc..c7e9710845eba5 100644
--- a/src/mono/mono/mini/mini-wasm.h
+++ b/src/mono/mono/mini/mini-wasm.h
@@ -91,8 +91,10 @@ typedef struct {
#define MONO_ARCH_HAS_REGISTER_ICALL 1
#define MONO_ARCH_HAVE_SDB_TRAMPOLINES 1
#define MONO_ARCH_LLVM_TARGET_LAYOUT "e-m:e-p:32:32-i64:64-n32:64-S128"
-#ifdef TARGET_WASI
+#if defined(TARGET_WASI) && defined(DISABLE_THREADS)
#define MONO_ARCH_LLVM_TARGET_TRIPLE "wasm32-unknown-wasi"
+#elif defined(TARGET_WASI)
+#define MONO_ARCH_LLVM_TARGET_TRIPLE "wasm32-unknown-wasi-threads"
#else
#define MONO_ARCH_LLVM_TARGET_TRIPLE "wasm32-unknown-emscripten"
#endif
diff --git a/src/mono/mono/utils/mono-threads-posix.c b/src/mono/mono/utils/mono-threads-posix.c
index 23c1e4f3056aaf..f9bdf49b90eb30 100644
--- a/src/mono/mono/utils/mono-threads-posix.c
+++ b/src/mono/mono/utils/mono-threads-posix.c
@@ -32,7 +32,7 @@
#include
-#if defined(_POSIX_VERSION) && !defined (HOST_WASM)
+#if defined(_POSIX_VERSION) && !defined (USE_WASM_BACKEND)
#include
@@ -127,11 +127,13 @@ mono_threads_platform_yield (void)
return sched_yield () == 0;
}
+#ifndef USE_PTHREAD_WASM_BACKEND
void
mono_threads_platform_exit (gsize exit_code)
{
pthread_exit ((gpointer) exit_code);
}
+#endif
gboolean
mono_thread_platform_external_eventloop_keepalive_check (void)
@@ -142,7 +144,7 @@ mono_thread_platform_external_eventloop_keepalive_check (void)
return FALSE;
}
-#if HOST_FUCHSIA
+#if defined(HOST_FUCHSIA) || !defined(HAVE_GETRLIMIT)
int
mono_thread_info_get_system_max_stack_size (void)
{
diff --git a/src/mono/mono/utils/mono-threads-wasi.S b/src/mono/mono/utils/mono-threads-wasi.S
index 6de64294456183..7c95b9f3b6af50 100644
--- a/src/mono/mono/utils/mono-threads-wasi.S
+++ b/src/mono/mono/utils/mono-threads-wasi.S
@@ -13,3 +13,16 @@ get_wasm_heap_base:
.functype get_wasm_heap_base () -> (i32)
global.get __heap_base@GOT
end_function
+
+.globl get_wasm_stack_low
+.globl get_wasm_stack_high
+
+get_wasm_stack_low:
+ .functype get_wasm_stack_low () -> (i32)
+ global.get __stack_low@GOT
+ end_function
+
+get_wasm_stack_high:
+ .functype get_wasm_stack_high () -> (i32)
+ global.get __stack_high@GOT
+ end_function
diff --git a/src/mono/mono/utils/mono-threads-wasm.c b/src/mono/mono/utils/mono-threads-wasm.c
index cfd411c712c385..bf3690ca7eaee0 100644
--- a/src/mono/mono/utils/mono-threads-wasm.c
+++ b/src/mono/mono/utils/mono-threads-wasm.c
@@ -683,7 +683,113 @@ mono_threads_wasm_sync_run_in_target_thread_vii (pthread_t target_thread, void (
#endif /* DISABLE_THREADS */
-#endif /* HOST_BROWSER */
+#endif /* defined(USE_WASM_BACKEND) */
+
+#elif defined(USE_PTHREAD_WASM_BACKEND)
+
+struct __pthread {
+ struct pthread *self;
+ struct pthread *prev, *next;
+ uintptr_t sysinfo;
+ uintptr_t canary;
+
+ int tid;
+ int errno_val;
+ volatile int detach_state;
+ volatile int cancel;
+ volatile unsigned char canceldisable, cancelasync;
+ unsigned char tsd_used:1;
+ unsigned char dlerror_flag:1;
+ unsigned char *map_base;
+ size_t map_size;
+ void *stack;
+ size_t stack_size;
+ size_t guard_size;
+ void *result;
+ struct __ptcb *cancelbuf;
+ void **tsd;
+ struct {
+ volatile void *volatile head;
+ long off;
+ volatile void *volatile pending;
+ } robust_list;
+ int h_errno_val;
+ volatile int timer_id;
+ locale_t locale;
+ volatile int killlock[1];
+ char *dlerror_buf;
+ void *stdio_locks;
+};
+
+uintptr_t get_wasm_stack_low(void);
+uintptr_t get_wasm_stack_high(void);
+
+void
+mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
+{
+ pthread_t self = pthread_self();
+ if (self->tid == 0x3fffffff) {
+ *staddr = (guint8*)get_wasm_stack_low ();
+ *stsize = get_wasm_stack_high() - get_wasm_stack_low();
+ } else {
+ *staddr = (guint8*)self->stack - self->stack_size;
+ *stsize = self->stack_size;
+ }
+}
+
+guint64
+mono_native_thread_os_id_get (void)
+{
+ return (guint64)pthread_self()->tid;
+}
+
+void
+mono_threads_suspend_init_signals (void)
+{
+}
+
+void
+mono_threads_platform_exit (gsize exit_code)
+{
+}
+
+void
+mono_threads_suspend_init (void)
+{
+}
+
+void
+mono_threads_suspend_register (MonoThreadInfo *info)
+{
+}
+
+gboolean
+mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
+{
+ return TRUE;
+}
+
+void
+mono_threads_suspend_free (MonoThreadInfo *info)
+{
+}
+
+gboolean
+mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
+{
+ return TRUE;
+}
+
+gboolean
+mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
+{
+ return TRUE;
+}
+
+void
+mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
+{
+}
#else
diff --git a/src/mono/mono/utils/mono-threads.c b/src/mono/mono/utils/mono-threads.c
index 14a00bc9154291..c5791bcce1796c 100644
--- a/src/mono/mono/utils/mono-threads.c
+++ b/src/mono/mono/utils/mono-threads.c
@@ -523,11 +523,9 @@ register_thread (MonoThreadInfo *info)
g_assert (staddr);
#endif /* TARGET_WASM */
-#ifdef HOST_WASM
-#ifndef DISABLE_THREADS
+#if defined(USE_WASM_BACKEND) && !defined(DISABLE_THREADS)
mono_native_tls_set_value (jobs_key, NULL);
-#endif /* DISABLE_THREADS */
-#endif /* HOST_WASM */
+#endif
g_assert (stsize);
info->stack_start_limit = staddr;
@@ -979,11 +977,9 @@ mono_thread_info_init (size_t info_size)
mono_threads_suspend_policy_init ();
-#ifdef HOST_WASM
-#ifndef DISABLE_THREADS
+#if defined(USE_WASM_BACKEND) && !defined(DISABLE_THREADS)
res = mono_native_tls_alloc (&jobs_key, NULL);
-#endif /* DISABLE_THREADS */
-#endif /* HOST_BROWSER */
+#endif
#ifdef HOST_WIN32
res = mono_native_tls_alloc (&thread_info_key, NULL);
diff --git a/src/mono/mono/utils/mono-threads.h b/src/mono/mono/utils/mono-threads.h
index a738460f58950f..4629019e951b05 100644
--- a/src/mono/mono/utils/mono-threads.h
+++ b/src/mono/mono/utils/mono-threads.h
@@ -128,8 +128,10 @@ and reduce the number of casts drastically.
/* If this is defined, use the signals backed on Mach. Debug only as signals can't be made usable on OSX. */
// #define USE_SIGNALS_ON_MACH
-#ifdef HOST_WASM
+#if (defined(HOST_WASM) && defined(DISABLE_THREADS)) || defined(HOST_BROWSER)
#define USE_WASM_BACKEND
+#elif defined(HOST_WASM)
+#define USE_PTHREAD_WASM_BACKEND
#elif defined (_POSIX_VERSION)
#if defined (__MACH__) && !defined (USE_SIGNALS_ON_MACH)
#define USE_MACH_BACKEND
@@ -846,7 +848,7 @@ void mono_threads_join_lock (void);
void mono_threads_join_unlock (void);
-#ifdef HOST_WASM
+#ifdef HOST_BROWSER
typedef void (*background_job_cb)(void);
#ifdef DISABLE_THREADS
void mono_main_thread_schedule_background_job (background_job_cb cb);
@@ -894,7 +896,7 @@ mono_win32_abort_blocking_io_call (THREAD_INFO_TYPE *info);
if (GetLastError () != _last_error_restore_point) \
mono_SetLastError (_last_error_restore_point);
-#elif defined(USE_WASM_BACKEND) || defined (USE_POSIX_BACKEND)
+#elif defined(USE_WASM_BACKEND) || defined(USE_PTHREAD_WASM_BACKEND) || defined(USE_POSIX_BACKEND)
#define MONO_DEFINE_LAST_ERROR_RESTORE_POINT \
int _last_errno_restore_point = errno;
diff --git a/src/mono/wasi/build/WasiApp.targets b/src/mono/wasi/build/WasiApp.targets
index b229d76dd71ff9..4a64bab593e952 100644
--- a/src/mono/wasi/build/WasiApp.targets
+++ b/src/mono/wasi/build/WasiApp.targets
@@ -311,8 +311,10 @@
<_WasmNativeFileForLinking Remove="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmEHLibToExclude)" />
<_WasmNativeFileForLinking Remove="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmSIMDLibToExclude)" />
- <_WasmNativeFileForLinking Include="$(WasiSysRoot)\lib\wasm32-wasi\libc++.a" />
- <_WasmNativeFileForLinking Include="$(WasiSysRoot)\lib\wasm32-wasi\libc++abi.a" />
+ <_WasmNativeFileForLinking Condition="'$(WasmEnableThreads)' != 'true'" Include="$(WasiSysRoot)\lib\wasm32-wasi\libc++.a" />
+ <_WasmNativeFileForLinking Condition="'$(WasmEnableThreads)' != 'true'" Include="$(WasiSysRoot)\lib\wasm32-wasi\libc++abi.a" />
+ <_WasmNativeFileForLinking Condition="'$(WasmEnableThreads)' == 'true'" Include="$(WasiSysRoot)\lib\wasm32-wasi-threads\libc++.a" />
+ <_WasmNativeFileForLinking Condition="'$(WasmEnableThreads)' == 'true'" Include="$(WasiSysRoot)\lib\wasm32-wasi-threads\libc++abi.a" />
<_WasmNativeFileForLinking Include="@(NativeFileReference)" />
@@ -326,6 +328,8 @@
<_WasmLinkDependencies Include="@(_WasiFilePathForFixup)" />
+ <_WasiLinkStepArgs Condition="'$(WasmEnableThreads)' == 'true'" Include="-target wasm32-wasi-threads -pthread" />
+
<_WasiLinkStepArgs Condition="'$(OS)' == 'Windows_NT'" Include=""$([System.String]::new(%(_WasiFilePathForFixup.Identity)).Replace('\', '/'))"" />
<_WasiLinkStepArgs Condition="'$(OS)' != 'Windows_NT'" Include="@(_WasiFilePathForFixup -> '"%(Identity)"')" />
diff --git a/src/mono/wasi/runtime/stubs.c b/src/mono/wasi/runtime/stubs.c
index b5978c9c0e7258..e7547152d05237 100644
--- a/src/mono/wasi/runtime/stubs.c
+++ b/src/mono/wasi/runtime/stubs.c
@@ -2,6 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
#include
+#include "wasm-config.h"
+
+#ifdef DISABLE_THREADS
// These are symbols that are never used at runtime, or at least don't need to do anything for prototype apps
@@ -14,11 +17,13 @@ int sem_timedwait (int *sem, void *abs_timeout) { assert(0); return 0; }
int __errno_location() { return 0; }
-void mono_log_close_syslog (void) { assert(0); }
-void mono_log_open_syslog (const char *a, void *b) { assert(0); }
-void mono_log_write_syslog (const char *a, int b, int c, const char *d) { assert(0); }
+void syslog(int pri, const char *fmt, int ignored) { assert (0); }
+
+#endif
-void mono_runtime_setup_stat_profiler () { assert(0); }
int mono_thread_state_init_from_handle (int *tctx, int *info, void *sigctx) { assert(0); return 0; }
+void mono_runtime_setup_stat_profiler () { assert(0); }
-void syslog(int pri, const char *fmt, int ignored) { assert (0); }
+void mono_log_close_syslog (void) { assert(0); }
+void mono_log_open_syslog (const char *a, void *b) { assert(0); }
+void mono_log_write_syslog (const char *a, int b, int c, const char *d) { assert(0); }
diff --git a/src/mono/wasi/runtime/synthetic-pthread.c b/src/mono/wasi/runtime/synthetic-pthread.c
index aa15f21f71107d..539f470b5ff16a 100644
--- a/src/mono/wasi/runtime/synthetic-pthread.c
+++ b/src/mono/wasi/runtime/synthetic-pthread.c
@@ -2,6 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
#include
+#include "wasm-config.h"
+
+#ifdef DISABLE_THREADS
#define SYNTHETIC_PTHREAD_KEYS_MAX 32
@@ -60,3 +63,5 @@ int pthread_cond_destroy(int a) { return 0; }
int pthread_mutex_init(int a, int b) { return 0; }
int pthread_mutex_destroy(int a) { return 0; }
int pthread_cond_timedwait(int a, int b, int c) { return 0; }
+
+#endif
diff --git a/src/mono/wasi/wasi.proj b/src/mono/wasi/wasi.proj
index bd03a1a0e79b44..75ff5c296f3d12 100644
--- a/src/mono/wasi/wasi.proj
+++ b/src/mono/wasi/wasi.proj
@@ -205,7 +205,8 @@
$(CMakeBuildRuntimeConfigureCmd) -DWASI_SDK_PREFIX=$(WASI_SDK_PATH)
$(CMakeBuildRuntimeConfigureCmd) -DCMAKE_SYSROOT=$(WASI_SDK_PATH)share/wasi-sysroot
$(CMakeBuildRuntimeConfigureCmd) -DCMAKE_CXX_FLAGS="--sysroot=$(WASI_SDK_PATH)share/wasi-sysroot"
- $(CMakeBuildRuntimeConfigureCmd) -DCMAKE_TOOLCHAIN_FILE=$(WASI_SDK_PATH)share/cmake/wasi-sdk.cmake
+ $(CMakeBuildRuntimeConfigureCmd) -DCMAKE_TOOLCHAIN_FILE=$(WASI_SDK_PATH)share/cmake/wasi-sdk.cmake
+ $(CMakeBuildRuntimeConfigureCmd) -DCMAKE_TOOLCHAIN_FILE=$(WASI_SDK_PATH)share/cmake/wasi-sdk-pthread.cmake
$(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_WASICC_FLAGS="$(CMakeConfigurationWasiFlags)"
$(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_LINK_FLAGS="$(CMakeConfigurationLinkFlags)"
diff --git a/src/native/libs/Common/pal_config.h.in b/src/native/libs/Common/pal_config.h.in
index c4843bf8712df7..3b6c4e1fb74c98 100644
--- a/src/native/libs/Common/pal_config.h.in
+++ b/src/native/libs/Common/pal_config.h.in
@@ -153,7 +153,7 @@
#cmakedefine01 HAVE_DIRENT_NAME_SIZE
#cmakedefine01 DIRENT_NAME_SIZE
-#ifndef HOST_WASI
+#ifndef HOST_BROWSER
#cmakedefine01 HAVE_GETRUSAGE
#endif
diff --git a/src/native/libs/System.Native/pal_time.c b/src/native/libs/System.Native/pal_time.c
index a249fe653be1c0..488c49776fd9f3 100644
--- a/src/native/libs/System.Native/pal_time.c
+++ b/src/native/libs/System.Native/pal_time.c
@@ -121,7 +121,7 @@ int64_t SystemNative_GetBootTimeTicks(void)
double SystemNative_GetCpuUtilization(ProcessCpuInformation* previousCpuInfo)
{
-#if defined(HAVE_GETRUSAGE) && !defined(HOST_BROWSER)
+#if defined(HAVE_GETRUSAGE)
uint64_t kernelTime = 0;
uint64_t userTime = 0;
diff --git a/src/native/libs/build-native.sh b/src/native/libs/build-native.sh
index 3e90b3805aaec9..6aa69b4d806ade 100755
--- a/src/native/libs/build-native.sh
+++ b/src/native/libs/build-native.sh
@@ -76,7 +76,11 @@ elif [[ "$__TargetOS" == wasi ]]; then
export WASI_SDK_PATH="${WASI_SDK_PATH%/}/"
export CLR_CC="$WASI_SDK_PATH"bin/clang
export TARGET_BUILD_ARCH=wasm
- __CMakeArgs="-DCLR_CMAKE_TARGET_OS=wasi -DCLR_CMAKE_TARGET_ARCH=wasm -DWASI_SDK_PREFIX=$WASI_SDK_PATH -DCMAKE_TOOLCHAIN_FILE=$WASI_SDK_PATH/share/cmake/wasi-sdk.cmake $__CMakeArgs"
+ if [[ "$__usePThreads" -eq 1 ]]; then
+ __CMakeArgs="-DCLR_CMAKE_TARGET_OS=wasi -DCLR_CMAKE_TARGET_ARCH=wasm -DWASI_SDK_PREFIX=$WASI_SDK_PATH -DCMAKE_TOOLCHAIN_FILE=$WASI_SDK_PATH/share/cmake/wasi-sdk-pthread.cmake $__CMakeArgs"
+ else
+ __CMakeArgs="-DCLR_CMAKE_TARGET_OS=wasi -DCLR_CMAKE_TARGET_ARCH=wasm -DWASI_SDK_PREFIX=$WASI_SDK_PATH -DCMAKE_TOOLCHAIN_FILE=$WASI_SDK_PATH/share/cmake/wasi-sdk.cmake $__CMakeArgs"
+ fi
elif [[ "$__TargetOS" == ios || "$__TargetOS" == iossimulator ]]; then
# nothing to do here
true