diff --git a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeHandleCache.cs b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeHandleCache.cs index 2ce8fbf1b378d5..76e58eeaf7e732 100644 --- a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeHandleCache.cs +++ b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeHandleCache.cs @@ -21,11 +21,13 @@ internal static class SafeHandleCache where T : SafeHandle /// internal static T GetInvalidHandle(Func invalidHandleFactory) { - T? currentHandle = Volatile.Read(ref s_invalidHandle); - if (currentHandle == null) + return s_invalidHandle ?? CreateInvalidHandle(invalidHandleFactory); + + static T CreateInvalidHandle(Func invalidHandleFactory) { T newHandle = invalidHandleFactory(); - currentHandle = Interlocked.CompareExchange(ref s_invalidHandle, newHandle, null); + T? currentHandle = Interlocked.CompareExchange(ref s_invalidHandle, newHandle, null); + if (currentHandle == null) { GC.SuppressFinalize(newHandle); @@ -35,9 +37,10 @@ internal static T GetInvalidHandle(Func invalidHandleFactory) { newHandle.Dispose(); } + + Debug.Assert(currentHandle.IsInvalid); + return currentHandle; } - Debug.Assert(currentHandle.IsInvalid); - return currentHandle; } /// Gets whether the specified handle is invalid handle. @@ -46,7 +49,7 @@ internal static T GetInvalidHandle(Func invalidHandleFactory) internal static bool IsCachedInvalidHandle(SafeHandle handle) { Debug.Assert(handle != null); - bool isCachedInvalidHandle = ReferenceEquals(handle, Volatile.Read(ref s_invalidHandle)); + bool isCachedInvalidHandle = ReferenceEquals(handle, s_invalidHandle); Debug.Assert(!isCachedInvalidHandle || handle.IsInvalid, "The cached invalid handle must still be invalid."); return isCachedInvalidHandle; } diff --git a/src/libraries/Common/src/System/Security/Cryptography/Oids.Shared.cs b/src/libraries/Common/src/System/Security/Cryptography/Oids.Shared.cs index 69f5b96dee02db..2f584a0bbfd7f8 100644 --- a/src/libraries/Common/src/System/Security/Cryptography/Oids.Shared.cs +++ b/src/libraries/Common/src/System/Security/Cryptography/Oids.Shared.cs @@ -8,38 +8,38 @@ namespace System.Security.Cryptography { internal static partial class Oids { - private static volatile Oid? s_rsaOid; - private static volatile Oid? s_ecPublicKeyOid; - private static volatile Oid? s_tripleDesCbcOid; - private static volatile Oid? s_aes256CbcOid; - private static volatile Oid? s_secp256R1Oid; - private static volatile Oid? s_secp384R1Oid; - private static volatile Oid? s_secp521R1Oid; - private static volatile Oid? s_sha256Oid; - private static volatile Oid? s_pkcs7DataOid; - private static volatile Oid? s_contentTypeOid; - private static volatile Oid? s_documentDescriptionOid; - private static volatile Oid? s_documentNameOid; - private static volatile Oid? s_localKeyIdOid; - private static volatile Oid? s_messageDigestOid; - private static volatile Oid? s_signingTimeOid; - private static volatile Oid? s_pkcs9ExtensionRequestOid; - private static volatile Oid? s_basicConstraints2Oid; - private static volatile Oid? s_enhancedKeyUsageOid; - private static volatile Oid? s_keyUsageOid; - private static volatile Oid? s_subjectAltNameOid; - private static volatile Oid? s_subjectKeyIdentifierOid; - private static volatile Oid? s_authorityKeyIdentifierOid; - private static volatile Oid? s_authorityInformationAccessOid; - private static volatile Oid? s_crlNumberOid; - private static volatile Oid? s_crlDistributionPointOid; - private static volatile Oid? s_commonNameOid; - private static volatile Oid? s_countryOrRegionOid; - private static volatile Oid? s_localityNameOid; - private static volatile Oid? s_stateOrProvinceNameOid; - private static volatile Oid? s_organizationOid; - private static volatile Oid? s_organizationalUnitOid; - private static volatile Oid? s_emailAddressOid; + private static Oid? s_rsaOid; + private static Oid? s_ecPublicKeyOid; + private static Oid? s_tripleDesCbcOid; + private static Oid? s_aes256CbcOid; + private static Oid? s_secp256R1Oid; + private static Oid? s_secp384R1Oid; + private static Oid? s_secp521R1Oid; + private static Oid? s_sha256Oid; + private static Oid? s_pkcs7DataOid; + private static Oid? s_contentTypeOid; + private static Oid? s_documentDescriptionOid; + private static Oid? s_documentNameOid; + private static Oid? s_localKeyIdOid; + private static Oid? s_messageDigestOid; + private static Oid? s_signingTimeOid; + private static Oid? s_pkcs9ExtensionRequestOid; + private static Oid? s_basicConstraints2Oid; + private static Oid? s_enhancedKeyUsageOid; + private static Oid? s_keyUsageOid; + private static Oid? s_subjectAltNameOid; + private static Oid? s_subjectKeyIdentifierOid; + private static Oid? s_authorityKeyIdentifierOid; + private static Oid? s_authorityInformationAccessOid; + private static Oid? s_crlNumberOid; + private static Oid? s_crlDistributionPointOid; + private static Oid? s_commonNameOid; + private static Oid? s_countryOrRegionOid; + private static Oid? s_localityNameOid; + private static Oid? s_stateOrProvinceNameOid; + private static Oid? s_organizationOid; + private static Oid? s_organizationalUnitOid; + private static Oid? s_emailAddressOid; internal static Oid RsaOid => s_rsaOid ??= InitializeOid(Rsa); internal static Oid EcPublicKeyOid => s_ecPublicKeyOid ??= InitializeOid(EcPublicKey); diff --git a/src/libraries/Microsoft.Extensions.Options/src/UnnamedOptionsManager.cs b/src/libraries/Microsoft.Extensions.Options/src/UnnamedOptionsManager.cs index 27f6d6e92ac215..e6ce20b94d1871 100644 --- a/src/libraries/Microsoft.Extensions.Options/src/UnnamedOptionsManager.cs +++ b/src/libraries/Microsoft.Extensions.Options/src/UnnamedOptionsManager.cs @@ -11,7 +11,7 @@ internal sealed class UnnamedOptionsManager<[DynamicallyAccessedMembers(Options. where TOptions : class { private readonly IOptionsFactory _factory; - private volatile object? _syncObj; + private object? _syncObj; private volatile TOptions? _value; public UnnamedOptionsManager(IOptionsFactory factory) => _factory = factory; diff --git a/src/libraries/Microsoft.Win32.Registry/src/Microsoft/Win32/RegistryKey.cs b/src/libraries/Microsoft.Win32.Registry/src/Microsoft/Win32/RegistryKey.cs index 1fedc0ec5c6a1e..400d16af590bc6 100644 --- a/src/libraries/Microsoft.Win32.Registry/src/Microsoft/Win32/RegistryKey.cs +++ b/src/libraries/Microsoft.Win32.Registry/src/Microsoft/Win32/RegistryKey.cs @@ -66,8 +66,8 @@ public sealed partial class RegistryKey : MarshalByRefObject, IDisposable private const int MaxKeyLength = 255; private const int MaxValueLength = 16383; - private volatile SafeRegistryHandle _hkey; - private volatile string _keyName; + private SafeRegistryHandle _hkey; + private string _keyName; private readonly bool _remoteKey; private volatile StateFlags _state; private volatile RegistryKeyPermissionCheck _checkMode; diff --git a/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveComparer.cs b/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveComparer.cs index 0c84d460db3655..82f587ec9616b9 100644 --- a/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveComparer.cs +++ b/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveComparer.cs @@ -17,7 +17,7 @@ namespace System.Collections public class CaseInsensitiveComparer : IComparer { private readonly CompareInfo _compareInfo; - private static volatile CaseInsensitiveComparer? s_InvariantCaseInsensitiveComparer; + private static CaseInsensitiveComparer? s_InvariantCaseInsensitiveComparer; public CaseInsensitiveComparer() { diff --git a/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveHashCodeProvider.cs b/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveHashCodeProvider.cs index 61777b6c93c50c..22bb48f8a1c2f0 100644 --- a/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveHashCodeProvider.cs +++ b/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveHashCodeProvider.cs @@ -12,7 +12,7 @@ namespace System.Collections [Obsolete("CaseInsensitiveHashCodeProvider has been deprecated. Use StringComparer instead.")] public class CaseInsensitiveHashCodeProvider : IHashCodeProvider { - private static volatile CaseInsensitiveHashCodeProvider? s_invariantCaseInsensitiveHashCodeProvider; + private static CaseInsensitiveHashCodeProvider? s_invariantCaseInsensitiveHashCodeProvider; private readonly CompareInfo _compareInfo; public CaseInsensitiveHashCodeProvider() diff --git a/src/libraries/System.ComponentModel.Primitives/src/System/ComponentModel/CategoryAttribute.cs b/src/libraries/System.ComponentModel.Primitives/src/System/ComponentModel/CategoryAttribute.cs index 2b9c0c08c88e19..fc06665da5ecfc 100644 --- a/src/libraries/System.ComponentModel.Primitives/src/System/ComponentModel/CategoryAttribute.cs +++ b/src/libraries/System.ComponentModel.Primitives/src/System/ComponentModel/CategoryAttribute.cs @@ -12,22 +12,22 @@ namespace System.ComponentModel [AttributeUsage(AttributeTargets.All)] public class CategoryAttribute : Attribute { - private static volatile CategoryAttribute? s_action; - private static volatile CategoryAttribute? s_appearance; - private static volatile CategoryAttribute? s_asynchronous; - private static volatile CategoryAttribute? s_behavior; - private static volatile CategoryAttribute? s_data; - private static volatile CategoryAttribute? s_design; - private static volatile CategoryAttribute? s_dragDrop; - private static volatile CategoryAttribute? s_defAttr; - private static volatile CategoryAttribute? s_focus; - private static volatile CategoryAttribute? s_format; - private static volatile CategoryAttribute? s_key; - private static volatile CategoryAttribute? s_layout; - private static volatile CategoryAttribute? s_mouse; - private static volatile CategoryAttribute? s_windowStyle; - - private bool _localized; + private static CategoryAttribute? s_action; + private static CategoryAttribute? s_appearance; + private static CategoryAttribute? s_asynchronous; + private static CategoryAttribute? s_behavior; + private static CategoryAttribute? s_data; + private static CategoryAttribute? s_design; + private static CategoryAttribute? s_dragDrop; + private static CategoryAttribute? s_defAttr; + private static CategoryAttribute? s_focus; + private static CategoryAttribute? s_format; + private static CategoryAttribute? s_key; + private static CategoryAttribute? s_layout; + private static CategoryAttribute? s_mouse; + private static CategoryAttribute? s_windowStyle; + + private volatile bool _localized; private readonly object _locker = new object(); diff --git a/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/XmlWriterTraceListener.cs b/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/XmlWriterTraceListener.cs index a513b8aa86159c..e5b72a51d41132 100644 --- a/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/XmlWriterTraceListener.cs +++ b/src/libraries/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/XmlWriterTraceListener.cs @@ -16,7 +16,7 @@ public class XmlWriterTraceListener : TextWriterTraceListener { private const string FixedHeader = ""; - private static volatile string? s_processName; + private static string? s_processName; private readonly string _machineName = Environment.MachineName; private StringBuilder? _strBldr; private XmlTextWriter? _xmlBlobWriter; @@ -252,13 +252,15 @@ private void WriteEndHeader() { if (OperatingSystem.IsBrowser()) // Process isn't supported on Browser { - s_processName = processName = string.Empty; + processName = string.Empty; } else { using Process process = Process.GetCurrentProcess(); - s_processName = processName = process.ProcessName; + processName = process.ProcessName; } + + s_processName = processName; } InternalWrite("\" />"); diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs index 4b04787d3acdce..0d647385f996ca 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -776,15 +776,10 @@ protected internal override Task SendAsync(HttpRequestMessa // lazy-load the validator func so it can be trimmed by the ILLinker if it isn't used. private static Func? s_dangerousAcceptAnyServerCertificateValidator; [UnsupportedOSPlatform("browser")] - public static Func DangerousAcceptAnyServerCertificateValidator - { - get - { - return Volatile.Read(ref s_dangerousAcceptAnyServerCertificateValidator) ?? - Interlocked.CompareExchange(ref s_dangerousAcceptAnyServerCertificateValidator, delegate { return true; }, null) ?? - s_dangerousAcceptAnyServerCertificateValidator; - } - } + public static Func DangerousAcceptAnyServerCertificateValidator => + s_dangerousAcceptAnyServerCertificateValidator ?? + Interlocked.CompareExchange(ref s_dangerousAcceptAnyServerCertificateValidator, delegate { return true; }, null) ?? + s_dangerousAcceptAnyServerCertificateValidator; private void ThrowForModifiedManagedSslOptionsIfStarted() { diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs index 9c3935701e82f9..7a6edd71053bb6 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs @@ -363,7 +363,7 @@ protected internal override Task SendAsync(HttpRequestMessa private static Func? s_dangerousAcceptAnyServerCertificateValidator; [UnsupportedOSPlatform("browser")] public static Func DangerousAcceptAnyServerCertificateValidator => - Volatile.Read(ref s_dangerousAcceptAnyServerCertificateValidator) ?? + s_dangerousAcceptAnyServerCertificateValidator ?? Interlocked.CompareExchange(ref s_dangerousAcceptAnyServerCertificateValidator, delegate { return true; }, null) ?? s_dangerousAcceptAnyServerCertificateValidator; diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SafeSocketHandle.Unix.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SafeSocketHandle.Unix.cs index 62417e62599ba9..35e9871bb88a68 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SafeSocketHandle.Unix.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SafeSocketHandle.Unix.cs @@ -96,18 +96,10 @@ internal void TrackOption(SocketOptionLevel level, SocketOptionName name) ExposedHandleOrUntrackedConfiguration = true; } - internal SocketAsyncContext AsyncContext - { - get - { - if (Volatile.Read(ref _asyncContext) == null) - { - Interlocked.CompareExchange(ref _asyncContext, new SocketAsyncContext(this), null); - } - - return _asyncContext!; - } - } + internal SocketAsyncContext AsyncContext => + _asyncContext ?? + Interlocked.CompareExchange(ref _asyncContext, new SocketAsyncContext(this), null) ?? + _asyncContext!; /// /// This represents whether the Socket instance is blocking or non-blocking *from the user's point of view*, diff --git a/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs b/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs index b07429f409cb60..2849756d131b4e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs +++ b/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs @@ -414,8 +414,7 @@ public void SetThreadPrincipal(IPrincipal principal) Debug.Assert(mi != null); // Don't throw PNSE if null like for WindowsPrincipal as UnauthenticatedPrincipal should // be available on all platforms. - Volatile.Write(ref s_getUnauthenticatedPrincipal, - mi.CreateDelegate>()); + s_getUnauthenticatedPrincipal = mi.CreateDelegate>(); } principal = s_getUnauthenticatedPrincipal(); @@ -430,8 +429,7 @@ public void SetThreadPrincipal(IPrincipal principal) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_Principal); } - Volatile.Write(ref s_getWindowsPrincipal, - mi.CreateDelegate>()); + s_getWindowsPrincipal = mi.CreateDelegate>(); } principal = s_getWindowsPrincipal(); diff --git a/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs index 3ab01cee369168..9fe1c77a1837e8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs @@ -85,7 +85,7 @@ static bool TryConvertFromInvariantString( { Type? typeDescriptorType = Type.GetType("System.ComponentModel.TypeDescriptor, System.ComponentModel.TypeConverter", throwOnError: false); MethodInfo? mi = typeDescriptorType?.GetMethod("ConvertFromInvariantString", BindingFlags.NonPublic | BindingFlags.Static); - Volatile.Write(ref s_convertFromInvariantString, mi == null ? new object() : mi.CreateDelegate>()); + s_convertFromInvariantString = mi == null ? new object() : mi.CreateDelegate>(); } if (!(s_convertFromInvariantString is Func convertFromInvariantString)) diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Symbolic/UnicodeCategoryConditions.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Symbolic/UnicodeCategoryConditions.cs index 27d8f18298a0bd..6ad4f5a526c4be 100644 --- a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Symbolic/UnicodeCategoryConditions.cs +++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Symbolic/UnicodeCategoryConditions.cs @@ -33,7 +33,7 @@ static UnicodeCategoryConditions() /// Gets a that represents the specified . public static BDD GetCategory(UnicodeCategory category) => - Volatile.Read(ref s_categories[(int)category]) ?? + s_categories[(int)category] ?? Interlocked.CompareExchange(ref s_categories[(int)category], BDD.Deserialize(UnicodeCategoryRanges.GetSerializedCategory(category)), null) ?? s_categories[(int)category]!; diff --git a/src/libraries/System.Windows.Extensions/src/System/Media/SystemSounds.cs b/src/libraries/System.Windows.Extensions/src/System/Media/SystemSounds.cs index da1c5de5f35dbe..a5042fc0242026 100644 --- a/src/libraries/System.Windows.Extensions/src/System/Media/SystemSounds.cs +++ b/src/libraries/System.Windows.Extensions/src/System/Media/SystemSounds.cs @@ -5,11 +5,11 @@ namespace System.Media { public static class SystemSounds { - private static volatile SystemSound? s_asterisk; - private static volatile SystemSound? s_beep; - private static volatile SystemSound? s_exclamation; - private static volatile SystemSound? s_hand; - private static volatile SystemSound? s_question; + private static SystemSound? s_asterisk; + private static SystemSound? s_beep; + private static SystemSound? s_exclamation; + private static SystemSound? s_hand; + private static SystemSound? s_question; public static SystemSound Asterisk {