Skip to content
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

[browser][MT] add memory barriers before sending messages to other workers #112199

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/mono/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ elseif(CLR_CMAKE_HOST_OS STREQUAL "emscripten")
add_compile_options(-Wno-strict-prototypes)
add_compile_options(-Wno-unused-but-set-variable)
add_compile_options(-Wno-single-bit-bitfield-constant-conversion)
if(NOT DISABLE_THREADS)
add_compile_options(-matomics)
endif()
set(DISABLE_EXECUTABLES 1)
# FIXME: Is there a cmake option for this ?
set(DISABLE_SHARED_LIBS 1)
Expand Down
4 changes: 1 addition & 3 deletions src/mono/browser/browser.proj
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,6 @@
<ItemGroup Condition="'$(WasmEnableThreads)' == 'true'">
<EmccExportedFunction Include="_emscripten_main_runtime_thread_id" />
</ItemGroup>
<ItemGroup Condition="'$(WasmEnableThreads)' == 'true'">
<EmccExportedFunction Include="_emscripten_main_runtime_thread_id" />
</ItemGroup>
<PropertyGroup>
<_EmccExportedLibraryFunction>"[@(EmccExportedLibraryFunction -> '%27%(Identity)%27', ',')]"</_EmccExportedLibraryFunction>
<_EmccExportedRuntimeMethods>"[@(EmccExportedRuntimeMethod -> '%27%(Identity)%27', ',')]"</_EmccExportedRuntimeMethods>
Expand Down Expand Up @@ -408,6 +405,7 @@
<CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableSIMD)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_COMPILE_OPTIONS=&quot;-msimd128&quot; -DCONFIGURATION_INTERPSIMDTABLES_LIB=&quot;simd&quot;</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableSIMD)' != 'true'">$(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_INTERPSIMDTABLES_LIB=&quot;nosimd&quot;</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableThreads)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DDISABLE_THREADS=0</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableThreads)' != 'true'">$(CMakeBuildRuntimeConfigureCmd) -DDISABLE_THREADS=1</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableJsInteropByValue)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DENABLE_JS_INTEROP_BY_VALUE=1</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) $(CMakeConfigurationEmsdkPath)</CMakeBuildRuntimeConfigureCmd>

