Skip to content

Commit 8763f6d

Browse files
authored
[browser][MT] smaller thread pool (#100415)
1 parent c5bead6 commit 8763f6d

10 files changed

Lines changed: 19 additions & 55 deletions

File tree

src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/JavaScriptImports.Generated.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ internal static unsafe partial class JavaScriptImports
4848
[JSImport("INTERNAL.mono_wasm_bind_cs_function")]
4949
public static partial void BindCSFunction(IntPtr monoMethod, string assemblyName, string namespaceName, string shortClassName, string methodName, int signatureHash, IntPtr signature);
5050

51-
#if FEATURE_WASM_MANAGED_THREADS
52-
[JSImport("INTERNAL.thread_available")]
53-
public static partial Task ThreadAvailable();
54-
#endif
55-
5651
#if DEBUG
5752
[JSImport("globalThis.console.log")]
5853
[return: JSMarshalAs<JSType.DiscardNoWait>] // this means that the message will arrive out of order, especially across threads.

src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSWebWorker.cs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,7 @@ public JSWebWorkerInstance(Func<Task<T>> body, CancellationToken cancellationTok
7676

7777
public Task<T> Start()
7878
{
79-
if (JSProxyContext.MainThreadContext.IsCurrentThread())
80-
{
81-
// give browser chance to load more threads
82-
// until there at least one thread loaded, it doesn't make sense to `Start`
83-
// because that would also hang, but in a way blocking the UI thread, much worse.
84-
JavaScriptImports.ThreadAvailable().ContinueWith(static (t, o) =>
85-
{
86-
var self = (JSWebWorkerInstance<T>)o!;
87-
if (t.IsCompletedSuccessfully)
88-
{
89-
self._thread.Start();
90-
}
91-
if (t.IsCanceled)
92-
{
93-
throw new OperationCanceledException("Cancelled while waiting for underlying WebWorker to become available.", self._cancellationToken);
94-
}
95-
throw t.Exception!;
96-
// ideally this will execute on UI thread quickly: ExecuteSynchronously
97-
}, this, _cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.FromCurrentSynchronizationContext());
98-
}
99-
else
100-
{
101-
_thread.Start();
102-
}
79+
_thread.Start();
10380
return _taskCompletionSource.Task;
10481
}
10582

src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/System.Threading.Tasks.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77
<PropertyGroup Condition="'$(TargetOS)' == 'browser'">
88
<XunitShowProgress>true</XunitShowProgress>
9+
<_WasmPThreadPoolUnusedSize>10</_WasmPThreadPoolUnusedSize>
910
</PropertyGroup>
1011
<ItemGroup>
1112
<Compile Include="Helpers.cs" />

src/libraries/System.Threading.Thread/tests/System.Threading.Thread.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
66
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
77
</PropertyGroup>
8+
<PropertyGroup Condition="'$(TargetOS)' == 'browser'">
9+
<XunitShowProgress>true</XunitShowProgress>
10+
<_WasmPThreadPoolUnusedSize>10</_WasmPThreadPoolUnusedSize>
11+
</PropertyGroup>
812
<ItemGroup>
913
<Compile Include="CompressedStackTests.cs" />
1014
<Compile Include="ExceptionTests.cs" />

src/libraries/System.Threading.ThreadPool/tests/System.Threading.ThreadPool.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
55
<TestRuntime>true</TestRuntime>
66
</PropertyGroup>
7+
<PropertyGroup Condition="'$(TargetOS)' == 'browser'">
8+
<XunitShowProgress>true</XunitShowProgress>
9+
<_WasmPThreadPoolUnusedSize>10</_WasmPThreadPoolUnusedSize>
10+
</PropertyGroup>
711
<ItemGroup>
812
<Compile Include="ThreadPoolTests.cs" />
913
<Compile Include="RegisteredWaitTests.cs" />

src/libraries/System.Threading/tests/System.Threading.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</PropertyGroup>
1010
<PropertyGroup Condition="'$(TargetOS)' == 'browser'">
1111
<XunitShowProgress>true</XunitShowProgress>
12+
<_WasmPThreadPoolUnusedSize>10</_WasmPThreadPoolUnusedSize>
1213
</PropertyGroup>
1314
<ItemGroup>
1415
<Compile Include="AsyncLocalTests.cs" />

src/mono/browser/runtime/exports-internal.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { mono_wasm_get_func_id_to_name_mappings } from "./logging";
2323
import { monoStringToStringUnsafe } from "./strings";
2424
import { mono_wasm_bind_cs_function } from "./invoke-cs";
2525

26-
import { mono_wasm_dump_threads, thread_available } from "./pthreads";
26+
import { mono_wasm_dump_threads } from "./pthreads";
2727

2828
export function export_internal (): any {
2929
return {
@@ -63,7 +63,6 @@ export function export_internal (): any {
6363
get_global_this,
6464
get_dotnet_instance: () => exportedRuntimeAPI,
6565
dynamic_import,
66-
thread_available: WasmEnableThreads ? thread_available : undefined,
6766
mono_wasm_bind_cs_function,
6867

6968
// BrowserWebSocket

src/mono/browser/runtime/loader/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ export function normalizeConfig () {
190190
if (WasmEnableThreads) {
191191

192192
if (!Number.isInteger(config.pthreadPoolInitialSize)) {
193-
config.pthreadPoolInitialSize = 7;
193+
config.pthreadPoolInitialSize = 5;
194194
}
195195
if (!Number.isInteger(config.pthreadPoolUnusedSize)) {
196-
config.pthreadPoolUnusedSize = 3;
196+
config.pthreadPoolUnusedSize = 1;
197197
}
198198
if (!Number.isInteger(config.finalizerThreadStartDelayMs)) {
199199
config.finalizerThreadStartDelayMs = 200;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export {
66
mono_wasm_pthread_ptr, update_thread_info, isMonoThreadMessage, monoThreadInfo,
77
} from "./shared";
88
export {
9-
mono_wasm_dump_threads, thread_available, cancelThreads, is_thread_available,
9+
mono_wasm_dump_threads, cancelThreads, is_thread_available,
1010
populateEmscriptenPool, mono_wasm_init_threads, init_finalizer_thread,
1111
waitForThread, replaceEmscriptenPThreadUI
1212
} from "./ui-thread";

src/mono/browser/runtime/pthreads/ui-thread.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
55
import BuildConfiguration from "consts:configuration";
66

77
import { } from "../globals";
8-
import { mono_log_debug, mono_log_warn } from "../logging";
98
import { MonoWorkerToMainMessage, monoThreadInfo, mono_wasm_pthread_ptr, update_thread_info, worker_empty_prefix } from "./shared";
109
import { Module, ENVIRONMENT_IS_WORKER, createPromiseController, loaderHelpers, mono_assert, runtimeHelpers } from "../globals";
11-
import { PThreadLibrary, MainToWorkerMessageType, MonoThreadMessage, PThreadInfo, PThreadPtr, PThreadPtrNull, PThreadWorker, PromiseAndController, PromiseController, Thread, WorkerToMainMessageType, monoMessageSymbol } from "../types/internal";
12-
import { mono_log_error, mono_log_info } from "../logging";
10+
import { PThreadLibrary, MainToWorkerMessageType, MonoThreadMessage, PThreadInfo, PThreadPtr, PThreadPtrNull, PThreadWorker, PromiseController, Thread, WorkerToMainMessageType, monoMessageSymbol } from "../types/internal";
11+
import { mono_log_error, mono_log_info, mono_log_debug } from "../logging";
1312
import { threads_c_functions as cwraps } from "../cwraps";
1413

1514
const threadPromises: Map<PThreadPtr, PromiseController<Thread>[]> = new Map();
@@ -119,32 +118,16 @@ function monoWorkerMessageHandler (worker: PThreadWorker, ev: MessageEvent<any>)
119118
}
120119
}
121120

122-
let pendingWorkerLoad: PromiseAndController<void> | undefined;
123-
124121
/// Called by Emscripten internals on the browser thread when a new pthread worker is created and added to the pthread worker pool.
125122
/// At this point the worker doesn't have any pthread assigned to it, yet.
126123
export function onWorkerLoadInitiated (worker: PThreadWorker, loaded: Promise<Worker>): void {
127124
if (!WasmEnableThreads) return;
128125
worker.addEventListener("message", (ev) => monoWorkerMessageHandler(worker, ev));
129-
if (pendingWorkerLoad == undefined) {
130-
pendingWorkerLoad = createPromiseController<void>();
131-
}
132126
loaded.then(() => {
133127
worker.info.isLoaded = true;
134-
if (pendingWorkerLoad != undefined) {
135-
pendingWorkerLoad.promise_control.resolve();
136-
pendingWorkerLoad = undefined;
137-
}
138128
});
139129
}
140130

141-
export function thread_available (): Promise<void> {
142-
if (!WasmEnableThreads) return null as any;
143-
if (pendingWorkerLoad == undefined) {
144-
return Promise.resolve();
145-
}
146-
return pendingWorkerLoad.promise;
147-
}
148131

149132
export function populateEmscriptenPool (): void {
150133
if (!WasmEnableThreads) return;
@@ -295,7 +278,7 @@ function getNewWorker (modulePThread: PThreadLibrary): PThreadWorker {
295278
if (!WasmEnableThreads) return null as any;
296279

297280
if (modulePThread.unusedWorkers.length == 0) {
298-
mono_log_warn(`Failed to find unused WebWorker, this may deadlock. Please increase the pthreadPoolReady. Running threads ${modulePThread.runningWorkers.length}. Loading workers: ${modulePThread.unusedWorkers.length}`);
281+
mono_log_debug(`Failed to find unused WebWorker, this may deadlock. Please increase the pthreadPoolReady. Running threads ${modulePThread.runningWorkers.length}. Loading workers: ${modulePThread.unusedWorkers.length}`);
299282
const worker = allocateUnusedWorker();
300283
modulePThread.loadWasmModuleToWorker(worker);
301284
availableThreadCount--;
@@ -316,7 +299,7 @@ function getNewWorker (modulePThread: PThreadLibrary): PThreadWorker {
316299
return worker;
317300
}
318301
}
319-
mono_log_warn(`Failed to find loaded WebWorker, this may deadlock. Please increase the pthreadPoolReady. Running threads ${modulePThread.runningWorkers.length}. Loading workers: ${modulePThread.unusedWorkers.length}`);
302+
mono_log_debug(`Failed to find loaded WebWorker, this may deadlock. Please increase the pthreadPoolReady. Running threads ${modulePThread.runningWorkers.length}. Loading workers: ${modulePThread.unusedWorkers.length}`);
320303
availableThreadCount--; // negative value
321304
return modulePThread.unusedWorkers.pop()!;
322305
}

0 commit comments

Comments
 (0)