Skip to content

Commit 3593cee

Browse files
committed
Remove Interlocked -> Volatile changes
1 parent 4212345 commit 3593cee

File tree

5 files changed

+36
-19
lines changed

5 files changed

+36
-19
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/DirectorySizeTracker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public DirectorySizeTracker(long maxSizeInBytes, string path)
3636
/// <returns>True if space is available else false.</returns>
3737
public bool IsSpaceAvailable(out long currentSizeInBytes)
3838
{
39-
currentSizeInBytes = Volatile.Read(ref this.directoryCurrentSizeInBytes);
39+
currentSizeInBytes = Interlocked.Read(ref this.directoryCurrentSizeInBytes);
4040
return currentSizeInBytes < this.maxSizeInBytes;
4141
}
4242

4343
public void RecountCurrentSize()
4444
{
4545
var size = CalculateFolderSize(this.path);
46-
Volatile.Write(ref this.directoryCurrentSizeInBytes, size);
46+
Interlocked.Exchange(ref this.directoryCurrentSizeInBytes, size);
4747
}
4848

4949
internal static long CalculateFolderSize(string path)

src/OpenTelemetry/Internal/InterlockedHelper.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,30 @@ internal static class InterlockedHelper
1010
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1111
public static void Add(ref double location, double value)
1212
{
13+
// Note: Not calling InterlockedHelper.Read here on purpose because it
14+
// is too expensive for fast/happy-path. If the first attempt fails
15+
// we'll end up in an Interlocked.CompareExchange loop anyway.
1316
double currentValue = Volatile.Read(ref location);
17+
18+
var returnedValue = Interlocked.CompareExchange(ref location, currentValue + value, currentValue);
19+
if (returnedValue != currentValue)
20+
{
21+
AddRare(ref location, value, returnedValue);
22+
}
23+
}
24+
25+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
26+
public static double Read(ref double location)
27+
=> Interlocked.CompareExchange(ref location, double.NaN, double.NaN);
28+
29+
[MethodImpl(MethodImplOptions.NoInlining)]
30+
private static void AddRare(ref double location, double value, double currentValue)
31+
{
32+
var sw = default(SpinWait);
1433
while (true)
1534
{
35+
sw.SpinOnce();
36+
1637
var returnedValue = Interlocked.CompareExchange(ref location, currentValue + value, currentValue);
1738
if (returnedValue == currentValue)
1839
{
@@ -22,8 +43,4 @@ public static void Add(ref double location, double value)
2243
currentValue = returnedValue;
2344
}
2445
}
25-
26-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
27-
public static double Read(ref double location)
28-
=> Interlocked.CompareExchange(ref location, 0, 0);
2946
}

src/OpenTelemetry/Metrics/Exemplar/Exemplar.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ internal void Update<T>(in ExemplarMeasurement<T> measurement)
135135
this.StoreRawTags(measurement.Tags);
136136
}
137137

138-
Volatile.Write(ref this.isCriticalSectionOccupied, 0);
138+
Interlocked.Exchange(ref this.isCriticalSectionOccupied, 0);
139139
}
140140

141141
internal void Reset()
@@ -168,7 +168,7 @@ internal void Collect(ref Exemplar destination, bool reset)
168168
destination.Reset();
169169
}
170170

171-
Volatile.Write(ref this.isCriticalSectionOccupied, 0);
171+
Interlocked.Exchange(ref this.isCriticalSectionOccupied, 0);
172172
}
173173

174174
internal readonly void Copy(ref Exemplar destination)

src/OpenTelemetry/Metrics/MetricPoint/MetricPoint.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ internal void Update(long number)
394394
case AggregationType.LongSumIncomingCumulative:
395395
case AggregationType.LongGauge:
396396
{
397-
Volatile.Write(ref this.runningValue.AsLong, number);
397+
Interlocked.Exchange(ref this.runningValue.AsLong, number);
398398
break;
399399
}
400400

