Skip to content

Commit 0300b04

Browse files
committed
Cleanup uses of Interlocked.CompareExchange<object>
1 parent 94b6d05 commit 0300b04

File tree

14 files changed

+27
-174
lines changed

14 files changed

+27
-174
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private bool InvocationListEquals(MulticastDelegate d)
144144

145145
private static bool TrySetSlot(object?[] a, int index, object o)
146146
{
147-
if (a[index] == null && Threading.Interlocked.CompareExchange<object?>(ref a[index], o, null) == null)
147+
if (a[index] == null && Threading.Interlocked.CompareExchange(ref a[index], o, null) == null)
148148
return true;
149149

150150
// 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/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Builder.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ public sealed class Builder : IDictionary<TKey, TValue>, IReadOnlyDictionary<TKe
5656
/// </summary>
5757
private int _version;
5858

59-
/// <summary>
60-
/// The object callers may use to synchronize access to this collection.
61-
/// </summary>
62-
private object? _syncRoot;
63-
6459
/// <summary>
6560
/// Initializes a new instance of the <see cref="ImmutableDictionary{TKey, TValue}.Builder"/> class.
6661
/// </summary>
@@ -251,18 +246,8 @@ ICollection IDictionary.Values
251246
/// </summary>
252247
/// <returns>An object that can be used to synchronize access to the <see cref="ICollection"/>.</returns>
253248
[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-
}
249+
object ICollection.SyncRoot =>
250+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
266251

267252
/// <summary>
268253
/// 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: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ public sealed class Builder : IList<T>, IList, IReadOnlyList<T>
4646
/// </summary>
4747
private int _version;
4848

49-
/// <summary>
50-
/// The object callers may use to synchronize access to this collection.
51-
/// </summary>
52-
private object? _syncRoot;
53-
5449
/// <summary>
5550
/// Initializes a new instance of the <see cref="Builder"/> class.
5651
/// </summary>
@@ -1159,18 +1154,8 @@ bool ICollection.IsSynchronized
11591154
/// </summary>
11601155
/// <returns>An object that can be used to synchronize access to the <see cref="ICollection"/>.</returns>
11611156
[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-
}
1157+
object ICollection.SyncRoot =>
1158+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field; {
11741159
#endregion
11751160
}
11761161
}

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ public sealed class Builder : IDictionary<TKey, TValue>, IReadOnlyDictionary<TKe
5959
/// </summary>
6060
private int _version;
6161

62-
/// <summary>
63-
/// The object callers may use to synchronize access to this collection.
64-
/// </summary>
65-
private object? _syncRoot;
66-
6762
/// <summary>
6863
/// Initializes a new instance of the <see cref="Builder"/> class.
6964
/// </summary>
@@ -262,18 +257,8 @@ ICollection IDictionary.Values
262257
/// </summary>
263258
/// <returns>An object that can be used to synchronize access to the <see cref="ICollection"/>.</returns>
264259
[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-
}
260+
object ICollection.SyncRoot =>
261+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
277262

278263
/// <summary>
279264
/// 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: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ public sealed class Builder : IReadOnlyCollection<T>, ISet<T>, ICollection
5151
/// </summary>
5252
private int _version;
5353

54-
/// <summary>
55-
/// The object callers may use to synchronize access to this collection.
56-
/// </summary>
57-
private object? _syncRoot;
58-
5954
/// <summary>
6055
/// Initializes a new instance of the <see cref="Builder"/> class.
6156
/// </summary>
@@ -505,18 +500,8 @@ bool ICollection.IsSynchronized
505500
/// </summary>
506501
/// <returns>An object that can be used to synchronize access to the <see cref="ICollection"/>.</returns>
507502
[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-
}
503+
object ICollection.SyncRoot =>
504+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
520505
#endregion
521506
}
522507
}

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
}

src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaSet.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,8 @@ public class XmlSchemaSet
5656
private XmlSchemaObjectTable? _typeExtensions;
5757

5858
//Thread safety
59-
private object? _internalSyncObject;
60-
internal object InternalSyncObject
61-
{
62-
get
63-
{
64-
if (_internalSyncObject == null)
65-
{
66-
object o = new object();
67-
Interlocked.CompareExchange<object?>(ref _internalSyncObject, o, null);
68-
}
69-
70-
return _internalSyncObject;
71-
}
72-
}
59+
internal object InternalSyncObject =>
60+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
7361

7462
//Constructors
7563

0 commit comments

Comments
 (0)