Skip to content

Commit 8a7e974

Browse files
committed
feat: Restore threading
1 parent 4190eb9 commit 8a7e974

File tree

8 files changed

+24
-41
lines changed

8 files changed

+24
-41
lines changed

build/ci/stage-build-linux-tests.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ jobs:
6666
"$(build.sourcesdirectory)/src/Uno.Wasm.Threads/bin/Release/net9.0/publish/wwwroot" \
6767
"$(build.sourcesdirectory)/src/Uno.Wasm.Threading.UITests" "http://localhost:8000/"
6868
displayName: Threading UI Tests
69-
condition: eq( variables['THREAD_TESTING'], 'true' )
7069
env:
7170
BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)"
7271
@@ -82,7 +81,6 @@ jobs:
8281
"$(build.sourcesdirectory)/src/Uno.Wasm.Threads.Aot/bin/Release/net9.0/publish/wwwroot" \
8382
"$(build.sourcesdirectory)/src/Uno.Wasm.Threading.UITests" "http://localhost:8000/"
8483
displayName: Threading AOT UI Tests
85-
condition: eq( variables['THREAD_TESTING'], 'true' )
8684
env:
8785
BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)"
8886

build/ci/stage-build-windows-tests.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ jobs:
241241
"http://localhost:8000/"
242242
243243
displayName: Threading Tests
244-
condition: eq( variables['THREAD_TESTING'], 'true' )
245244
env:
246245
BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)"
247246
@@ -258,7 +257,6 @@ jobs:
258257
"$(build.sourcesdirectory)\src\Uno.Wasm.Threading.UITests" `
259258
"http://localhost:8000/"
260259
displayName: Threading AOT Tests
261-
condition: eq( variables['THREAD_TESTING'], 'true' )
262260
env:
263261
BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)"
264262

src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
<WasmEnableThreads Condition=" '$(WasmShellEnableThreads)' == 'true' ">true</WasmEnableThreads>
203203

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

207207
<!-- Native build compatibility -->
208208
<WasmBuildNative Condition="

src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/HotReloadSupport.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ namespace Uno.WebAssembly.Bootstrap {
3737
(<any>this._context).config.environmentVariables['ASPNETCORE-BROWSER-TOOLS']
3838
|| (<any>this._context).config.environmentVariables['__ASPNETCORE_BROWSER_TOOLS'];
3939

40+
if (!await HotReloadSupport._initializeMethod()) {
41+
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.");
42+
}
43+
44+
let capabilities = await HotReloadSupport._getApplyUpdateCapabilitiesMethod();
45+
4046
// Take the place of the internal .NET for WebAssembly APIs for metadata updates coming
4147
// from the "BrowserLink" feature.
4248
(function (Blazor) {
4349
Blazor._internal = {
44-
initialize: function () {
45-
if (!HotReloadSupport._initializeMethod()) {
46-
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.");
47-
}
48-
},
49-
5050
applyExisting: async function (): Promise<void> {
5151

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

6464
getApplyUpdateCapabilities: function () {
65-
Blazor._internal.initialize();
66-
return HotReloadSupport._getApplyUpdateCapabilitiesMethod();
65+
return capabilities;
6766
},
6867

6968
applyHotReload: function (moduleId: any, metadataDelta: any, ilDelta: any, pdbDelta: any, updatedTypes: any) {
70-
Blazor._internal.initialize();
71-
return HotReloadSupport._applyHotReloadDeltaMethod(moduleId, metadataDelta, ilDelta, pdbDelta || "", updatedTypes || []);
69+
HotReloadSupport._applyHotReloadDeltaMethod(moduleId, metadataDelta, ilDelta, pdbDelta || "", updatedTypes || []);
7270
}
7371
};
7472
})((<any>window).Blazor || ((<any>window).Blazor = {}));
7573

76-
// Apply pending updates caused by a browser refresh
77-
(<any>window).Blazor._internal.initialize();
7874
await (<any>window).Blazor._internal.applyExisting();
7975
}
8076
}

src/Uno.Wasm.MetadataUpdater/WebAssemblyHotReload.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static partial class WebAssemblyHotReload
2929
};
3030

3131
[JSExport]
32-
internal static bool Initialize()
32+
internal static async Task<bool> Initialize()
3333
{
3434
bool.TryParse(Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_LOG_METADATA_UPDATES"), out _debugEnabled);
3535

@@ -62,7 +62,7 @@ internal static bool Initialize()
6262
/// For framework use only.
6363
/// </summary>
6464
[JSExport]
65-
public static void ApplyHotReloadDelta(string moduleIdString, string metadataDelta, string ilDeta, string pdbDelta, int[] updatedTypes)
65+
public static async Task ApplyHotReloadDelta(string moduleIdString, string metadataDelta, string ilDeta, string pdbDelta, int[] updatedTypes)
6666
{
6767
if (_linkerEnabled)
6868
{
@@ -93,7 +93,7 @@ public static void ApplyHotReloadDelta(string moduleIdString, string metadataDel
9393
/// For framework use only.
9494
/// </summary>
9595
[JSExport]
96-
public static string GetApplyUpdateCapabilities()
96+
public static async Task<string> GetApplyUpdateCapabilities()
9797
{
9898
if (_debugEnabled)
9999
{

src/Uno.Wasm.Threads.Shared/Program.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ public static class Program
3434
private static List<string> _messages = new List<string>();
3535
private static bool _mainThreadInvoked = false;
3636
private static Timer _timer;
37+
private static SynchronizationContext _jsContext = SynchronizationContext.Current;
3738

3839
static void Main()
3940
{
4041
var runtimeMode = Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_MODE");
4142
Console.WriteLine($"Runtime Version: " + RuntimeInformation.FrameworkDescription);
4243
Console.WriteLine($"Runtime Mode: " + runtimeMode);
4344
Console.WriteLine($"TID: {Thread.CurrentThread.ManagedThreadId}");
45+
Console.WriteLine($"SynchronizationContext: {_jsContext}");
4446

4547
Runtime.InvokeJS("Interop.appendResult('Startup')");
4648

@@ -81,7 +83,7 @@ void logMessage(string message)
8183
int index = Interlocked.Increment(ref counter);
8284
lock (_gate)
8385
{
84-
_messages.Add($"[tid:{Thread.CurrentThread.ManagedThreadId}]: {index} / {message}");
86+
_messages.Add($"[tid:{Thread.CurrentThread.ManagedThreadId}][ctx:{SynchronizationContext.Current?.ToString()}]: {index} / {message}");
8587
}
8688
}
8789

@@ -92,34 +94,29 @@ void DoWork(string name)
9294

9395
if ((i % 2000) == 0)
9496
{
95-
WebAssembly.JSInterop.InternalCalls.InvokeOnMainThread();
97+
Console.WriteLine("InvokeOnMainThread1");
98+
_jsContext.Post(_ => MainThreadCallback(), null);
9699
}
97100
}
98101
}
99102

103+
#pragma warning disable CA1416 // Validate platform compatibility
100104
new Thread(_ =>
101105
{
102-
Console.WriteLine($"Starting thread [tid:{Thread.CurrentThread.ManagedThreadId}]");
106+
Console.WriteLine($"Starting thread [tid:{Thread.CurrentThread.ManagedThreadId}] [ctx:{SynchronizationContext.Current?.ToString()}]");
103107

104-
WebAssembly.JSInterop.InternalCalls.InvokeOnMainThread();
108+
_jsContext.Post(_ => MainThreadCallback(), null);
109+
Console.WriteLine("InvokeOnMainThread2");
105110

106111
DoWork("thread1");
107112
tcs.TrySetResult(true);
108113
Console.WriteLine($"Stopping thread [tid:{Thread.CurrentThread.ManagedThreadId}]");
109114
}).Start();
115+
#pragma warning restore CA1416 // Validate platform compatibility
110116

111117
await tcs.Task;
112118

113119
_event.Set();
114120
}
115121
}
116122
}
117-
118-
namespace WebAssembly.JSInterop
119-
{
120-
public static class InternalCalls
121-
{
122-
[MethodImplAttribute(MethodImplOptions.InternalCall)]
123-
public static extern void InvokeOnMainThread();
124-
}
125-
}

src/Uno.Wasm.Threads.Shared/WasmScripts/test.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,3 @@ var Interop = {
99
parent.appendChild(txt, parent.lastChild);
1010
}
1111
};
12-
13-
var DotNet = {
14-
invokeOnMainThread: function (str) {
15-
let getApplyUpdateCapabilitiesMethod = BINDING.bind_static_method("[Uno.Wasm.Threads] Uno.Wasm.Sample.Program:MainThreadCallback");
16-
getApplyUpdateCapabilitiesMethod();
17-
}
18-
}

src/Uno.Wasm.Threads/Uno.Wasm.Threads.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk.WebAssembly">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<OutputType>Exe</OutputType>
66
<IsPackable>false</IsPackable>
77
<StartupObject>Uno.Wasm.Sample.Program</StartupObject>
88
<MonoRuntimeDebuggerEnabled Condition="'$(Configuration)'=='Debug'">true</MonoRuntimeDebuggerEnabled>
99
<!--<WasmShellMonoRuntimeExecutionMode Condition="$([MSBuild]::IsOsPlatform('Linux'))">FullAOT</WasmShellMonoRuntimeExecutionMode>-->
1010
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1111
<WasmShellEnableThreads>true</WasmShellEnableThreads>
12+
<!--<WasmBuildNative>true</WasmBuildNative>-->
1213
<!--<WasmShellILLinkerEnabled>false</WasmShellILLinkerEnabled>-->
1314
</PropertyGroup>
1415

0 commit comments

Comments
 (0)