From 429485cfc62766ffa143df5ea4de1d73ffbe6dbb Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Wed, 12 May 2021 22:13:40 -0700 Subject: [PATCH] Use platform guard attributes --- eng/Versions.props | 2 +- .../src/ConsoleLoggerProvider.cs | 10 ++++++++-- .../SocketsHttpHandler/HttpConnectionPool.cs | 20 +++++++++---------- .../Tasks/ThreadPoolTaskScheduler.cs | 2 -- .../src/System/Threading/Thread.cs | 1 + 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index ae34e9f29851a..2b9e23ccb0ca6 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -47,7 +47,7 @@ 3.8.0 - 6.0.0-preview5.21219.2 + 6.0.0-preview5.21262.4 3.10.0-2.final 3.10.0-2.final diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs index a7ac0f7f86d87..bb24ed8f28aac 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs @@ -47,8 +47,8 @@ public ConsoleLoggerProvider(IOptionsMonitor options, IEnu _optionsReloadToken = _options.OnChange(ReloadLoggerOptions); _messageQueue = new ConsoleLoggerProcessor(); - // TODO update when https://github.com/dotnet/runtime/issues/44922 implemented - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || DoesWindowsConsoleSupportAnsi()) + + if (DoesWindowsConsoleSupportAnsi()) { _messageQueue.Console = new AnsiLogConsole(); _messageQueue.ErrorConsole = new AnsiLogConsole(stdErr: true); @@ -60,8 +60,14 @@ public ConsoleLoggerProvider(IOptionsMonitor options, IEnu } } + [UnsupportedOSPlatformGuard("windows")] private static bool DoesWindowsConsoleSupportAnsi() { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return true; + } + // for Windows, check the console mode var stdOutHandle = Interop.Kernel32.GetStdHandle(Interop.Kernel32.STD_OUTPUT_HANDLE); if (!Interop.Kernel32.GetConsoleMode(stdOutHandle, out int consoleMode)) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs index 16272cbaa4336..40d9798212759 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs @@ -76,6 +76,9 @@ internal sealed class HttpConnectionPool : IDisposable private byte[]? _http2AltSvcOriginUri; internal readonly byte[]? _http2EncodedAuthorityHostHeader; + [SupportedOSPlatformGuard("linux")] + [SupportedOSPlatformGuard("macOS")] + [SupportedOSPlatformGuard("Windows")] private readonly bool _http3Enabled; private Http3Connection? _http3Connection; private SemaphoreSlim? _http3ConnectionCreateLock; @@ -122,8 +125,8 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK } _http2Enabled = _poolManager.Settings._maxHttpVersion >= HttpVersion.Version20; - // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished - if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) + + if (_http3Enabled) { _http3Enabled = _poolManager.Settings._maxHttpVersion >= HttpVersion.Version30 && (_poolManager.Settings._quicImplementationProvider ?? QuicImplementationProviders.Default).IsSupported; } @@ -261,14 +264,10 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK _http3EncodedAuthorityHostHeader = QPackEncoder.EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(H3StaticTable.Authority, hostHeader); } - // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished - if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) + if (_http3Enabled) { - if (_http3Enabled) - { - _sslOptionsHttp3 = ConstructSslOptions(poolManager, sslHostName); - _sslOptionsHttp3.ApplicationProtocols = s_http3ApplicationProtocols; - } + _sslOptionsHttp3 = ConstructSslOptions(poolManager, sslHostName); + _sslOptionsHttp3.ApplicationProtocols = s_http3ApplicationProtocols; } } @@ -866,8 +865,7 @@ private async ValueTask DetermineVersionAndSendAsync(HttpRe { HttpResponseMessage? response; - // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished - if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) + if (_http3Enabled) { response = await TrySendUsingHttp3Async(request, async, doRequestAuth, cancellationToken).ConfigureAwait(false); if (response is not null) diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ThreadPoolTaskScheduler.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ThreadPoolTaskScheduler.cs index 692e39bb6b015..e1469c5cc8b80 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ThreadPoolTaskScheduler.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ThreadPoolTaskScheduler.cs @@ -45,13 +45,11 @@ protected internal override void QueueTask(Task task) if (Thread.IsThreadStartSupported && (options & TaskCreationOptions.LongRunning) != 0) { // Run LongRunning tasks on their own dedicated thread. -#pragma warning disable CA1416 // TODO: https://github.com/dotnet/runtime/issues/44922 new Thread(s_longRunningThreadWork) { IsBackground = true, Name = ".NET Long Running Task" }.UnsafeStart(task); -#pragma warning restore CA1416 } else { diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs index aa6e8c6f8d582..cb16c8b99d4f1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs @@ -164,6 +164,7 @@ public Thread(ParameterizedThreadStart start, int maxStackSize) } #if !TARGET_BROWSER + [UnsupportedOSPlatformGuard("browser")] internal const bool IsThreadStartSupported = true; /// Causes the operating system to change the state of the current instance to , and optionally supplies an object containing data to be used by the method the thread executes.