From 2cab5f3c2addfcac7bf37436ef99d609760c86c7 Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Fri, 27 Sep 2024 22:52:32 -0400 Subject: [PATCH 1/4] fix: Restore threading tests --- build/ci/stage-build-linux-tests.yml | 2 -- build/ci/stage-build-windows-tests.yml | 2 -- src/Uno.Wasm.Threads.Shared/Program.cs | 19 ++++++++----------- .../WasmScripts/test.js | 7 ------- 4 files changed, 8 insertions(+), 22 deletions(-) diff --git a/build/ci/stage-build-linux-tests.yml b/build/ci/stage-build-linux-tests.yml index 380ec5ac8..40263bb5a 100644 --- a/build/ci/stage-build-linux-tests.yml +++ b/build/ci/stage-build-linux-tests.yml @@ -66,7 +66,6 @@ jobs: "$(build.sourcesdirectory)/src/Uno.Wasm.Threads/bin/Release/net9.0/publish/wwwroot" \ "$(build.sourcesdirectory)/src/Uno.Wasm.Threading.UITests" "http://localhost:8000/" displayName: Threading UI Tests - condition: eq( variables['THREAD_TESTING'], 'true' ) env: BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)" @@ -82,7 +81,6 @@ jobs: "$(build.sourcesdirectory)/src/Uno.Wasm.Threads.Aot/bin/Release/net9.0/publish/wwwroot" \ "$(build.sourcesdirectory)/src/Uno.Wasm.Threading.UITests" "http://localhost:8000/" displayName: Threading AOT UI Tests - condition: eq( variables['THREAD_TESTING'], 'true' ) env: BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)" diff --git a/build/ci/stage-build-windows-tests.yml b/build/ci/stage-build-windows-tests.yml index d73463285..69313ce39 100644 --- a/build/ci/stage-build-windows-tests.yml +++ b/build/ci/stage-build-windows-tests.yml @@ -241,7 +241,6 @@ jobs: "http://localhost:8000/" displayName: Threading Tests - condition: eq( variables['THREAD_TESTING'], 'true' ) env: BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)" @@ -258,7 +257,6 @@ jobs: "$(build.sourcesdirectory)\src\Uno.Wasm.Threading.UITests" ` "http://localhost:8000/" displayName: Threading AOT Tests - condition: eq( variables['THREAD_TESTING'], 'true' ) env: BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)" diff --git a/src/Uno.Wasm.Threads.Shared/Program.cs b/src/Uno.Wasm.Threads.Shared/Program.cs index a330f9dd2..e5e6f69e4 100644 --- a/src/Uno.Wasm.Threads.Shared/Program.cs +++ b/src/Uno.Wasm.Threads.Shared/Program.cs @@ -34,6 +34,7 @@ public static class Program private static List _messages = new List(); private static bool _mainThreadInvoked = false; private static Timer _timer; + private static SynchronizationContext _jsContext = SynchronizationContext.Current; static void Main() { @@ -41,6 +42,7 @@ static void Main() Console.WriteLine($"Runtime Version: " + RuntimeInformation.FrameworkDescription); Console.WriteLine($"Runtime Mode: " + runtimeMode); Console.WriteLine($"TID: {Thread.CurrentThread.ManagedThreadId}"); + Console.WriteLine($"SynchronizationContext: {_jsContext}"); Runtime.InvokeJS("Interop.appendResult('Startup')"); @@ -92,21 +94,25 @@ void DoWork(string name) if ((i % 2000) == 0) { - WebAssembly.JSInterop.InternalCalls.InvokeOnMainThread(); + Console.WriteLine("InvokeOnMainThread1"); + _jsContext.Post(_ => MainThreadCallback(), null); } } } +#pragma warning disable CA1416 // Validate platform compatibility new Thread(_ => { Console.WriteLine($"Starting thread [tid:{Thread.CurrentThread.ManagedThreadId}]"); - WebAssembly.JSInterop.InternalCalls.InvokeOnMainThread(); + _jsContext.Post(_ => MainThreadCallback(), null); + Console.WriteLine("InvokeOnMainThread2"); DoWork("thread1"); tcs.TrySetResult(true); Console.WriteLine($"Stopping thread [tid:{Thread.CurrentThread.ManagedThreadId}]"); }).Start(); +#pragma warning restore CA1416 // Validate platform compatibility await tcs.Task; @@ -114,12 +120,3 @@ void DoWork(string name) } } } - -namespace WebAssembly.JSInterop -{ - public static class InternalCalls - { - [MethodImplAttribute(MethodImplOptions.InternalCall)] - public static extern void InvokeOnMainThread(); - } -} diff --git a/src/Uno.Wasm.Threads.Shared/WasmScripts/test.js b/src/Uno.Wasm.Threads.Shared/WasmScripts/test.js index 8c378a488..a45bb99eb 100644 --- a/src/Uno.Wasm.Threads.Shared/WasmScripts/test.js +++ b/src/Uno.Wasm.Threads.Shared/WasmScripts/test.js @@ -9,10 +9,3 @@ var Interop = { parent.appendChild(txt, parent.lastChild); } }; - -var DotNet = { - invokeOnMainThread: function (str) { - let getApplyUpdateCapabilitiesMethod = BINDING.bind_static_method("[Uno.Wasm.Threads] Uno.Wasm.Sample.Program:MainThreadCallback"); - getApplyUpdateCapabilitiesMethod(); - } -} From feb018d6a6d4614719d80f3ff354e0210165665c Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Mon, 30 Sep 2024 09:05:17 -0400 Subject: [PATCH 2/4] chore: Adjust main thread blocking mode --- .../ts/Uno/WebAssembly/Bootstrapper.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/Bootstrapper.ts b/src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/Bootstrapper.ts index ec4bccb50..df8688b25 100644 --- a/src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/Bootstrapper.ts +++ b/src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/Bootstrapper.ts @@ -102,7 +102,17 @@ namespace Uno.WebAssembly.Bootstrap { preRun: () => bootstrapper.wasmRuntimePreRun(), }) .withRuntimeOptions(config.config.uno_runtime_options) - .withConfig({ loadAllSatelliteResources: config.config.uno_loadAllSatelliteResources }); + + if (config.config.uno_loadAllSatelliteResources) { + m.dotnet + .withConfig({ loadAllSatelliteResources: config.config.uno_loadAllSatelliteResources }); + } + + var features = config.config.environmentVariables['UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES']; + if (features && features.includes('threads')) { + m.dotnet + .withConfig({ jsThreadBlockingMode: "DangerousAllowBlockingWait" }); + } const dotnetRuntime = await m.default( (context: DotnetPublicAPI) => { From b332cad3129b848da082c204a335ba905020ddee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Laban?= Date: Mon, 30 Sep 2024 11:03:20 -0400 Subject: [PATCH 3/4] chore: Adjust null checking --- src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/Bootstrapper.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/Bootstrapper.ts b/src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/Bootstrapper.ts index df8688b25..37fff5d6a 100644 --- a/src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/Bootstrapper.ts +++ b/src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/Bootstrapper.ts @@ -108,8 +108,8 @@ namespace Uno.WebAssembly.Bootstrap { .withConfig({ loadAllSatelliteResources: config.config.uno_loadAllSatelliteResources }); } - var features = config.config.environmentVariables['UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES']; - if (features && features.includes('threads')) { + var features = config.config.environmentVariables['UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES'] || ""; + if (features.includes('threads')) { m.dotnet .withConfig({ jsThreadBlockingMode: "DangerousAllowBlockingWait" }); } From 6bb78e3bfd9a73e7fa22ddbff108e61b166dc246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Laban?= Date: Mon, 30 Sep 2024 13:38:14 -0400 Subject: [PATCH 4/4] chore: Adjust threads detection --- src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets b/src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets index fb217b5b0..b7f01d0b4 100644 --- a/src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets +++ b/src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets @@ -304,7 +304,7 @@ EmccFlags="@(EmccFlags)" EmscriptenVersion="$(EmscriptenVersion)" EnableLogProfiler="$(WasmShellEnableLogProfiler)" - EnableThreads="@(WasmEnableThreads)" + EnableThreads="$(WasmEnableThreads)" EnableTracing="@(WasmEnableTracing)" GenerateAOTProfile="$(WasmShellGenerateAOTProfile)" IndexHtmlPath="$(WasmShellIndexHtmlPath)"