diff --git a/src/mono/browser/runtime/invoke-js.ts b/src/mono/browser/runtime/invoke-js.ts index 5323407eefa0f..9c118329b15aa 100644 --- a/src/mono/browser/runtime/invoke-js.ts +++ b/src/mono/browser/runtime/invoke-js.ts @@ -6,7 +6,7 @@ import BuildConfiguration from "consts:configuration"; import { marshal_exception_to_cs, bind_arg_marshal_to_cs } from "./marshal-to-cs"; import { get_signature_argument_count, bound_js_function_symbol, get_sig, get_signature_version, get_signature_type, imported_js_function_symbol, get_signature_handle, get_signature_function_name, get_signature_module_name } from "./marshal"; -import { setI32_unchecked, receiveWorkerHeapViews } from "./memory"; +import { setI32_unchecked, receiveWorkerHeapViews, forceThreadMemoryViewRefresh } from "./memory"; import { stringToMonoStringRoot } from "./strings"; import { MonoObject, MonoObjectRef, JSFunctionSignature, JSMarshalerArguments, WasmRoot, BoundMarshalerToJs, JSFnHandle, BoundMarshalerToCs, JSHandle, MarshalerType } from "./types/internal"; import { Int32Ptr } from "./types/emscripten"; @@ -52,6 +52,9 @@ export function mono_wasm_invoke_import_async(args: JSMarshalerArguments, signat let max_postpone_count = 10; function postpone_invoke_import_async() { if (max_postpone_count < 0 || is_thread_available()) { + if (WasmEnableThreads) { + forceThreadMemoryViewRefresh(); + } bound_fn(args); Module._free(args as any); } else { diff --git a/src/mono/browser/runtime/pthreads/shared/emscripten-replacements.ts b/src/mono/browser/runtime/pthreads/shared/emscripten-replacements.ts index 2f004c49369a6..2d2c22cb1ab0e 100644 --- a/src/mono/browser/runtime/pthreads/shared/emscripten-replacements.ts +++ b/src/mono/browser/runtime/pthreads/shared/emscripten-replacements.ts @@ -5,11 +5,11 @@ import WasmEnableThreads from "consts:wasmEnableThreads"; import BuildConfiguration from "consts:configuration"; import { dumpThreads, onWorkerLoadInitiated, resolveThreadPromises } from "../browser"; -import { mono_wasm_pthread_on_pthread_created } from "../worker"; +import { mono_wasm_pthread_on_pthread_created, onRunMessage } from "../worker"; import { PThreadLibrary, PThreadWorker, getModulePThread, getUnusedWorkerPool } from "./emscripten-internals"; -import { loaderHelpers, mono_assert } from "../../globals"; +import { Module, loaderHelpers, mono_assert } from "../../globals"; import { mono_log_warn } from "../../logging"; -import { PThreadPtrNull } from "./types"; +import { PThreadPtr, PThreadPtrNull } from "./types"; /** @module emscripten-replacements Replacements for individual functions in the emscripten PThreads library. * These have a hard dependency on the version of Emscripten that we are using and may need to be kept in sync with @@ -22,6 +22,13 @@ export function replaceEmscriptenPThreadLibrary(modulePThread: PThreadLibrary): const originalLoadWasmModuleToWorker = modulePThread.loadWasmModuleToWorker; const originalThreadInitTLS = modulePThread.threadInitTLS; const originalReturnWorkerToPool = modulePThread.returnWorkerToPool; + const original_emscripten_thread_init = (Module as any)["__emscripten_thread_init"]; + + + (Module as any)["__emscripten_thread_init"] = (pthread_ptr: PThreadPtr, isMainBrowserThread: number, isMainRuntimeThread: number, canBlock: number) => { + onRunMessage(pthread_ptr); + original_emscripten_thread_init(pthread_ptr, isMainBrowserThread, isMainRuntimeThread, canBlock); + }; modulePThread.loadWasmModuleToWorker = (worker: PThreadWorker): Promise => { const afterLoaded = originalLoadWasmModuleToWorker(worker); diff --git a/src/mono/browser/runtime/pthreads/worker/index.ts b/src/mono/browser/runtime/pthreads/worker/index.ts index 568bf51008292..29229c23e5fd8 100644 --- a/src/mono/browser/runtime/pthreads/worker/index.ts +++ b/src/mono/browser/runtime/pthreads/worker/index.ts @@ -20,6 +20,7 @@ import { postRunWorker, preRunWorker } from "../../startup"; import { mono_log_debug, mono_log_error } from "../../logging"; import { CharPtr } from "../../types/emscripten"; import { utf8ToString } from "../../strings"; +import { forceThreadMemoryViewRefresh } from "../../memory"; // re-export some of the events types export { @@ -85,6 +86,10 @@ function monoDedicatedChannelMessageFromMainToWorker(event: MessageEvent mono_log_debug("got message from main on the dedicated channel", event.data); } +export function onRunMessage(pthread_ptr: PThreadPtr) { + monoThreadInfo.pthreadId = pthread_ptr; + forceThreadMemoryViewRefresh(); +} /// Called by emscripten when a pthread is setup to run on a worker. Can be called multiple times /// for the same webworker, since emscripten can reuse workers. @@ -92,6 +97,8 @@ function monoDedicatedChannelMessageFromMainToWorker(event: MessageEvent export function mono_wasm_pthread_on_pthread_created(): void { if (!WasmEnableThreads) return; try { + forceThreadMemoryViewRefresh(); + const pthread_id = mono_wasm_pthread_ptr(); mono_assert(!is_nullish(pthread_id), "pthread_self() returned null"); monoThreadInfo.pthreadId = pthread_id; diff --git a/src/mono/browser/runtime/scheduling.ts b/src/mono/browser/runtime/scheduling.ts index 69412b7947329..4651516271fe6 100644 --- a/src/mono/browser/runtime/scheduling.ts +++ b/src/mono/browser/runtime/scheduling.ts @@ -6,6 +6,7 @@ import WasmEnableThreads from "consts:wasmEnableThreads"; import cwraps from "./cwraps"; import { ENVIRONMENT_IS_WORKER, Module, loaderHelpers } from "./globals"; import { is_thread_available } from "./pthreads/shared/emscripten-replacements"; +import { forceThreadMemoryViewRefresh } from "./memory"; let spread_timers_maximum = 0; let pump_count = 0; @@ -43,6 +44,9 @@ function mono_background_exec_until_done() { if (!loaderHelpers.is_runtime_running()) { return; } + if (WasmEnableThreads) { + forceThreadMemoryViewRefresh(); + } while (pump_count > 0) { --pump_count; cwraps.mono_background_exec(); @@ -85,6 +89,9 @@ export function mono_wasm_schedule_timer(shortestDueTimeMs: number): void { function mono_wasm_schedule_timer_tick() { Module.maybeExit(); + if (WasmEnableThreads) { + forceThreadMemoryViewRefresh(); + } if (!loaderHelpers.is_runtime_running()) { return; }