Skip to content

Commit 39691b6

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

File tree

9 files changed

+18
-105
lines changed

9 files changed

+18
-105
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.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/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

src/libraries/System.Text.Encoding.CodePages/src/System/Text/DBCSCodePageEncoding.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,8 @@ protected virtual unsafe void CleanUpEndBytes(char* chars)
225225
}
226226

227227
// Private object for locking instead of locking on a public type for SQL reliability work.
228-
private static object? s_InternalSyncObject;
229-
private static object InternalSyncObject
230-
{
231-
get
232-
{
233-
if (s_InternalSyncObject == null)
234-
{
235-
object o = new object();
236-
Interlocked.CompareExchange<object?>(ref s_InternalSyncObject, o, null);
237-
}
238-
return s_InternalSyncObject;
239-
}
240-
}
228+
private static object InternalSyncObject =>
229+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
241230

242231
// Read in our best fit table
243232
protected override unsafe void ReadBestFitTable()

src/libraries/System.Text.Encoding.CodePages/src/System/Text/DecoderBestFitFallback.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,8 @@ internal sealed class InternalDecoderBestFitFallbackBuffer : DecoderFallbackBuff
4747
private readonly InternalDecoderBestFitFallback _oFallback;
4848

4949
// Private object for locking instead of locking on a public type for SQL reliability work.
50-
private static object? s_InternalSyncObject;
51-
private static object InternalSyncObject
52-
{
53-
get
54-
{
55-
if (s_InternalSyncObject == null)
56-
{
57-
object o = new object();
58-
Interlocked.CompareExchange<object?>(ref s_InternalSyncObject, o, null);
59-
}
60-
return s_InternalSyncObject;
61-
}
62-
}
50+
private static object InternalSyncObject =>
51+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
6352

6453
// Constructor
6554
public InternalDecoderBestFitFallbackBuffer(InternalDecoderBestFitFallback fallback)

src/libraries/System.Text.Encoding.CodePages/src/System/Text/EncoderBestFitFallback.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,8 @@ internal sealed class InternalEncoderBestFitFallbackBuffer : EncoderFallbackBuff
4747
private int _iSize;
4848

4949
// Private object for locking instead of locking on a public type for SQL reliability work.
50-
private static object? s_InternalSyncObject;
51-
private static object InternalSyncObject
52-
{
53-
get
54-
{
55-
if (s_InternalSyncObject == null)
56-
{
57-
object o = new object();
58-
Interlocked.CompareExchange<object?>(ref s_InternalSyncObject, o, null);
59-
}
60-
return s_InternalSyncObject;
61-
}
62-
}
50+
private static object InternalSyncObject =>
51+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
6352

6453
// Constructor
6554
public InternalEncoderBestFitFallbackBuffer(InternalEncoderBestFitFallback fallback)

src/libraries/System.Text.Encoding.CodePages/src/System/Text/SBCSCodePageEncoding.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,8 @@ protected override unsafe void LoadManagedCodePage()
129129
}
130130

131131
// Private object for locking instead of locking on a public type for SQL reliability work.
132-
private static object? s_InternalSyncObject;
133-
private static object InternalSyncObject
134-
{
135-
get
136-
{
137-
if (s_InternalSyncObject == null)
138-
{
139-
object o = new object();
140-
Interlocked.CompareExchange<object?>(ref s_InternalSyncObject, o, null);
141-
}
142-
return s_InternalSyncObject;
143-
}
144-
}
132+
private static object InternalSyncObject =>
133+
field ?? Interlocked.CompareExchange(ref field, new object(), null) ?? field;
145134

146135
// Read in our best fit table
147136
protected override unsafe void ReadBestFitTable()

0 commit comments

Comments
 (0)