Skip to content

Commit

Permalink
Remove unnecessary volatile from EventCounter (#37309)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Jun 3, 2020
1 parent b40bd47 commit 1ae9925
Showing 1 changed file with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ public EventCounter(string name, EventSource eventSource) : base(name, eventSour
_min = double.PositiveInfinity;
_max = double.NegativeInfinity;

InitializeBuffer();
var bufferedValues = new double[BufferedSize];
for (int i = 0; i < bufferedValues.Length; i++)
{
bufferedValues[i] = UnusedBufferSlotValue;
}
Volatile.Write(ref _bufferedValues, bufferedValues);

Publish();
}

Expand Down Expand Up @@ -136,19 +142,9 @@ internal void ResetStatistics()
// Values buffering
private const int BufferedSize = 10;
private const double UnusedBufferSlotValue = double.NegativeInfinity;
private volatile double[] _bufferedValues;
private readonly double[] _bufferedValues;
private volatile int _bufferedValuesIndex;

[MemberNotNull(nameof(_bufferedValues))]
private void InitializeBuffer()
{
_bufferedValues = new double[BufferedSize];
for (int i = 0; i < _bufferedValues.Length; i++)
{
_bufferedValues[i] = UnusedBufferSlotValue;
}
}

private void Enqueue(double value)
{
// It is possible that two threads read the same bufferedValuesIndex, but only one will be able to write the slot, so that is okay.
Expand Down

0 comments on commit 1ae9925

Please sign in to comment.