diff --git a/src/coreclr/System.Private.CoreLib/src/System/MulticastDelegate.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/MulticastDelegate.CoreCLR.cs index 9740d23843f42b..1c9a4c6c384b29 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/MulticastDelegate.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/MulticastDelegate.CoreCLR.cs @@ -9,6 +9,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Serialization; +using System.Threading; namespace System { @@ -144,7 +145,7 @@ private bool InvocationListEquals(MulticastDelegate d) private static bool TrySetSlot(object?[] a, int index, object o) { - if (a[index] == null && Threading.Interlocked.CompareExchange(ref a[index], o, null) == null) + if (a[index] == null && Interlocked.CompareExchange(ref a[index], o, null) == null) return true; // The slot may be already set because we have added and removed the same method before. diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs index 17bb50457648f5..f9c11e07891095 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs @@ -34,7 +34,7 @@ internal sealed partial class RuntimeAssembly : Assembly #endregion - internal IntPtr GetUnderlyingNativeHandle() { return m_assembly; } + internal IntPtr GetUnderlyingNativeHandle() => m_assembly; private sealed class ManifestResourceStream : UnmanagedMemoryStream { @@ -52,17 +52,8 @@ internal unsafe ManifestResourceStream(RuntimeAssembly manifestAssembly, byte* p // NOTE: no reason to override Write(Span), since a ManifestResourceStream is read-only. } - internal object SyncRoot - { - get - { - if (m_syncRoot == null) - { - Interlocked.CompareExchange(ref m_syncRoot, new object(), null); - } - return m_syncRoot; - } - } + internal object SyncRoot => + m_syncRoot ?? Interlocked.CompareExchange(ref m_syncRoot, new object(), null) ?? m_syncRoot; public override event ModuleResolveEventHandler? ModuleResolve { diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Delegate.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Delegate.cs index 92a029cbf9837c..532e84cad3138a 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Delegate.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Delegate.cs @@ -9,6 +9,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Serialization; +using System.Threading; using Internal.Reflection.Augments; using Internal.Runtime; @@ -465,7 +466,7 @@ private unsafe Delegate NewMulticastDelegate(Wrapper[] invocationList, int invoc private static bool TrySetSlot(Wrapper[] a, int index, Delegate o) { - if (a[index].Value == null && System.Threading.Interlocked.CompareExchange(ref a[index].Value, o, null) == null) + if (a[index].Value == null && Interlocked.CompareExchange(ref a[index].Value, o, null) == null) return true; // The slot may be already set because we have added and removed the same method before. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Builder.cs index 0476b66430d24e..a78409ee75c6a8 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Builder.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Threading; namespace System.Collections.Immutable { @@ -56,11 +57,6 @@ public sealed class Builder : IDictionary, IReadOnlyDictionary private int _version; - /// - /// The object callers may use to synchronize access to this collection. - /// - private object? _syncRoot; - /// /// Initializes a new instance of the class. /// @@ -251,18 +247,8 @@ ICollection IDictionary.Values /// /// An object that can be used to synchronize access to the . [DebuggerBrowsable(DebuggerBrowsableState.Never)] - object ICollection.SyncRoot - { - get - { - if (_syncRoot == null) - { - Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null); - } - - return _syncRoot; - } - } + object ICollection.SyncRoot => + field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field; /// /// Gets a value indicating whether access to the is synchronized (thread safe). diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs index fc75f69a594d6c..0fa8b325114bf9 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Threading; namespace System.Collections.Immutable { @@ -46,11 +47,6 @@ public sealed class Builder : IList, IList, IReadOnlyList /// private int _version; - /// - /// The object callers may use to synchronize access to this collection. - /// - private object? _syncRoot; - /// /// Initializes a new instance of the class. /// @@ -1159,18 +1155,8 @@ bool ICollection.IsSynchronized /// /// An object that can be used to synchronize access to the . [DebuggerBrowsable(DebuggerBrowsableState.Never)] - object ICollection.SyncRoot - { - get - { - if (_syncRoot == null) - { - System.Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null); - } - - return _syncRoot; - } - } + object ICollection.SyncRoot => + field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field; #endregion } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Builder.cs index ff5fe9ae030587..2f273499239656 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Builder.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Threading; namespace System.Collections.Immutable { @@ -59,11 +60,6 @@ public sealed class Builder : IDictionary, IReadOnlyDictionary private int _version; - /// - /// The object callers may use to synchronize access to this collection. - /// - private object? _syncRoot; - /// /// Initializes a new instance of the class. /// @@ -262,18 +258,8 @@ ICollection IDictionary.Values /// /// An object that can be used to synchronize access to the . [DebuggerBrowsable(DebuggerBrowsableState.Never)] - object ICollection.SyncRoot - { - get - { - if (_syncRoot == null) - { - Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null); - } - - return _syncRoot; - } - } + object ICollection.SyncRoot => + field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field; /// /// Gets a value indicating whether access to the is synchronized (thread safe). diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs index 2f0a963972d081..1c3a778a5bdc6f 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Threading; namespace System.Collections.Immutable { @@ -51,11 +52,6 @@ public sealed class Builder : IReadOnlyCollection, ISet, ICollection /// private int _version; - /// - /// The object callers may use to synchronize access to this collection. - /// - private object? _syncRoot; - /// /// Initializes a new instance of the class. /// @@ -505,18 +501,8 @@ bool ICollection.IsSynchronized /// /// An object that can be used to synchronize access to the . [DebuggerBrowsable(DebuggerBrowsableState.Never)] - object ICollection.SyncRoot - { - get - { - if (_syncRoot == null) - { - Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null); - } - - return _syncRoot; - } - } + object ICollection.SyncRoot => + field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field; #endregion } } diff --git a/src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/Switch.cs b/src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/Switch.cs index a6b4f3c41ec044..035debdb32a3c9 100644 --- a/src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/Switch.cs +++ b/src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/Switch.cs @@ -25,25 +25,14 @@ public abstract class Switch private bool _initializing; private volatile string? _switchValueString = string.Empty; private readonly string _defaultValue; - private object? _initializedLock; private static readonly List> s_switches = new List>(); private static int s_LastCollectionCount; private StringDictionary? _attributes; - private object InitializedLock - { - get - { - if (_initializedLock == null) - { - object o = new object(); - Interlocked.CompareExchange(ref _initializedLock, o, null); - } + private object InitializedLock => + field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field; - return _initializedLock; - } - } /// /// Initializes a new instance of the diff --git a/src/libraries/System.Private.CoreLib/src/System/TimeZone.cs b/src/libraries/System.Private.CoreLib/src/System/TimeZone.cs index f8796313cc1cf5..107a8052f253dc 100644 --- a/src/libraries/System.Private.CoreLib/src/System/TimeZone.cs +++ b/src/libraries/System.Private.CoreLib/src/System/TimeZone.cs @@ -15,19 +15,8 @@ public abstract class TimeZone private static volatile TimeZone? currentTimeZone; // Private object for locking instead of locking on a public type for SQL reliability work. - private static object? s_InternalSyncObject; - private static object InternalSyncObject - { - get - { - if (s_InternalSyncObject == null) - { - object o = new object(); - Interlocked.CompareExchange(ref s_InternalSyncObject, o, null); - } - return s_InternalSyncObject; - } - } + private static object InternalSyncObject => + field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field; protected TimeZone() { diff --git a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs index b96094746ea465..8b21982599f04b 100644 --- a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs +++ b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs @@ -75,7 +75,7 @@ public XNode? LastNode XText t = new XText(s); t.parent = this; t.next = t; - Interlocked.CompareExchange(ref content, t, s); + Interlocked.CompareExchange(ref content, t, s); } return (XNode)content; } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaSet.cs b/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaSet.cs index db8d7a7396296c..3443437867a6e9 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaSet.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaSet.cs @@ -56,20 +56,8 @@ public class XmlSchemaSet private XmlSchemaObjectTable? _typeExtensions; //Thread safety - private object? _internalSyncObject; - internal object InternalSyncObject - { - get - { - if (_internalSyncObject == null) - { - object o = new object(); - Interlocked.CompareExchange(ref _internalSyncObject, o, null); - } - - return _internalSyncObject; - } - } + internal object InternalSyncObject => + field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field; //Constructors diff --git a/src/libraries/System.Text.Encoding.CodePages/src/System/Text/DBCSCodePageEncoding.cs b/src/libraries/System.Text.Encoding.CodePages/src/System/Text/DBCSCodePageEncoding.cs index 8bd9670e356bfc..ae3cdfbb6b4a89 100644 --- a/src/libraries/System.Text.Encoding.CodePages/src/System/Text/DBCSCodePageEncoding.cs +++ b/src/libraries/System.Text.Encoding.CodePages/src/System/Text/DBCSCodePageEncoding.cs @@ -225,19 +225,8 @@ protected virtual unsafe void CleanUpEndBytes(char* chars) } // Private object for locking instead of locking on a public type for SQL reliability work. - private static object? s_InternalSyncObject; - private static object InternalSyncObject - { - get - { - if (s_InternalSyncObject == null) - { - object o = new object(); - Interlocked.CompareExchange(ref s_InternalSyncObject, o, null); - } - return s_InternalSyncObject; - } - } + private static object InternalSyncObject => + field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field; // Read in our best fit table protected override unsafe void ReadBestFitTable() diff --git a/src/libraries/System.Text.Encoding.CodePages/src/System/Text/DecoderBestFitFallback.cs b/src/libraries/System.Text.Encoding.CodePages/src/System/Text/DecoderBestFitFallback.cs index 90eb8b958e3e4e..66aff6b17a675f 100644 --- a/src/libraries/System.Text.Encoding.CodePages/src/System/Text/DecoderBestFitFallback.cs +++ b/src/libraries/System.Text.Encoding.CodePages/src/System/Text/DecoderBestFitFallback.cs @@ -47,19 +47,8 @@ internal sealed class InternalDecoderBestFitFallbackBuffer : DecoderFallbackBuff private readonly InternalDecoderBestFitFallback _oFallback; // Private object for locking instead of locking on a public type for SQL reliability work. - private static object? s_InternalSyncObject; - private static object InternalSyncObject - { - get - { - if (s_InternalSyncObject == null) - { - object o = new object(); - Interlocked.CompareExchange(ref s_InternalSyncObject, o, null); - } - return s_InternalSyncObject; - } - } + private static object InternalSyncObject => + field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field; // Constructor public InternalDecoderBestFitFallbackBuffer(InternalDecoderBestFitFallback fallback) diff --git a/src/libraries/System.Text.Encoding.CodePages/src/System/Text/EncoderBestFitFallback.cs b/src/libraries/System.Text.Encoding.CodePages/src/System/Text/EncoderBestFitFallback.cs index 1342edf8b15c49..2aa7cf75910a92 100644 --- a/src/libraries/System.Text.Encoding.CodePages/src/System/Text/EncoderBestFitFallback.cs +++ b/src/libraries/System.Text.Encoding.CodePages/src/System/Text/EncoderBestFitFallback.cs @@ -47,19 +47,8 @@ internal sealed class InternalEncoderBestFitFallbackBuffer : EncoderFallbackBuff private int _iSize; // Private object for locking instead of locking on a public type for SQL reliability work. - private static object? s_InternalSyncObject; - private static object InternalSyncObject - { - get - { - if (s_InternalSyncObject == null) - { - object o = new object(); - Interlocked.CompareExchange(ref s_InternalSyncObject, o, null); - } - return s_InternalSyncObject; - } - } + private static object InternalSyncObject => + field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field; // Constructor public InternalEncoderBestFitFallbackBuffer(InternalEncoderBestFitFallback fallback) diff --git a/src/libraries/System.Text.Encoding.CodePages/src/System/Text/SBCSCodePageEncoding.cs b/src/libraries/System.Text.Encoding.CodePages/src/System/Text/SBCSCodePageEncoding.cs index 184f88b8e656b0..5fda8e1215bf37 100644 --- a/src/libraries/System.Text.Encoding.CodePages/src/System/Text/SBCSCodePageEncoding.cs +++ b/src/libraries/System.Text.Encoding.CodePages/src/System/Text/SBCSCodePageEncoding.cs @@ -129,19 +129,8 @@ protected override unsafe void LoadManagedCodePage() } // Private object for locking instead of locking on a public type for SQL reliability work. - private static object? s_InternalSyncObject; - private static object InternalSyncObject - { - get - { - if (s_InternalSyncObject == null) - { - object o = new object(); - Interlocked.CompareExchange(ref s_InternalSyncObject, o, null); - } - return s_InternalSyncObject; - } - } + private static object InternalSyncObject => + field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field; // Read in our best fit table protected override unsafe void ReadBestFitTable()