Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara committed Nov 10, 2023
1 parent 5501924 commit 3ef9590
Show file tree
Hide file tree
Showing 46 changed files with 526 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ jobs:
alwaysRun: ${{ parameters.isWasmOnlyBuild }}

# NOTE - Since threading is experimental, we don't want to block mainline work
shouldContinueOnError: true
# TODO put back shouldContinueOnError: true
scenarios:
- WasmTestOnBrowser
#- WasmTestOnNodeJS - this is not supported yet, https://github.com/dotnet/runtime/issues/85592
Expand Down
9 changes: 7 additions & 2 deletions src/libraries/Common/src/Interop/Browser/Interop.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ internal static unsafe partial class Runtime
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern unsafe void BindCSFunction(in string fully_qualified_name, int signature_hash, void* signature, out int is_exception, out object result);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void ResolveOrRejectPromise(void* data);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern IntPtr RegisterGCRoot(IntPtr start, int bytesSize, IntPtr name);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void DeregisterGCRoot(IntPtr handle);
Expand All @@ -33,6 +31,13 @@ internal static unsafe partial class Runtime
public static extern void InstallWebWorkerInterop(bool installJSSynchronizationContext);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void UninstallWebWorkerInterop(bool uninstallJSSynchronizationContext);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void ResolveOrRejectPromiseUI(IntPtr data);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void ResolveOrRejectPromiseCurrent(IntPtr data);
#else
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void ResolveOrRejectPromise(void* data);
#endif

