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] try to fix "Invalid atomic access index" #98608

Merged
merged 1 commit into from
Feb 17, 2024
Merged
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
5 changes: 4 additions & 1 deletion src/mono/browser/runtime/invoke-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<PThreadWorker> => {
const afterLoaded = originalLoadWasmModuleToWorker(worker);
Expand Down
7 changes: 7 additions & 0 deletions src/mono/browser/runtime/pthreads/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -85,13 +86,19 @@ function monoDedicatedChannelMessageFromMainToWorker(event: MessageEvent<string>
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.
/// This is an implementation detail, that shouldn't be used directly.
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;
Expand Down
7 changes: 7 additions & 0 deletions src/mono/browser/runtime/scheduling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
Loading