Add SpinWait for double counter updates (#2960)

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
This commit is contained in:
Utkarsh Umesan Pillai 2022-03-07 18:17:56 -08:00 committed by GitHub
parent 83c767148a
commit 6973a539d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 3 deletions

View File

@ -290,12 +290,24 @@ namespace OpenTelemetry.Metrics
case AggregationType.DoubleSumIncomingDelta:
{
double initValue, newValue;
do
var sw = default(SpinWait);
while (true)
{
initValue = this.runningValue.AsDouble;
newValue = initValue + number;
unchecked
{
newValue = initValue + number;
}
if (initValue == Interlocked.CompareExchange(ref this.runningValue.AsDouble, newValue, initValue))
{
break;
}
sw.SpinOnce();
}
while (initValue != Interlocked.CompareExchange(ref this.runningValue.AsDouble, newValue, initValue));
break;
}