Expand Down
30 changes: 27 additions & 3 deletions src/mono/browser/runtime/corebindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void mono_wasm_assembly_get_entry_point (char *assembly_name, int auto_insert_br
void mono_wasm_get_assembly_export (char *assembly_name, char *namespace, char *classname, char *methodname, int signature_hash, MonoMethod **method_out);

#ifndef DISABLE_THREADS
void wasm_atomic_fence(void);

void mono_wasm_release_cs_owned_object_post (pthread_t target_tid, int js_handle);
void mono_wasm_resolve_or_reject_promise_post (pthread_t target_tid, void *args);
void mono_wasm_cancel_promise_post (pthread_t target_tid, int task_holder_gc_handle);
Expand All @@ -46,6 +48,7 @@ extern void mono_wasm_install_js_worker_interop (int context_gc_handle);
void mono_wasm_install_js_worker_interop_wrapper (int context_gc_handle, void* beforeSyncJSImport, void* afterSyncJSImport, void* pumpHandler);
extern void mono_wasm_uninstall_js_worker_interop ();
extern void mono_wasm_invoke_jsimport_MT (void* signature, void* args);
void mono_wasm_invoke_jsimport_MT_barrier (void* signature, void* args);
void mono_wasm_invoke_jsimport_async_post (pthread_t target_tid, void* signature, void* args);
void mono_wasm_invoke_jsimport_sync_send (pthread_t target_tid, void* signature, void* args);
void mono_wasm_invoke_js_function_send (pthread_t target_tid, int function_js_handle, void *args);
Expand Down Expand Up @@ -73,7 +76,7 @@ void bindings_initialize_internals (void)
mono_add_internal_call ("Interop/Runtime::ResolveOrRejectPromisePost", mono_wasm_resolve_or_reject_promise_post);
mono_add_internal_call ("Interop/Runtime::InstallWebWorkerInterop", mono_wasm_install_js_worker_interop_wrapper);
mono_add_internal_call ("Interop/Runtime::UninstallWebWorkerInterop", mono_wasm_uninstall_js_worker_interop);
mono_add_internal_call ("Interop/Runtime::InvokeJSImportSync", mono_wasm_invoke_jsimport_MT);
mono_add_internal_call ("Interop/Runtime::InvokeJSImportSync", mono_wasm_invoke_jsimport_MT_barrier);
mono_add_internal_call ("Interop/Runtime::InvokeJSImportSyncSend", mono_wasm_invoke_jsimport_sync_send);
mono_add_internal_call ("Interop/Runtime::InvokeJSImportAsyncPost", mono_wasm_invoke_jsimport_async_post);
mono_add_internal_call ("Interop/Runtime::InvokeJSFunctionSend", mono_wasm_invoke_js_function_send);
Expand Down Expand Up @@ -255,43 +258,64 @@ void mono_wasm_install_js_worker_interop_wrapper (int context_gc_handle, void* b
before_sync_js_import = beforeSyncJSImport;
after_sync_js_import = afterSyncJSImport;
synchronization_context_pump_handler = pumpHandler;
__sync_synchronize ();
wasm_atomic_fence ();
mono_wasm_install_js_worker_interop (context_gc_handle);
}

// async
void mono_wasm_release_cs_owned_object_post (pthread_t target_tid, int js_handle)
{
__sync_synchronize ();
wasm_atomic_fence ();
mono_threads_wasm_async_run_in_target_thread_vi (target_tid, (void (*) (gpointer))mono_wasm_release_cs_owned_object, (gpointer)js_handle);
}

// async
void mono_wasm_resolve_or_reject_promise_post (pthread_t target_tid, void* args)
{
__sync_synchronize ();
wasm_atomic_fence ();
mono_threads_wasm_async_run_in_target_thread_vi (target_tid, (void (*) (gpointer))mono_wasm_resolve_or_reject_promise, (gpointer)args);
}

// async
void mono_wasm_cancel_promise_post (pthread_t target_tid, int task_holder_gc_handle)
{
__sync_synchronize ();
wasm_atomic_fence ();
mono_threads_wasm_async_run_in_target_thread_vi (target_tid, (void (*) (gpointer))mono_wasm_cancel_promise, (gpointer)task_holder_gc_handle);
}

// async
void mono_wasm_invoke_jsimport_async_post (pthread_t target_tid, void* signature, void* args)
{
mono_threads_wasm_async_run_in_target_thread_vii (target_tid, (void (*) (gpointer, gpointer))mono_wasm_invoke_jsimport_MT, (gpointer)signature, (gpointer)args);
__sync_synchronize ();
wasm_atomic_fence ();
mono_threads_wasm_async_run_in_target_thread_vii (target_tid, (void (*) (gpointer, gpointer))mono_wasm_invoke_jsimport_MT_barrier, (gpointer)signature, (gpointer)args);
}

// sync
void mono_wasm_invoke_jsimport_sync_send (pthread_t target_tid, void* signature, void* args)
{
mono_threads_wasm_sync_run_in_target_thread_vii (target_tid, (void (*) (gpointer, gpointer))mono_wasm_invoke_jsimport_MT, (gpointer)signature, (gpointer)args);
__sync_synchronize ();
wasm_atomic_fence ();
mono_threads_wasm_sync_run_in_target_thread_vii (target_tid, (void (*) (gpointer, gpointer))mono_wasm_invoke_jsimport_MT_barrier, (gpointer)signature, (gpointer)args);
}

// sync
void mono_wasm_invoke_js_function_send (pthread_t target_tid, int function_js_handle, void *args)
{
__sync_synchronize ();
wasm_atomic_fence ();
mono_threads_wasm_sync_run_in_target_thread_vii (target_tid, (void (*) (gpointer, gpointer))mono_wasm_invoke_js_function, (gpointer)function_js_handle, (gpointer)args);
}

void mono_wasm_invoke_jsimport_MT_barrier (void* signature, void* args)
{
__sync_synchronize ();
wasm_atomic_fence ();
mono_wasm_invoke_jsimport_MT (signature, args);
}

#endif /* DISABLE_THREADS */
24 changes: 24 additions & 0 deletions src/mono/browser/runtime/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ char *mono_method_get_full_name (MonoMethod *method);
#ifndef INVARIANT_TIMEZONE
extern void mono_register_timezones_bundle (void);
#endif /* INVARIANT_TIMEZONE */

#ifndef DISABLE_THREADS
void wasm_atomic_fence(void);
#endif /* DISABLE_THREADS */

extern void mono_wasm_set_entrypoint_breakpoint (const char* assembly_name, int method_token);

extern void mono_bundled_resources_add_assembly_resource (const char *id, const char *name, const uint8_t *data, uint32_t size, void (*free_func)(void *, void*), void *free_data);
Expand Down Expand Up @@ -188,6 +193,11 @@ mono_wasm_load_runtime (int debug_level)
runtime_initialized = 1;
const char *interp_opts = "";

#ifndef DISABLE_THREADS
wasm_atomic_fence ();
__sync_synchronize ();
#endif /* DISABLE_THREADS */

#ifndef INVARIANT_GLOBALIZATION
mono_wasm_link_icu_shim ();
#endif
Expand Down Expand Up @@ -230,6 +240,12 @@ mono_wasm_load_runtime (int debug_level)
root_domain = mono_wasm_load_runtime_common (debug_level, wasm_trace_logger, interp_opts);

bindings_initialize_internals();

#ifndef DISABLE_THREADS
wasm_atomic_fence ();
__sync_synchronize ();
#endif /* DISABLE_THREADS */

}

