Skip to content

Commit a59a2e1

Browse files
committed
Simplify lazy initialization by removing Volatile.Read
Replaced `Volatile.Read` with plain field reads. The memory barriers provided by `CompareExchange` are sufficient to ensure thread safety and visibility, making `Volatile.Read` unnecessary. Follow-up to dotnet#117817 Related: dotnet#100969 cc: @stephentoub
1 parent bf92e06 commit a59a2e1

File tree

2 files changed

+2
-6
lines changed
  • src/libraries
    • System.Diagnostics.TraceSource/src/System/Diagnostics
    • System.Private.CoreLib/src/System/Threading/Tasks

2 files changed

+2
-6
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ private Trace()
2020
}
2121

2222
public static CorrelationManager CorrelationManager =>
23-
Volatile.Read(ref field) ??
24-
Interlocked.CompareExchange(ref field, new(), null) ??
25-
field;
23+
field ?? Interlocked.CompareExchange(ref field, new(), null) ?? field;
2624

2725
/// <devdoc>
2826
/// <para>Gets the collection of listeners that is monitoring the trace output.</para>

src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,7 @@ internal TResult GetResultCore(bool waitCompletionNotification)
472472
/// the default constructor on the factory type.
473473
/// </remarks>
474474
public static new TaskFactory<TResult> Factory =>
475-
Volatile.Read(ref field) ??
476-
Interlocked.CompareExchange(ref field, new(), null) ??
477-
field;
475+
field ?? Interlocked.CompareExchange(ref field, new(), null) ?? field;
478476

479477
/// <summary>
480478
/// Evaluates the value selector of the Task which is passed in as an object and stores the result.

0 commit comments

Comments
 (0)