Skip to content

Commit d06742c

Browse files
authored
try to fix #96545 (#98608)
1 parent 1ad22a5 commit d06742c

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

src/mono/browser/runtime/invoke-js.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import BuildConfiguration from "consts:configuration";
66

77
import { marshal_exception_to_cs, bind_arg_marshal_to_cs } from "./marshal-to-cs";
88
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";
9-
import { setI32_unchecked, receiveWorkerHeapViews } from "./memory";
9+
import { setI32_unchecked, receiveWorkerHeapViews, forceThreadMemoryViewRefresh } from "./memory";
1010
import { stringToMonoStringRoot } from "./strings";
1111
import { MonoObject, MonoObjectRef, JSFunctionSignature, JSMarshalerArguments, WasmRoot, BoundMarshalerToJs, JSFnHandle, BoundMarshalerToCs, JSHandle, MarshalerType } from "./types/internal";
1212
import { Int32Ptr } from "./types/emscripten";
@@ -52,6 +52,9 @@ export function mono_wasm_invoke_import_async(args: JSMarshalerArguments, signat
5252
let max_postpone_count = 10;
5353
function postpone_invoke_import_async() {
5454
if (max_postpone_count < 0 || is_thread_available()) {
55+
if (WasmEnableThreads) {
56+
forceThreadMemoryViewRefresh();
57+
}
5558
bound_fn(args);
5659
Module._free(args as any);
5760
} else {

src/mono/browser/runtime/pthreads/shared/emscripten-replacements.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
55
import BuildConfiguration from "consts:configuration";
66

77
import { dumpThreads, onWorkerLoadInitiated, resolveThreadPromises } from "../browser";
8-
import { mono_wasm_pthread_on_pthread_created } from "../worker";
8+
import { mono_wasm_pthread_on_pthread_created, onRunMessage } from "../worker";
99
import { PThreadLibrary, PThreadWorker, getModulePThread, getUnusedWorkerPool } from "./emscripten-internals";
10-
import { loaderHelpers, mono_assert } from "../../globals";
10+
import { Module, loaderHelpers, mono_assert } from "../../globals";
1111
import { mono_log_warn } from "../../logging";
12-
import { PThreadPtrNull } from "./types";
12+
import { PThreadPtr, PThreadPtrNull } from "./types";
1313

1414
/** @module emscripten-replacements Replacements for individual functions in the emscripten PThreads library.
1515
* 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):
2222
const originalLoadWasmModuleToWorker = modulePThread.loadWasmModuleToWorker;
2323
const originalThreadInitTLS = modulePThread.threadInitTLS;
2424
const originalReturnWorkerToPool = modulePThread.returnWorkerToPool;
25+
const original_emscripten_thread_init = (Module as any)["__emscripten_thread_init"];
26+
27+
28+
(Module as any)["__emscripten_thread_init"] = (pthread_ptr: PThreadPtr, isMainBrowserThread: number, isMainRuntimeThread: number, canBlock: number) => {
29+
onRunMessage(pthread_ptr);
30+
original_emscripten_thread_init(pthread_ptr, isMainBrowserThread, isMainRuntimeThread, canBlock);
31+
};
2532

2633
modulePThread.loadWasmModuleToWorker = (worker: PThreadWorker): Promise<PThreadWorker> => {
2734
const afterLoaded = originalLoadWasmModuleToWorker(worker);

src/mono/browser/runtime/pthreads/worker/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { postRunWorker, preRunWorker } from "../../startup";
2020
import { mono_log_debug, mono_log_error } from "../../logging";
2121
import { CharPtr } from "../../types/emscripten";
2222
import { utf8ToString } from "../../strings";
23+
import { forceThreadMemoryViewRefresh } from "../../memory";
2324

2425
// re-export some of the events types
2526
export {
@@ -85,13 +86,19 @@ function monoDedicatedChannelMessageFromMainToWorker(event: MessageEvent<string>
8586
mono_log_debug("got message from main on the dedicated channel", event.data);
8687
}
8788

89+
export function onRunMessage(pthread_ptr: PThreadPtr) {
90+
monoThreadInfo.pthreadId = pthread_ptr;
91+
forceThreadMemoryViewRefresh();
92+
}
8893

8994
/// Called by emscripten when a pthread is setup to run on a worker. Can be called multiple times
9095
/// for the same webworker, since emscripten can reuse workers.
9196
/// This is an implementation detail, that shouldn't be used directly.
9297
export function mono_wasm_pthread_on_pthread_created(): void {
9398
if (!WasmEnableThreads) return;
9499
try {
100+
forceThreadMemoryViewRefresh();
101+
95102
const pthread_id = mono_wasm_pthread_ptr();
96103
mono_assert(!is_nullish(pthread_id), "pthread_self() returned null");
97104
monoThreadInfo.pthreadId = pthread_id;

src/mono/browser/runtime/scheduling.ts

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
66
import cwraps from "./cwraps";
77
import { ENVIRONMENT_IS_WORKER, Module, loaderHelpers } from "./globals";
88
import { is_thread_available } from "./pthreads/shared/emscripten-replacements";
9+
import { forceThreadMemoryViewRefresh } from "./memory";
910

1011
let spread_timers_maximum = 0;
1112
let pump_count = 0;
@@ -43,6 +44,9 @@ function mono_background_exec_until_done() {
4344
if (!loaderHelpers.is_runtime_running()) {
4445
return;
4546
}
47+
if (WasmEnableThreads) {
48+
forceThreadMemoryViewRefresh();
49+
}
4650
while (pump_count > 0) {
4751
--pump_count;
4852
cwraps.mono_background_exec();
@@ -85,6 +89,9 @@ export function mono_wasm_schedule_timer(shortestDueTimeMs: number): void {
8589

8690
function mono_wasm_schedule_timer_tick() {
8791
Module.maybeExit();
92+
if (WasmEnableThreads) {
93+
forceThreadMemoryViewRefresh();
94+
}
8895
if (!loaderHelpers.is_runtime_running()) {
8996
return;
9097
}

0 commit comments

Comments
 (0)