Skip to content

Commit

Permalink
proxy pthread worker messages to websocket, if enabled
Browse files Browse the repository at this point in the history
use a new MonoThreadMessageApplyMonoConfig message to send
the MonoConfig from the main thread to each worker when the workers
set up the communication channel to the main thread.

then if the diagnosticTracing property is true, redirect the worker
console logging to a websocket.

Fixes dotnet#72606
  • Loading branch information
lambdageek committed Aug 13, 2022
1 parent 8c686a2 commit aa2d070
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/mono/wasm/runtime/pthreads/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { MonoWorkerMessageChannelCreated, isMonoWorkerMessageChannelCreated, monoSymbol } from "../shared";
import { MonoWorkerMessageChannelCreated, isMonoWorkerMessageChannelCreated, monoSymbol, makeMonoThreadMessageApplyMonoConfig } from "../shared";
import { pthread_ptr } from "../shared/types";
import { MonoThreadMessage } from "../shared";
import { PromiseController, createPromiseController } from "../../promise-controller";
import { MonoConfig, mono_assert } from "../../types";
import Internals from "../shared/emscripten-internals";
import { runtimeHelpers } from "../../imports";

const threads: Map<pthread_ptr, Thread> = new Map();

Expand Down Expand Up @@ -94,6 +95,7 @@ function monoWorkerMessageHandler(worker: Worker, ev: MessageEvent<MonoWorkerMes
const thread = addThread(pthread_id, worker, port);
port.addEventListener("message", (ev) => monoDedicatedChannelMessageFromWorkerToMain(ev, thread));
port.start();
port.postMessage(makeMonoThreadMessageApplyMonoConfig(runtimeHelpers.config));
resolvePromises(pthread_id, thread);
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/mono/wasm/runtime/pthreads/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

import { Module } from "../../imports";
import { MonoConfig } from "../../types";
import { pthread_ptr } from "./types";

export interface PThreadInfo {
Expand Down Expand Up @@ -42,6 +43,29 @@ export function isMonoThreadMessage(x: unknown): x is MonoThreadMessage {
return typeof (xmsg.type) === "string" && typeof (xmsg.cmd) === "string";
}

// message from the main thread to the pthread worker that passes the MonoConfig to the worker
export interface MonoThreadMessageApplyMonoConfig extends MonoThreadMessage {
type: "pthread";
cmd: "apply_mono_config";
config: string;
}

export function isMonoThreadMessageApplyMonoConfig(x: unknown): x is MonoThreadMessageApplyMonoConfig {
if (!isMonoThreadMessage(x)) {
return false;
}
const xmsg = x as MonoThreadMessageApplyMonoConfig;
return xmsg.type === "pthread" && xmsg.cmd === "apply_mono_config" && typeof (xmsg.config) === "string";
}

export function makeMonoThreadMessageApplyMonoConfig(config: MonoConfig): MonoThreadMessageApplyMonoConfig {
return {
type: "pthread",
cmd: "apply_mono_config",
config: JSON.stringify(config)
};
}

/// Messages sent using the worker object's postMessage() method ///

/// a symbol that we use as a key on messages on the global worker-to-main channel to identify our own messages
Expand Down
30 changes: 28 additions & 2 deletions src/mono/wasm/runtime/pthreads/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import MonoWasmThreads from "consts:monoWasmThreads";
import { Module, ENVIRONMENT_IS_PTHREAD } from "../../imports";
import { makeChannelCreatedMonoMessage } from "../shared";
import { isMonoThreadMessageApplyMonoConfig, makeChannelCreatedMonoMessage } from "../shared";
import type { pthread_ptr } from "../shared/types";
import { mono_assert, is_nullish } from "../../types";
import { mono_assert, is_nullish, MonoConfig } from "../../types";
import type { MonoThreadMessage } from "../shared";
import {
PThreadSelf,
Expand All @@ -16,6 +16,7 @@ import {
dotnetPthreadAttached,
WorkerThreadEventTarget
} from "./events";
import { setup_proxy_console } from "../../logging";

// re-export some of the events types
export {
Expand Down Expand Up @@ -54,7 +55,16 @@ export let pthread_self: PThreadSelf = null as any as PThreadSelf;
export const currentWorkerThreadEvents: WorkerThreadEventTarget =
MonoWasmThreads ? new EventTarget() : null as any as WorkerThreadEventTarget; // treeshake if threads are disabled


// this is the message handler for the worker that receives messages from the main thread
// extend this with new cases as needed
function monoDedicatedChannelMessageFromMainToWorker(event: MessageEvent<string>): void {
if (isMonoThreadMessageApplyMonoConfig(event.data)) {
const config = JSON.parse(event.data.config) as MonoConfig;
console.debug("MONO_WASM: applying mono config from main", event.data.config);
onMonoConfigReceived(config);
return;
}
console.debug("MONO_WASM: got message from main on the dedicated channel", event.data);
}

Expand All @@ -70,6 +80,22 @@ function setupChannelToMainThread(pthread_ptr: pthread_ptr): PThreadSelf {
return pthread_self;
}

// TODO: should we just assign to Module.config here?
let workerMonoConfig: MonoConfig = null as unknown as MonoConfig;

// called when the main thread sends us the mono config
function onMonoConfigReceived(config: MonoConfig): void {
if (workerMonoConfig !== null) {
console.debug("MONO_WASM: mono config already received");
return;
}
workerMonoConfig = config;
console.debug("MONO_WASM: mono config received", config);
if (workerMonoConfig.diagnosticTracing) {
setup_proxy_console("pthread-worker", console, self.location.href);
}
}

/// This is an implementation detail function.
/// Called in the worker thread from mono when a pthread becomes attached to the mono runtime.
export function mono_wasm_pthread_on_pthread_attached(pthread_id: pthread_ptr): void {
Expand Down

0 comments on commit aa2d070

Please sign in to comment.