From 302ebebd87e94a08b9bebd92718ed5b404cf1ae0 Mon Sep 17 00:00:00 2001 From: Maxim Lipnin Date: Wed, 16 Sep 2020 15:23:16 +0300 Subject: [PATCH 1/3] Mark some APIs throwing PNSE from runtime as unsupported on Browser --- .../System.Threading.Thread/ref/System.Threading.Thread.cs | 2 ++ src/libraries/System.Threading/ref/System.Threading.cs | 5 +++++ .../System.Private.CoreLib/src/System/Threading/Monitor.cs | 6 ++++++ .../src/System/Threading/Thread.Mono.cs | 3 +++ 4 files changed, 16 insertions(+) diff --git a/src/libraries/System.Threading.Thread/ref/System.Threading.Thread.cs b/src/libraries/System.Threading.Thread/ref/System.Threading.Thread.cs index bd511d1705471..479f7f5394435 100644 --- a/src/libraries/System.Threading.Thread/ref/System.Threading.Thread.cs +++ b/src/libraries/System.Threading.Thread/ref/System.Threading.Thread.cs @@ -89,7 +89,9 @@ public static void SetData(System.LocalDataStoreSlot slot, object? data) { } public static void Sleep(int millisecondsTimeout) { } public static void Sleep(System.TimeSpan timeout) { } public static void SpinWait(int iterations) { } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public void Start() { } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public void Start(object? parameter) { } [System.ObsoleteAttribute("Thread.Suspend has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. https://go.microsoft.com/fwlink/?linkid=14202", false)] public void Suspend() { } diff --git a/src/libraries/System.Threading/ref/System.Threading.cs b/src/libraries/System.Threading/ref/System.Threading.cs index d2f60ac8cf5bd..db8ef2cc2eda6 100644 --- a/src/libraries/System.Threading/ref/System.Threading.cs +++ b/src/libraries/System.Threading/ref/System.Threading.cs @@ -280,10 +280,15 @@ public static void TryEnter(object obj, ref bool lockTaken) { } public static void TryEnter(object obj, int millisecondsTimeout, ref bool lockTaken) { } public static bool TryEnter(object obj, System.TimeSpan timeout) { throw null; } public static void TryEnter(object obj, System.TimeSpan timeout, ref bool lockTaken) { } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public static bool Wait(object obj) { throw null; } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public static bool Wait(object obj, int millisecondsTimeout) { throw null; } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) { throw null; } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public static bool Wait(object obj, System.TimeSpan timeout) { throw null; } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public static bool Wait(object obj, System.TimeSpan timeout, bool exitContext) { throw null; } } public sealed partial class Mutex : System.Threading.WaitHandle diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/Threading/Monitor.cs b/src/mono/netcore/System.Private.CoreLib/src/System/Threading/Monitor.cs index 6f6003d5b5ff1..62fa739b59ac9 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/Threading/Monitor.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/Threading/Monitor.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Runtime.CompilerServices; +using System.Runtime.Versioning; namespace System.Threading { @@ -80,6 +81,7 @@ public static bool IsEntered(object obj) return IsEnteredNative(obj); } + [UnsupportedOSPlatform("browser")] public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) { if (obj == null) @@ -87,12 +89,16 @@ public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) return ObjWait(exitContext, millisecondsTimeout, obj); } + [UnsupportedOSPlatform("browser")] public static bool Wait(object obj, TimeSpan timeout, bool exitContext) => Wait(obj, MillisecondsTimeoutFromTimeSpan(timeout), exitContext); + [UnsupportedOSPlatform("browser")] public static bool Wait(object obj, int millisecondsTimeout) => Wait(obj, millisecondsTimeout, false); + [UnsupportedOSPlatform("browser")] public static bool Wait(object obj, TimeSpan timeout) => Wait(obj, MillisecondsTimeoutFromTimeSpan(timeout), false); + [UnsupportedOSPlatform("browser")] public static bool Wait(object obj) => Wait(obj, Timeout.Infinite, false); public static void Pulse(object obj) diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/Threading/Thread.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/System/Threading/Thread.Mono.cs index bf6f7a3430eea..47b26c9dfb9f7 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/Threading/Thread.Mono.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/Threading/Thread.Mono.cs @@ -5,6 +5,7 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using System.Runtime.Versioning; namespace System.Threading { @@ -251,11 +252,13 @@ public static void Sleep(int millisecondsTimeout) internal static void UninterruptibleSleep0() => SleepInternal(0, false); + [UnsupportedOSPlatform("browser")] public void Start() { StartInternal(this); } + [UnsupportedOSPlatform("browser")] public void Start(object parameter) { if (m_start is ThreadStart) From 7b8a4a7f8c786f59070e78b2f30d2c99a9d12615 Mon Sep 17 00:00:00 2001 From: Maxim Lipnin Date: Wed, 16 Sep 2020 15:58:33 +0300 Subject: [PATCH 2/3] Annotate APIs in coreclr part --- .../src/System/Threading/Monitor.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Threading/Monitor.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Threading/Monitor.cs index 80226acdbcecd..0665ac8f0b97d 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Threading/Monitor.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Threading/Monitor.cs @@ -12,10 +12,11 @@ ** =============================================================================*/ -using System.Runtime.CompilerServices; using System.Diagnostics; -using System.Runtime.InteropServices; using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; namespace System.Threading { @@ -170,6 +171,7 @@ public static bool IsEntered(object obj) [MethodImpl(MethodImplOptions.InternalCall)] private static extern bool ObjWait(bool exitContext, int millisecondsTimeout, object obj); + [UnsupportedOSPlatform("browser")] public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) { if (obj == null) @@ -177,21 +179,25 @@ public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) return ObjWait(exitContext, millisecondsTimeout, obj); } + [UnsupportedOSPlatform("browser")] public static bool Wait(object obj, TimeSpan timeout, bool exitContext) { return Wait(obj, MillisecondsTimeoutFromTimeSpan(timeout), exitContext); } + [UnsupportedOSPlatform("browser")] public static bool Wait(object obj, int millisecondsTimeout) { return Wait(obj, millisecondsTimeout, false); } + [UnsupportedOSPlatform("browser")] public static bool Wait(object obj, TimeSpan timeout) { return Wait(obj, MillisecondsTimeoutFromTimeSpan(timeout), false); } + [UnsupportedOSPlatform("browser")] public static bool Wait(object obj) { return Wait(obj, Timeout.Infinite, false); From cc5c1589afd619e6fa5c5b673d5f46717081ca67 Mon Sep 17 00:00:00 2001 From: Maxim Lipnin Date: Wed, 16 Sep 2020 16:24:15 +0300 Subject: [PATCH 3/3] Annotate APIs in coreclr part --- .../src/System/Threading/Thread.CoreCLR.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs index 9edaf81e8a878..882ce2144892f 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs @@ -6,6 +6,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Serialization; +using System.Runtime.Versioning; namespace System.Threading { @@ -182,6 +183,7 @@ internal ThreadHandle GetNativeHandle() /// method on the IThreadable interface passed in the constructor. Once the /// thread is dead, it cannot be restarted with another call to Start. /// + [UnsupportedOSPlatform("browser")] public void Start(object? parameter) { // In the case of a null delegate (second call to start on same thread) @@ -196,6 +198,7 @@ public void Start(object? parameter) Start(); } + [UnsupportedOSPlatform("browser")] public void Start() { #if FEATURE_COMINTEROP_APARTMENT_SUPPORT