#region Legacy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ internal static (ConsoleLogger Logger, ConsoleSink Sink, ConsoleSink ErrorSink,
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public void ConsoleLoggerOptions_TimeStampFormat_IsReloaded()
{
// Arrange
Expand All @@ -88,7 +87,6 @@ public void ConsoleLoggerOptions_TimeStampFormat_IsReloaded()
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
[MemberData(nameof(FormatterNames))]
public void InvalidLogLevel_Throws(string formatterName)
{
Expand All @@ -103,7 +101,6 @@ public void InvalidLogLevel_Throws(string formatterName)
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
[MemberData(nameof(FormatterNamesAndLevels))]
public void NoMessageOrException_Noop(string formatterName, LogLevel level)
{
Expand All @@ -123,7 +120,6 @@ public void NoMessageOrException_Noop(string formatterName, LogLevel level)
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
[MemberData(nameof(FormatterNamesAndLevels))]
public void Log_LogsCorrectTimestamp(string formatterName, LogLevel level)
{
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<EnableDefaultItems>true</EnableDefaultItems>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetOS)' == 'browser'">
<WasmXHarnessMonoArgs>--setenv=XHARNESS_LOG_TEST_START=true --no-memory-snapshot</WasmXHarnessMonoArgs>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\src\LogLevelAttribute.cs"
Link="tests\DI.Common\Common\src\LogLevelAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetOS)' == 'browser'">
<WasmXHarnessMonoArgs>--setenv=XHARNESS_LOG_TEST_START=true --no-memory-snapshot</WasmXHarnessMonoArgs>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonTestPath)TestUtilities\System\DisableParallelization.cs" Link="Common\TestUtilities\System\DisableParallelization.cs" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/libraries/System.IO.Pipelines/tests/PipeWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ public async Task WritesUsingGetMemoryWorks()
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91547", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public async Task CompleteWithLargeWriteThrows()
{
var completeDelay = TimeSpan.FromMilliseconds(10);
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Linq.Parallel/tests/ExchangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public static void Partitioning_Default_Longrunning(Labeled<ParallelQuery<int>>

[ConditionalTheory]
[MemberData(nameof(PartitioningData), new[] { 0, 1, 2, 16, 1024 })]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91541", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public static void Partitioning_Striped(Labeled<ParallelQuery<int>> labeled, int count, int partitions)
{
if (partitions > 1 && !PlatformDetection.IsThreadingSupported)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,38 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using System.Threading;

namespace System.Runtime.InteropServices.JavaScript
{
internal static unsafe partial class JavaScriptImports
{
#if FEATURE_WASM_THREADS
private const int messageSize = 4 * 16;

public static void ResolveOrRejectPromise(Span<JSMarshalerArgument> arguments, JSSynchronizationContext synchronizationContext)
{
// TODO: optimize to zero copy for the sync dispatch on the same thread ? (only on JSWebWorker)
var message = Marshal.AllocHGlobal(messageSize);
fixed (JSMarshalerArgument* ptr = arguments)
{
Unsafe.CopyBlock((void*)message, ptr, messageSize);
}

if (synchronizationContext._IsMainThread)
{
Interop.Runtime.ResolveOrRejectPromiseUI(message);
}
else
{
synchronizationContext.Post(static (message) =>
{
IntPtr m = (IntPtr)message!;
Interop.Runtime.ResolveOrRejectPromiseCurrent(m);
}, message);
}
}
#else
public static void ResolveOrRejectPromise(Span<JSMarshalerArgument> arguments)
{
fixed (JSMarshalerArgument* ptr = arguments)
Expand All @@ -19,6 +46,7 @@ public static void ResolveOrRejectPromise(Span<JSMarshalerArgument> arguments)
}
}
}
#endif

#if !DISABLE_LEGACY_JS_INTEROP
#region legacy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public static void InstallWebWorkerInterop(bool installJSSynchronizationContext,
var ctx = JSSynchronizationContext.CurrentJSSynchronizationContext;
if (ctx == null)
{
ctx = new JSSynchronizationContext(Thread.CurrentThread, currentThreadId);
ctx = new JSSynchronizationContext(Thread.CurrentThread, currentThreadId, isMainThread);
ctx.previousSynchronizationContext = SynchronizationContext.Current;
JSSynchronizationContext.CurrentJSSynchronizationContext = ctx;
SynchronizationContext.SetSynchronizationContext(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal sealed class JSSynchronizationContext : SynchronizationContext
internal static JSSynchronizationContext? CurrentJSSynchronizationContext;
internal SynchronizationContext? previousSynchronizationContext;
internal bool isDisposed;
internal bool _IsMainThread;

internal readonly struct WorkItem
{
Expand All @@ -45,9 +46,9 @@ public WorkItem(SendOrPostCallback callback, object? data, ManualResetEventSlim?
}
}

internal JSSynchronizationContext(Thread targetThread, IntPtr targetThreadId)
internal JSSynchronizationContext(Thread targetThread, IntPtr targetThreadId, bool isMainThread)
: this(
targetThread, targetThreadId,
targetThread, targetThreadId, isMainThread,
Channel.CreateUnbounded<WorkItem>(
new UnboundedChannelOptions { SingleWriter = false, SingleReader = true, AllowSynchronousContinuations = true }
)
Expand All @@ -65,17 +66,18 @@ internal static void AssertWebWorkerContext()
#endif
}

private JSSynchronizationContext(Thread targetThread, IntPtr targetThreadId, WorkItemQueueType queue)
private JSSynchronizationContext(Thread targetThread, IntPtr targetThreadId, bool isMainThread, WorkItemQueueType queue)
{
TargetThread = targetThread;
TargetThreadId = targetThreadId;
Queue = queue;
_IsMainThread = isMainThread;
_DataIsAvailable = DataIsAvailable;
}

public override SynchronizationContext CreateCopy()
{
return new JSSynchronizationContext(TargetThread, TargetThreadId, Queue);
return new JSSynchronizationContext(TargetThread, TargetThreadId, _IsMainThread, Queue);
}

internal void AwaitNewData()
Expand Down Expand Up @@ -137,7 +139,10 @@ public override void Send(SendOrPostCallback d, object? state)
}

[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe void TargetThreadScheduleBackgroundJob(IntPtr targetThread, void* callback);
// targetThreadId could be long, like internal Thread.thread_id
// it's pthread_t tid, which is 4 bytes on emscripten, so we are fine here
// MonoNativeThreadId is 4 bytes on emscripten too, so we need IntPtr
internal static extern unsafe void TargetThreadScheduleBackgroundJob(IntPtr targetThreadId, void* callback);

#pragma warning disable CS3016 // Arrays as attribute arguments is not CLS-compliant
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand Down
Loading

0 comments on commit 3ef9590

Please sign in to comment.