Skip to content

Commit

Permalink
Use Volatile.Read over Interlocked.Read.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed Jun 18, 2022
1 parent 274dc44 commit 6c49e37
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void Subscribe()

public void OnNext(DiagnosticListener value)
{
if ((Interlocked.Read(ref this.disposed) == 0) &&
if ((Volatile.Read(ref this.disposed) == 0) &&
this.diagnosticSourceFilter(value))
{
var handler = this.handlerFactory(value.Name);
Expand Down
10 changes: 5 additions & 5 deletions src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,21 +403,21 @@ internal void TakeSnapshot(bool outputDelta)
{
if (outputDelta)
{
long initValue = Interlocked.Read(ref this.runningValue.AsLong);
long initValue = Volatile.Read(ref this.runningValue.AsLong);
this.snapshotValue.AsLong = initValue - this.deltaLastValue.AsLong;
this.deltaLastValue.AsLong = initValue;
this.MetricPointStatus = MetricPointStatus.NoCollectPending;

// Check again if value got updated, if yes reset status.
// This ensures no Updates get Lost.
if (initValue != Interlocked.Read(ref this.runningValue.AsLong))
if (initValue != Volatile.Read(ref this.runningValue.AsLong))
{
this.MetricPointStatus = MetricPointStatus.CollectPending;
}
}
else
{
this.snapshotValue.AsLong = Interlocked.Read(ref this.runningValue.AsLong);
this.snapshotValue.AsLong = Volatile.Read(ref this.runningValue.AsLong);
}

break;
Expand Down Expand Up @@ -460,12 +460,12 @@ internal void TakeSnapshot(bool outputDelta)

case AggregationType.LongGauge:
{
this.snapshotValue.AsLong = Interlocked.Read(ref this.runningValue.AsLong);
this.snapshotValue.AsLong = Volatile.Read(ref this.runningValue.AsLong);
this.MetricPointStatus = MetricPointStatus.NoCollectPending;

// Check again if value got updated, if yes reset status.
// This ensures no Updates get Lost.
if (this.snapshotValue.AsLong != Interlocked.Read(ref this.runningValue.AsLong))
if (this.snapshotValue.AsLong != Volatile.Read(ref this.runningValue.AsLong))
{
this.MetricPointStatus = MetricPointStatus.CollectPending;
}
Expand Down

0 comments on commit 6c49e37

Please sign in to comment.