Skip to content

Commit b3037c6

Browse files
authored
Cleanup uses of Interlocked.CompareExchange<object> (#117858)
1 parent a656a36 commit b3037c6

File tree

15 files changed

+34
-175
lines changed

15 files changed

+34
-175
lines changed

src/coreclr/System.Private.CoreLib/src/System/MulticastDelegate.CoreCLR.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Runtime.CompilerServices;
1010
using System.Runtime.InteropServices;
1111
using System.Runtime.Serialization;
12+
using System.Threading;
1213

1314
namespace System
1415
{
@@ -144,7 +145,7 @@ private bool InvocationListEquals(MulticastDelegate d)
144145

145146
private static bool TrySetSlot(object?[] a, int index, object o)
146147
{
147-
if (a[index] == null && Threading.Interlocked.CompareExchange<object?>(ref a[index], o, null) == null)
148+
if (a[index] == null && Interlocked.CompareExchange(ref a[index], o, null) == null)
148149
return true;
149150

150151
// The slot may be already set because we have added and removed the same method before.

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal sealed partial class RuntimeAssembly : Assembly
3434

3535
#endregion
3636

37-
internal IntPtr GetUnderlyingNativeHandle() { return m_assembly; }
37+
internal IntPtr GetUnderlyingNativeHandle() => m_assembly;
3838

3939
private sealed class ManifestResourceStream : UnmanagedMemoryStream
4040
{
@@ -52,17 +52,8 @@ internal unsafe ManifestResourceStream(RuntimeAssembly manifestAssembly, byte* p
5252
// NOTE: no reason to override Write(Span<byte>), since a ManifestResourceStream is read-only.
5353
}
5454

55-
internal object SyncRoot
56-
{
57-
get
58-
{
59-
if (m_syncRoot == null)
60-
{
61-
Interlocked.CompareExchange<object?>(ref m_syncRoot, new object(), null);
62-
}
63-
return m_syncRoot;
64-
}
65-
}
55+
internal object SyncRoot =>
56+
m_syncRoot ?? Interlocked.CompareExchange(ref m_syncRoot, new object(), null) ?? m_syncRoot;
6657

6758
public override event ModuleResolveEventHandler? ModuleResolve
6859
{

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Delegate.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Runtime.CompilerServices;
1010
using System.Runtime.InteropServices;
1111
using System.Runtime.Serialization;
12+
using System.Threading;
1213

1314
using Internal.Reflection.Augments;
1415
using Internal.Runtime;
@@ -465,7 +466,7 @@ private unsafe Delegate NewMulticastDelegate(Wrapper[] invocationList, int invoc
465466

466467
private static bool TrySetSlot(Wrapper[] a, int index, Delegate o)
467468
{
468-
if (a[index].Value == null && System.Threading.Interlocked.CompareExchange(ref a[index].Value, o, null) == null)
469+
if (a[index].Value == null && Interlocked.CompareExchange(ref a[index].Value, o, null) == null)
469470
return true;
470471

471472
// The slot may be already set because we have added and removed the same method before.

src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Builder.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7+
using System.Threading;
78

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

59-
/// <summary>
60-
/// The object callers may use to synchronize access to this collection.
61-
/// </summary>
62-
private object? _syncRoot;
63-
6460
/// <summary>
6561
/// Initializes a new instance of the <see cref="ImmutableDictionary{TKey, TValue}.Builder"/> class.
6662
/// </summary>
@@ -251,18 +247,8 @@ ICollection IDictionary.Values
251247
/// </summary>
252248
/// <returns>An object that can be used to synchronize access to the <see cref="ICollection"/>.</returns>
253249
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
254-
object ICollection.SyncRoot
255-
{
256-
get
257-
{
258-
if (_syncRoot == null)
259-
{
260-
Threading.Interlocked.CompareExchange<object?>(ref _syncRoot, new object(), null);
261-
}
262-
263-
return _syncRoot;
264-
}
265-
}
250+
object ICollection.SyncRoot =>
251+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
266252

267253
/// <summary>
268254
/// Gets a value indicating whether access to the <see cref="ICollection"/> is synchronized (thread safe).

src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7+
using System.Threading;
78

89
namespace System.Collections.Immutable
910
{
@@ -46,11 +47,6 @@ public sealed class Builder : IList<T>, IList, IReadOnlyList<T>
4647
/// </summary>
4748
private int _version;
4849

49-
/// <summary>
50-
/// The object callers may use to synchronize access to this collection.
51-
/// </summary>
52-
private object? _syncRoot;
53-
5450
/// <summary>
5551
/// Initializes a new instance of the <see cref="Builder"/> class.
5652
/// </summary>
@@ -1159,18 +1155,8 @@ bool ICollection.IsSynchronized
11591155
/// </summary>
11601156
/// <returns>An object that can be used to synchronize access to the <see cref="ICollection"/>.</returns>
11611157
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
1162-
object ICollection.SyncRoot
1163-
{
1164-
get
1165-
{
1166-
if (_syncRoot == null)
1167-
{
1168-
System.Threading.Interlocked.CompareExchange<object?>(ref _syncRoot, new object(), null);
1169-
}
1170-
1171-
return _syncRoot;
1172-
}
1173-
}
1158+
object ICollection.SyncRoot =>
1159+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
11741160
#endregion
11751161
}
11761162
}

src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Builder.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7+
using System.Threading;
78

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

62-
/// <summary>
63-
/// The object callers may use to synchronize access to this collection.
64-
/// </summary>
65-
private object? _syncRoot;
66-
6763
/// <summary>
6864
/// Initializes a new instance of the <see cref="Builder"/> class.
6965
/// </summary>
@@ -262,18 +258,8 @@ ICollection IDictionary.Values
262258
/// </summary>
263259
/// <returns>An object that can be used to synchronize access to the <see cref="ICollection"/>.</returns>
264260
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
265-
object ICollection.SyncRoot
266-
{
267-
get
268-
{
269-
if (_syncRoot == null)
270-
{
271-
Threading.Interlocked.CompareExchange<object?>(ref _syncRoot, new object(), null);
272-
}
273-
274-
return _syncRoot;
275-
}
276-
}
261+
object ICollection.SyncRoot =>
262+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
277263

278264
/// <summary>
279265
/// Gets a value indicating whether access to the <see cref="ICollection"/> is synchronized (thread safe).

src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7+
using System.Threading;
78

89
namespace System.Collections.Immutable
910
{
@@ -51,11 +52,6 @@ public sealed class Builder : IReadOnlyCollection<T>, ISet<T>, ICollection
5152
/// </summary>
5253
private int _version;
5354

54-
/// <summary>
55-
/// The object callers may use to synchronize access to this collection.
56-
/// </summary>
57-
private object? _syncRoot;
58-
5955
/// <summary>
6056
/// Initializes a new instance of the <see cref="Builder"/> class.
6157
/// </summary>
@@ -505,18 +501,8 @@ bool ICollection.IsSynchronized
505501
/// </summary>
506502
/// <returns>An object that can be used to synchronize access to the <see cref="ICollection"/>.</returns>
507503
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
508-
object ICollection.SyncRoot
509-
{
510-
get
511-
{
512-
if (_syncRoot == null)
513-
{
514-
Threading.Interlocked.CompareExchange<object?>(ref _syncRoot, new object(), null);
515-
}
516-
517-
return _syncRoot;
518-
}
519-
}
504+
object ICollection.SyncRoot =>
505+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
520506
#endregion
521507
}
522508
}

src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/Switch.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,14 @@ public abstract class Switch
2525
private bool _initializing;
2626
private volatile string? _switchValueString = string.Empty;
2727
private readonly string _defaultValue;
28-
private object? _initializedLock;
2928

3029
private static readonly List<WeakReference<Switch>> s_switches = new List<WeakReference<Switch>>();
3130
private static int s_LastCollectionCount;
3231
private StringDictionary? _attributes;
3332

34-
private object InitializedLock
35-
{
36-
get
37-
{
38-
if (_initializedLock == null)
39-
{
40-
object o = new object();
41-
Interlocked.CompareExchange<object?>(ref _initializedLock, o, null);
42-
}
33+
private object InitializedLock =>
34+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
4335

44-
return _initializedLock;
45-
}
46-
}
4736

4837
/// <devdoc>
4938
/// <para>Initializes a new instance of the <see cref='System.Diagnostics.Switch'/>

src/libraries/System.Private.CoreLib/src/System/TimeZone.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,8 @@ public abstract class TimeZone
1515
private static volatile TimeZone? currentTimeZone;
1616

1717
// Private object for locking instead of locking on a public type for SQL reliability work.
18-
private static object? s_InternalSyncObject;
19-
private static object InternalSyncObject
20-
{
21-
get
22-
{
23-
if (s_InternalSyncObject == null)
24-
{
25-
object o = new object();
26-
Interlocked.CompareExchange<object?>(ref s_InternalSyncObject, o, null);
27-
}
28-
return s_InternalSyncObject;
29-
}
30-
}
18+
private static object InternalSyncObject =>
19+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
3120

3221
protected TimeZone()
3322
{

src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public XNode? LastNode
7575
XText t = new XText(s);
7676
t.parent = this;
7777
t.next = t;
78-
Interlocked.CompareExchange<object>(ref content, t, s);
78+
Interlocked.CompareExchange(ref content, t, s);
7979
}
8080
return (XNode)content;
8181
}

0 commit comments

Comments
 (0)