Skip to content

Commit

Permalink
feat: Restore threading
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Feb 28, 2025
1 parent 4190eb9 commit 8a7e974
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 41 deletions.
2 changes: 0 additions & 2 deletions build/ci/stage-build-linux-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -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)"
Expand Down
2 changes: 0 additions & 2 deletions build/ci/stage-build-windows-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ jobs:
"http://localhost:8000/"
displayName: Threading Tests
condition: eq( variables['THREAD_TESTING'], 'true' )
env:
BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)"
Expand All @@ -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)"
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
<WasmEnableThreads Condition=" '$(WasmShellEnableThreads)' == 'true' ">true</WasmEnableThreads>

<!-- https://github.com/dotnet/runtime/blob/1231a9addd0fb3cfb806c2a89cb45a3018c48bb2/src/mono/browser/build/BrowserWasmApp.targets#L125-L126 -->
<_WasmPThreadPoolInitialSize Condition=" '$(WasmShellPThreadsPoolSize)' == 'true' ">true</_WasmPThreadPoolInitialSize>
<_WasmPThreadPoolInitialSize Condition=" '$(WasmShellPThreadsPoolSize)' == '' ">2</_WasmPThreadPoolInitialSize>

<!-- Native build compatibility -->
<WasmBuildNative Condition="
Expand Down
20 changes: 8 additions & 12 deletions src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/HotReloadSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ namespace Uno.WebAssembly.Bootstrap {
(<any>this._context).config.environmentVariables['ASPNETCORE-BROWSER-TOOLS']
|| (<any>this._context).config.environmentVariables['__ASPNETCORE_BROWSER_TOOLS'];

if (!await HotReloadSupport._initializeMethod()) {
console.warn("The application was compiled with the IL linker enabled, hot reload is disabled. See https://aka.platform.uno/wasm-il-linker for more details.");
}

let capabilities = await HotReloadSupport._getApplyUpdateCapabilitiesMethod();

// Take the place of the internal .NET for WebAssembly APIs for metadata updates coming
// from the "BrowserLink" feature.
(function (Blazor) {
Blazor._internal = {
initialize: function () {
if (!HotReloadSupport._initializeMethod()) {
console.warn("The application was compiled with the IL linker enabled, hot reload is disabled. See https://aka.platform.uno/wasm-il-linker for more details.");
}
},

applyExisting: async function (): Promise<void> {

if (browserToolsVariable == "true")
Expand All @@ -62,19 +62,15 @@ namespace Uno.WebAssembly.Bootstrap {
},

getApplyUpdateCapabilities: function () {
Blazor._internal.initialize();
return HotReloadSupport._getApplyUpdateCapabilitiesMethod();
return capabilities;
},

applyHotReload: function (moduleId: any, metadataDelta: any, ilDelta: any, pdbDelta: any, updatedTypes: any) {
Blazor._internal.initialize();
return HotReloadSupport._applyHotReloadDeltaMethod(moduleId, metadataDelta, ilDelta, pdbDelta || "", updatedTypes || []);
HotReloadSupport._applyHotReloadDeltaMethod(moduleId, metadataDelta, ilDelta, pdbDelta || "", updatedTypes || []);
}
};
})((<any>window).Blazor || ((<any>window).Blazor = {}));

// Apply pending updates caused by a browser refresh
(<any>window).Blazor._internal.initialize();
await (<any>window).Blazor._internal.applyExisting();
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Uno.Wasm.MetadataUpdater/WebAssemblyHotReload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static partial class WebAssemblyHotReload
};

[JSExport]
internal static bool Initialize()
internal static async Task<bool> Initialize()
{
bool.TryParse(Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_LOG_METADATA_UPDATES"), out _debugEnabled);

Expand Down Expand Up @@ -62,7 +62,7 @@ internal static bool Initialize()
/// For framework use only.
/// </summary>
[JSExport]
public static void ApplyHotReloadDelta(string moduleIdString, string metadataDelta, string ilDeta, string pdbDelta, int[] updatedTypes)
public static async Task ApplyHotReloadDelta(string moduleIdString, string metadataDelta, string ilDeta, string pdbDelta, int[] updatedTypes)
{
if (_linkerEnabled)
{
Expand Down Expand Up @@ -93,7 +93,7 @@ public static void ApplyHotReloadDelta(string moduleIdString, string metadataDel
/// For framework use only.
/// </summary>
[JSExport]
public static string GetApplyUpdateCapabilities()
public static async Task<string> GetApplyUpdateCapabilities()
{
if (_debugEnabled)
{
Expand Down
23 changes: 10 additions & 13 deletions src/Uno.Wasm.Threads.Shared/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public static class Program
private static List<string> _messages = new List<string>();
private static bool _mainThreadInvoked = false;
private static Timer _timer;
private static SynchronizationContext _jsContext = SynchronizationContext.Current;

static void Main()
{
var runtimeMode = Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_MODE");
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')");

Expand Down Expand Up @@ -81,7 +83,7 @@ void logMessage(string message)
int index = Interlocked.Increment(ref counter);
lock (_gate)
{
_messages.Add($"[tid:{Thread.CurrentThread.ManagedThreadId}]: {index} / {message}");
_messages.Add($"[tid:{Thread.CurrentThread.ManagedThreadId}][ctx:{SynchronizationContext.Current?.ToString()}]: {index} / {message}");
}
}

Expand All @@ -92,34 +94,29 @@ 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}]");
Console.WriteLine($"Starting thread [tid:{Thread.CurrentThread.ManagedThreadId}] [ctx:{SynchronizationContext.Current?.ToString()}]");

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;

_event.Set();
}
}
}

namespace WebAssembly.JSInterop
{
public static class InternalCalls
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern void InvokeOnMainThread();
}
}
7 changes: 0 additions & 7 deletions src/Uno.Wasm.Threads.Shared/WasmScripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
3 changes: 2 additions & 1 deletion src/Uno.Wasm.Threads/Uno.Wasm.Threads.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.WebAssembly">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<StartupObject>Uno.Wasm.Sample.Program</StartupObject>
<MonoRuntimeDebuggerEnabled Condition="'$(Configuration)'=='Debug'">true</MonoRuntimeDebuggerEnabled>
<!--<WasmShellMonoRuntimeExecutionMode Condition="$([MSBuild]::IsOsPlatform('Linux'))">FullAOT</WasmShellMonoRuntimeExecutionMode>-->
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<WasmShellEnableThreads>true</WasmShellEnableThreads>
<!--<WasmBuildNative>true</WasmBuildNative>-->
<!--<WasmShellILLinkerEnabled>false</WasmShellILLinkerEnabled>-->
</PropertyGroup>

Expand Down

0 comments on commit 8a7e974

Please sign in to comment.