Skip to content

Commit 96f5412

Browse files
committed
Fix ThreadSafeDouble, ThreadSafeLong
1 parent 1ddd539 commit 96f5412

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

src/Prometheus.Client/Internal/ThreadSafeDouble.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ public void Add(double increment)
2626
{
2727
while (true)
2828
{
29-
long initialValue = _value;
30-
double computedValue = BitConverter.Int64BitsToDouble(initialValue) + increment;
29+
var initialValue = Interlocked.Read(ref _value);
30+
var computedValue = BitConverter.Int64BitsToDouble(initialValue) + increment;
3131

3232
if (initialValue == Interlocked.CompareExchange(ref _value, BitConverter.DoubleToInt64Bits(computedValue), initialValue))
3333
return;
3434
}
35+
3536
}
3637

3738
public override string ToString()

src/Prometheus.Client/Internal/ThreadSafeLong.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,7 @@ public long Value
2323

2424
public void Add(long increment)
2525
{
26-
while (true)
27-
{
28-
long initialValue = _value;
29-
long computedValue = initialValue + increment;
30-
31-
if (initialValue == Interlocked.CompareExchange(ref _value, computedValue, initialValue))
32-
return;
33-
}
26+
Interlocked.Add(ref _value, increment);
3427
}
3528

3629
public override string ToString()

0 commit comments

Comments
 (0)