Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Threading;

namespace System
{
Expand Down Expand Up @@ -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<object?>(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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -52,17 +52,8 @@ internal unsafe ManifestResourceStream(RuntimeAssembly manifestAssembly, byte* p
// NOTE: no reason to override Write(Span<byte>), since a ManifestResourceStream is read-only.
}

internal object SyncRoot
{
get
{
if (m_syncRoot == null)
{
Interlocked.CompareExchange<object?>(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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace System.Collections.Immutable
{
Expand Down Expand Up @@ -56,11 +57,6 @@ public sealed class Builder : IDictionary<TKey, TValue>, IReadOnlyDictionary<TKe
/// </summary>
private int _version;

/// <summary>
/// The object callers may use to synchronize access to this collection.
/// </summary>
private object? _syncRoot;

/// <summary>
/// Initializes a new instance of the <see cref="ImmutableDictionary{TKey, TValue}.Builder"/> class.
/// </summary>
Expand Down Expand Up @@ -251,18 +247,8 @@ ICollection IDictionary.Values
/// </summary>
/// <returns>An object that can be used to synchronize access to the <see cref="ICollection"/>.</returns>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
object ICollection.SyncRoot
{
get
{
if (_syncRoot == null)
{
Threading.Interlocked.CompareExchange<object?>(ref _syncRoot, new object(), null);
}

return _syncRoot;
}
}
object ICollection.SyncRoot =>
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;

/// <summary>
/// Gets a value indicating whether access to the <see cref="ICollection"/> is synchronized (thread safe).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace System.Collections.Immutable
{
Expand Down Expand Up @@ -46,11 +47,6 @@ public sealed class Builder : IList<T>, IList, IReadOnlyList<T>
/// </summary>
private int _version;

/// <summary>
/// The object callers may use to synchronize access to this collection.
/// </summary>
private object? _syncRoot;

/// <summary>
/// Initializes a new instance of the <see cref="Builder"/> class.
/// </summary>
Expand Down Expand Up @@ -1159,18 +1155,8 @@ bool ICollection.IsSynchronized
/// </summary>
/// <returns>An object that can be used to synchronize access to the <see cref="ICollection"/>.</returns>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
object ICollection.SyncRoot
{
get
{
if (_syncRoot == null)
{
System.Threading.Interlocked.CompareExchange<object?>(ref _syncRoot, new object(), null);
}

return _syncRoot;
}
}
object ICollection.SyncRoot =>
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
#endregion
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace System.Collections.Immutable
{
Expand Down Expand Up @@ -59,11 +60,6 @@ public sealed class Builder : IDictionary<TKey, TValue>, IReadOnlyDictionary<TKe
/// </summary>
private int _version;

/// <summary>
/// The object callers may use to synchronize access to this collection.
/// </summary>
private object? _syncRoot;

/// <summary>
/// Initializes a new instance of the <see cref="Builder"/> class.
/// </summary>
Expand Down Expand Up @@ -262,18 +258,8 @@ ICollection IDictionary.Values
/// </summary>
/// <returns>An object that can be used to synchronize access to the <see cref="ICollection"/>.</returns>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
object ICollection.SyncRoot
{
get
{
if (_syncRoot == null)
{
Threading.Interlocked.CompareExchange<object?>(ref _syncRoot, new object(), null);
}

return _syncRoot;
}
}
object ICollection.SyncRoot =>
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;

/// <summary>
/// Gets a value indicating whether access to the <see cref="ICollection"/> is synchronized (thread safe).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace System.Collections.Immutable
{
Expand Down Expand Up @@ -51,11 +52,6 @@ public sealed class Builder : IReadOnlyCollection<T>, ISet<T>, ICollection
/// </summary>
private int _version;

/// <summary>
/// The object callers may use to synchronize access to this collection.
/// </summary>
private object? _syncRoot;

/// <summary>
/// Initializes a new instance of the <see cref="Builder"/> class.
/// </summary>
Expand Down Expand Up @@ -505,18 +501,8 @@ bool ICollection.IsSynchronized
/// </summary>
/// <returns>An object that can be used to synchronize access to the <see cref="ICollection"/>.</returns>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
object ICollection.SyncRoot
{
get
{
if (_syncRoot == null)
{
Threading.Interlocked.CompareExchange<object?>(ref _syncRoot, new object(), null);
}

return _syncRoot;
}
}
object ICollection.SyncRoot =>
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
#endregion
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<WeakReference<Switch>> s_switches = new List<WeakReference<Switch>>();
private static int s_LastCollectionCount;
private StringDictionary? _attributes;

private object InitializedLock
{
get
{
if (_initializedLock == null)
{
object o = new object();
Interlocked.CompareExchange<object?>(ref _initializedLock, o, null);
}
private object InitializedLock =>
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;

return _initializedLock;
}
}

/// <devdoc>
/// <para>Initializes a new instance of the <see cref='System.Diagnostics.Switch'/>
Expand Down
15 changes: 2 additions & 13 deletions src/libraries/System.Private.CoreLib/src/System/TimeZone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object?>(ref s_InternalSyncObject, o, null);
}
return s_InternalSyncObject;
}
}
private static object InternalSyncObject =>
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;

protected TimeZone()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public XNode? LastNode
XText t = new XText(s);
t.parent = this;
t.next = t;
Interlocked.CompareExchange<object>(ref content, t, s);
Interlocked.CompareExchange(ref content, t, s);
}
return (XNode)content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object?>(ref _internalSyncObject, o, null);
}

return _internalSyncObject;
}
}
internal object InternalSyncObject =>
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;

//Constructors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object?>(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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object?>(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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object?>(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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object?>(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()
Expand Down
Loading