int initialize_runtime()
Expand Down Expand Up @@ -283,6 +299,8 @@ mono_wasm_print_thread_dump (void)
static void
mono_wasm_invoke_jsexport_async_post_cb (MonoMethod *method, void* args)
{
wasm_atomic_fence ();
__sync_synchronize ();
mono_wasm_invoke_jsexport (method, args);
if (args) {
MonoBoolean *is_receiver_should_free = (MonoBoolean *)(((char *) args) + 20/*JSMarshalerArgumentOffsets.ReceiverShouldFree*/);
Expand All @@ -296,6 +314,8 @@ mono_wasm_invoke_jsexport_async_post_cb (MonoMethod *method, void* args)
EMSCRIPTEN_KEEPALIVE void
mono_wasm_invoke_jsexport_async_post (void* target_thread, MonoMethod *method, void* args /*JSMarshalerArguments*/)
{
wasm_atomic_fence ();
__sync_synchronize ();
mono_threads_wasm_async_run_in_target_thread_vii(target_thread, (void (*)(gpointer, gpointer))mono_wasm_invoke_jsexport_async_post_cb, method, args);
}

Expand All @@ -310,6 +330,8 @@ extern sync_context_pump synchronization_context_pump_handler;
EMSCRIPTEN_KEEPALIVE void
mono_wasm_invoke_jsexport_sync (MonoMethod *method, void* args)
{
wasm_atomic_fence ();
__sync_synchronize ();
before_sync_js_import (args);
mono_wasm_invoke_jsexport (method, args);
after_sync_js_import (args);
Expand All @@ -319,6 +341,8 @@ mono_wasm_invoke_jsexport_sync (MonoMethod *method, void* args)
EMSCRIPTEN_KEEPALIVE void
mono_wasm_invoke_jsexport_sync_send (void* target_thread, MonoMethod *method, void* args /*JSMarshalerArguments*/)
{
wasm_atomic_fence ();
__sync_synchronize ();
mono_threads_wasm_sync_run_in_target_thread_vii (target_thread, (void (*)(gpointer, gpointer))mono_wasm_invoke_jsexport_sync, method, args);
}

Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/runtime/pthreads/deputy-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function mono_wasm_start_deputy_thread_async () {
}
});

forceThreadMemoryViewRefresh();
await start_runtime();

postMessageToMain({
Expand Down
3 changes: 3 additions & 0 deletions src/mono/browser/runtime/pthreads/ui-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { MonoWorkerToMainMessage, monoThreadInfo, mono_wasm_pthread_ptr, update_
import { Module, ENVIRONMENT_IS_WORKER, createPromiseController, loaderHelpers, mono_assert, runtimeHelpers } from "../globals";
import { PThreadLibrary, MainToWorkerMessageType, MonoThreadMessage, PThreadInfo, PThreadPtr, PThreadPtrNull, PThreadWorker, PromiseController, Thread, WorkerToMainMessageType, monoMessageSymbol } from "../types/internal";
import { mono_log_info, mono_log_debug, mono_log_warn } from "../logging";
import { forceThreadMemoryViewRefresh } from "../memory";

const threadPromises: Map<PThreadPtr, PromiseController<Thread>[]> = new Map();

Expand Down Expand Up @@ -71,6 +72,8 @@ function monoWorkerMessageHandler (worker: PThreadWorker, ev: MessageEvent<any>)
return;
}

forceThreadMemoryViewRefresh();

let thread: Thread;
pthreadId = message.info?.pthreadId ?? 0;
worker.info = Object.assign({}, worker.info, message.info);
Expand Down
3 changes: 2 additions & 1 deletion src/mono/browser/runtime/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { populateEmscriptenPool, mono_wasm_init_threads } from "./pthreads";
import { currentWorkerThreadEvents, dotnetPthreadCreated, initWorkerThreadEvents, monoThreadInfo } from "./pthreads";
import { mono_wasm_pthread_ptr, update_thread_info } from "./pthreads";
import { jiterpreter_allocate_tables } from "./jiterpreter-support";
import { localHeapViewU8, malloc } from "./memory";
import { forceThreadMemoryViewRefresh, localHeapViewU8, malloc } from "./memory";
import { assertNoProxies } from "./gc-handles";
import { runtimeList } from "./exports";
import { nativeAbort, nativeExit } from "./run";
Expand Down Expand Up @@ -321,6 +321,7 @@ async function onRuntimeInitializedAsync (userOnRuntimeInitialized: (module:Emsc
await wait_for_all_assets();

if (WasmEnableThreads) {
forceThreadMemoryViewRefresh();
runtimeHelpers.deputyWorker.thread!.postMessageToWorker({
type:"deputyThread",
cmd: MainToWorkerMessageType.allAssetsLoaded,
Expand Down
1 change: 0 additions & 1 deletion src/mono/llvm/llvm-init.proj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<MonoLLVMToolsVersion Condition="'$(MonoLLVMHostOS)' == 'linux-musl'">$(runtimelinuxmuslx64MicrosoftNETCoreRuntimeMonoLLVMToolsVersion)</MonoLLVMToolsVersion>
<MonoLLVMToolsVersion Condition="'$(MonoLLVMHostOS)' == 'win'">$(runtimewinx64MicrosoftNETCoreRuntimeMonoLLVMToolsVersion)</MonoLLVMToolsVersion>
<MonoLLVMToolsVersion Condition="'$(MonoLLVMHostOS)' == 'osx'">$(runtimeosxx64MicrosoftNETCoreRuntimeMonoLLVMToolsVersion)</MonoLLVMToolsVersion>
<MonoLLVMSDKDebugSuffix Condition="'$(MonoLLVMHostOS)' == 'win' and '$(Configuration)' == 'Debug'" >.Debug</MonoLLVMSDKDebugSuffix>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/mono/mono/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ set(utils_unix_sources

if(HOST_WIN32)
set(utils_platform_sources ${utils_win32_sources})
elseif(HOST_WASM)
elseif(HOST_WASM AND DISABLE_THREADS)
set(utils_platform_sources ${utils_unix_sources} mono-threads-wasm.S)
elseif(HOST_WASM AND NOT DISABLE_THREADS)
set(utils_platform_sources ${utils_unix_sources} mono-threads-wasm.S mono-threads-wasm-MT.S)
else()
set(utils_platform_sources ${utils_unix_sources})
endif()
Expand Down
7 changes: 7 additions & 0 deletions src/mono/mono/utils/mono-memory-model.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

#include <config.h>

#ifdef HOST_WASI
void wasm_atomic_fence(void);
#endif

#if _MSC_VER
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
Expand Down Expand Up @@ -41,6 +45,9 @@ mono_memory_barrier (void)
static inline void
mono_memory_barrier (void)
{
#ifdef HOST_WASI
wasm_atomic_fence ();
#endif
__sync_synchronize ();
}

Expand Down
12 changes: 12 additions & 0 deletions src/mono/mono/utils/mono-threads-wasm-MT.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.globl wasm_atomic_fence

.globaltype __stack_pointer, i32

# simulate atomic.fence
wasm_atomic_fence:
.functype wasm_atomic_fence () -> ()
global.get __stack_pointer
i32.const 0
i32.atomic.rmw.or 0
drop
end_function
9 changes: 8 additions & 1 deletion src/mono/mono/utils/mono-threads-wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
#include <emscripten/threading.h>
#include <mono/metadata/threads-types.h>
#endif

#else
void
wasm_atomic_fence (void)
{
}
#endif

uintptr_t get_wasm_stack_high(void);
Expand Down Expand Up @@ -311,6 +315,9 @@ mono_threads_platform_in_critical_region (THREAD_INFO_TYPE *info)
void
mono_memory_barrier_process_wide (void)
{
#ifndef DISABLE_THREADS
mono_memory_barrier ();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is not enough for GC. Maybe we need to set barrier on all threads ? How?

#endif
}

#ifdef HOST_BROWSER
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasi/wasi.proj
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@
<CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableSIMD)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_COMPILE_OPTIONS=&quot;-msimd128&quot; -DCONFIGURATION_INTERPSIMDTABLES_LIB=&quot;simd&quot;</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableSIMD)' != 'true'">$(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_INTERPSIMDTABLES_LIB=&quot;nosimd&quot;</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableThreads)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DDISABLE_THREADS=0</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableThreads)' != 'true'">$(CMakeBuildRuntimeConfigureCmd) -DDISABLE_THREADS=1</CMakeBuildRuntimeConfigureCmd>
<CMakeBuildRuntimeConfigureCmd Condition="'$(OS)' == 'Windows_NT'">call &quot;$(RepositoryEngineeringDir)native\init-vs-env.cmd&quot; wasm &amp;&amp; $(CMakeBuildRuntimeConfigureCmd)</CMakeBuildRuntimeConfigureCmd>

<CMakeOptions Condition="'$(MonoVerboseBuild)' != ''">-v</CMakeOptions>
Expand Down
Loading