@@ -451,7 +451,7 @@ internal void UpdateWithExemplar(long number, ReadOnlySpan<KeyValuePair<string,
451451
case AggregationType.LongSumIncomingCumulative:
452452
case AggregationType.LongGauge:
453453
{
454-
Volatile.Write(ref this.runningValue.AsLong, number);
454+
Interlocked.Exchange(ref this.runningValue.AsLong, number);
455455
break;
456456
}
457457

@@ -510,7 +510,7 @@ internal void Update(double number)
510510
case AggregationType.DoubleSumIncomingCumulative:
511511
case AggregationType.DoubleGauge:
512512
{
513-
Volatile.Write(ref this.runningValue.AsDouble, number);
513+
Interlocked.Exchange(ref this.runningValue.AsDouble, number);
514514
break;
515515
}
516516

@@ -567,7 +567,7 @@ internal void UpdateWithExemplar(double number, ReadOnlySpan<KeyValuePair<string
567567
case AggregationType.DoubleSumIncomingCumulative:
568568
case AggregationType.DoubleGauge:
569569
{
570-
Volatile.Write(ref this.runningValue.AsDouble, number);
570+
Interlocked.Exchange(ref this.runningValue.AsDouble, number);
571571
break;
572572
}
573573

@@ -622,7 +622,7 @@ internal void TakeSnapshot(bool outputDelta)
622622
{
623623
if (outputDelta)
624624
{
625-
long initValue = Volatile.Read(ref this.runningValue.AsLong);
625+
long initValue = Interlocked.Read(ref this.runningValue.AsLong);
626626
this.snapshotValue.AsLong = initValue - this.deltaLastValue.AsLong;
627627
this.deltaLastValue.AsLong = initValue;
628628
this.MetricPointStatus = MetricPointStatus.NoCollectPending;
@@ -636,7 +636,7 @@ internal void TakeSnapshot(bool outputDelta)
636636
}
637637
else
638638
{
639-
this.snapshotValue.AsLong = Volatile.Read(ref this.runningValue.AsLong);
639+
this.snapshotValue.AsLong = Interlocked.Read(ref this.runningValue.AsLong);
640640
}
641641

642642
break;
@@ -647,7 +647,7 @@ internal void TakeSnapshot(bool outputDelta)
647647
{
648648
if (outputDelta)
649649
{
650-
double initValue = Volatile.Read(ref this.runningValue.AsDouble);
650+
double initValue = InterlockedHelper.Read(ref this.runningValue.AsDouble);
651651
this.snapshotValue.AsDouble = initValue - this.deltaLastValue.AsDouble;
652652
this.deltaLastValue.AsDouble = initValue;
653653
this.MetricPointStatus = MetricPointStatus.NoCollectPending;
@@ -661,15 +661,15 @@ internal void TakeSnapshot(bool outputDelta)
661661
}
662662
else
663663
{
664-
this.snapshotValue.AsDouble = Volatile.Read(ref this.runningValue.AsDouble);
664+
this.snapshotValue.AsDouble = InterlockedHelper.Read(ref this.runningValue.AsDouble);
665665
}
666666

667667
break;
668668
}
669669

670670
case AggregationType.LongGauge:
671671
{
672-
this.snapshotValue.AsLong = Volatile.Read(ref this.runningValue.AsLong);
672+
this.snapshotValue.AsLong = Interlocked.Read(ref this.runningValue.AsLong);
673673
this.MetricPointStatus = MetricPointStatus.NoCollectPending;
674674

675675
// Check again if value got updated, if yes reset status.
@@ -684,7 +684,7 @@ internal void TakeSnapshot(bool outputDelta)
684684

685685
case AggregationType.DoubleGauge:
686686
{
687-
this.snapshotValue.AsDouble = Volatile.Read(ref this.runningValue.AsDouble);
687+
this.snapshotValue.AsDouble = InterlockedHelper.Read(ref this.runningValue.AsDouble);
688688
this.MetricPointStatus = MetricPointStatus.NoCollectPending;
689689

690690
// Check again if value got updated, if yes reset status.

src/OpenTelemetry/Metrics/MetricPoint/MetricPointOptionalComponents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void AcquireLock()
4848
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4949
public void ReleaseLock()
5050
{
51-
Volatile.Write(ref this.isCriticalSectionOccupied, 0);
51+
Interlocked.Exchange(ref this.isCriticalSectionOccupied, 0);
5252
}
5353

5454
// Note: This method is marked as NoInlining because the whole point of it

0 commit comments

Comments
 (0)