@@ -10,36 +10,20 @@ 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.
1613 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 ) ;
22- }
23- }
24-
25- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
26- public static double Read ( ref double location )
27- => Interlocked . CompareExchange ( ref location , 0 , 0 ) ;
28-
29- [ MethodImpl ( MethodImplOptions . NoInlining ) ]
30- private static void AddRare ( ref double location , double value )
31- {
32- var sw = default ( SpinWait ) ;
3314 while ( true )
3415 {
35- sw . SpinOnce ( ) ;
36-
37- double currentValue = Volatile . Read ( ref location ) ;
3816 var returnedValue = Interlocked . CompareExchange ( ref location , currentValue + value , currentValue ) ;
3917 if ( returnedValue == currentValue )
4018 {
4119 break ;
4220 }
21+
22+ currentValue = returnedValue ;
4323 }
4424 }
25+
26+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
27+ public static double Read ( ref double location )
28+ => Interlocked . CompareExchange ( ref location , 0 , 0 ) ;
4529}
0 commit comments