-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[wasm-mt] Use asset loading for dotnet.worker.js; update WasmAppBuilder #73697
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a90464e
[wasm-mt] Override Emscripten PThread.allocateUnusedWorker
lambdageek 673cd1f
Add IncludeThreadsWorker and PThreadPoolSize props to WasmAppBuilder
lambdageek e12de7b
Move emscripten PThread internals access to a separate module
lambdageek a0e71e4
Load js-module-threads asset in replacement allocateUnusedWorker
lambdageek 5db8e8c
Update samples to explicitly enable threading / perftracing
lambdageek af2b21a
tighten up Internals types
lambdageek f1c7d59
Merge remote-tracking branch 'origin/main' into wasm-mt-assets
lambdageek 0a8357b
apply review feedback
lambdageek e655a46
fix import
lambdageek 8c686a2
Apply suggestions from code review
lambdageek aa2d070
proxy pthread worker messages to websocket, if enabled
lambdageek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/mono/wasm/runtime/pthreads/shared/emscripten-internals.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
import { Module } from "../../imports"; | ||
import { pthread_ptr } from "./types"; | ||
|
||
/** @module emscripten-internals accessors to the functions in the emscripten PThreads library, including | ||
* the low-level representations of {@linkcode pthread_ptr} thread info structs, etc. | ||
* Additionally, note that some of these functions are replaced by {@linkcode file://./emscripten-replacements.ts}. | ||
* These have a hard dependency on the version of Emscripten that we are using and may need to be kept in sync with | ||
* {@linkcode file://./../../../emsdk/upstream/emscripten/src/library_pthread.js} | ||
*/ | ||
|
||
// This is what we know about the Emscripten PThread library | ||
interface PThreadLibrary { | ||
unusedWorkers: Worker[]; | ||
pthreads: PThreadInfoMap; | ||
allocateUnusedWorker: () => void; | ||
loadWasmModuleToWorker: (worker: Worker, onFinishedLoading?: (worker: Worker) => void) => void; | ||
} | ||
|
||
interface EmscriptenPThreadInfo { | ||
threadInfoStruct: pthread_ptr; | ||
} | ||
|
||
/// N.B. emscripten deletes the `pthread` property from the worker when it is not actively running a pthread | ||
interface PThreadWorker extends Worker { | ||
pthread: EmscriptenPThreadInfo; | ||
} | ||
|
||
interface PThreadObject { | ||
worker: PThreadWorker; | ||
} | ||
|
||
interface PThreadInfoMap { | ||
[key: pthread_ptr]: PThreadObject | undefined; | ||
} | ||
|
||
|
||
function isRunningPThreadWorker(w: Worker): w is PThreadWorker { | ||
return (<any>w).pthread !== undefined; | ||
} | ||
|
||
/// These utility functions dig into Emscripten internals | ||
const Internals = { | ||
get modulePThread(): PThreadLibrary { | ||
return (<any>Module).PThread as PThreadLibrary; | ||
}, | ||
getWorker: (pthread_ptr: pthread_ptr): PThreadWorker | undefined => { | ||
// see https://github.com/emscripten-core/emscripten/pull/16239 | ||
return Internals.modulePThread.pthreads[pthread_ptr]?.worker; | ||
}, | ||
getThreadId: (worker: Worker): pthread_ptr | undefined => { | ||
/// See library_pthread.js in Emscripten. | ||
/// They hang a "pthread" object from the worker if the worker is running a thread, and remove it when the thread stops by doing `pthread_exit` or when it's joined using `pthread_join`. | ||
if (!isRunningPThreadWorker(worker)) | ||
return undefined; | ||
const emscriptenThreadInfo = worker.pthread; | ||
return emscriptenThreadInfo.threadInfoStruct; | ||
}, | ||
allocateUnusedWorker: (): void => { | ||
/// See library_pthread.js in Emscripten. | ||
/// This function allocates a new worker and adds it to the pool of workers. | ||
/// It's called when the pool of workers is empty and a new thread is created. | ||
Internals.modulePThread.allocateUnusedWorker(); | ||
}, | ||
getUnusedWorkerPool: (): Worker[] => { | ||
return Internals.modulePThread.unusedWorkers; | ||
}, | ||
loadWasmModuleToWorker: (worker: Worker, onFinishedLoading: () => void): void => { | ||
Internals.modulePThread.loadWasmModuleToWorker(worker, onFinishedLoading); | ||
} | ||
}; | ||
|
||
|
||
export default Internals; |
44 changes: 44 additions & 0 deletions
44
src/mono/wasm/runtime/pthreads/shared/emscripten-replacements.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
import MonoWasmThreads from "consts:monoWasmThreads"; | ||
import { PThreadReplacements } from "../../types"; | ||
import { afterLoadWasmModuleToWorker } from "../browser"; | ||
import { afterThreadInitTLS } from "../worker"; | ||
import Internals from "./emscripten-internals"; | ||
import { resolve_asset_path } from "../../assets"; | ||
import { mono_assert } from "../../types"; | ||
import { runtimeHelpers } from "../../imports"; | ||
|
||
/** @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 | ||
* {@linkcode file://./../../../emsdk/upstream/emscripten/src/library_pthread.js} | ||
*/ | ||
|
||
export function replaceEmscriptenPThreadLibrary(replacements: PThreadReplacements): void { | ||
if (MonoWasmThreads) { | ||
const originalLoadWasmModuleToWorker = replacements.loadWasmModuleToWorker; | ||
replacements.loadWasmModuleToWorker = (worker: Worker, onFinishedLoading?: (worker: Worker) => void): void => { | ||
originalLoadWasmModuleToWorker(worker, onFinishedLoading); | ||
afterLoadWasmModuleToWorker(worker); | ||
}; | ||
const originalThreadInitTLS = replacements.threadInitTLS; | ||
replacements.threadInitTLS = (): void => { | ||
originalThreadInitTLS(); | ||
afterThreadInitTLS(); | ||
}; | ||
// const originalAllocateUnusedWorker = replacements.allocateUnusedWorker; | ||
replacements.allocateUnusedWorker = replacementAllocateUnusedWorker; | ||
} | ||
} | ||
|
||
/// We replace Module["PThreads"].allocateUnusedWorker with this version that knows about assets | ||
function replacementAllocateUnusedWorker(): void { | ||
if (runtimeHelpers.diagnosticTracing) | ||
console.debug("MONO_WASM: replacementAllocateUnusedWorker"); | ||
const asset = resolve_asset_path("js-module-threads"); | ||
const uri = asset.resolvedUrl; | ||
mono_assert(uri !== undefined, "could not resolve the uri for the js-module-threads asset"); | ||
const worker = new Worker(uri); | ||
Internals.getUnusedWorkerPool().push(worker); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.shared.json" | ||
"extends": "../../tsconfig.shared.json", | ||
"include": [ | ||
"../../**/*.ts", | ||
"../../**/*.d.ts" | ||
] | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
/// pthread_t in C | ||
export type pthread_ptr = number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should subscribe onError here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and maybe proxy the console in the worker, similar way how it was done in crypto worker. See how we passed the json.stringify(runtimeHelpers.config) and then only forward console when enabled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Emscripten installs an onerror handler of their own that will overwrite ours. we can set one up later (in
afterLoadWasmModuleToWorker
) but we should probably not use the worker onerror handler for anything that we want to handle - we should catch exceptions around our code and post it using our custom message channel. We have a task to do that in the tracking issue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, i'll see if I can add that over the weekend; if not, there's a task to do it in the tracking issue