Tests/IMToolsTests.cs
/// <summary>
/// Option without allocation for capturing `a` in closure of `getNewValue`
/// </summary>
Option without allocation for capturing `a` in closure of `getNewValue`
Option without allocation for capturing ` a ` in closure of ` getNewValue `
[MethodImpl((MethodImplOptions)256)]
        public static T Swap<T, A>(ref T value, A a, Func<T, A, T> getNewValue,
            int retryCountUntilThrow = RETRY_COUNT_UNTIL_THROW)
            where T : class
        {
#if SUPPORTS_SPIN_WAIT
            var spinWait = new SpinWait();
#endif
            var retryCount = 0;
            while (true)
            {
                var oldValue = value;
                var newValue = getNewValue(oldValue, a);
                if (Interlocked.CompareExchange(ref value, newValue, oldValue) == oldValue)
                    return oldValue;
                if (++retryCount > retryCountUntilThrow)
                    ThrowRetryCountExceeded(retryCountUntilThrow);
#if SUPPORTS_SPIN_WAIT
                spinWait.SpinOnce();
#endif
            }
        }
[ MethodImpl ( ( MethodImplOptions ) 256 ) ] public static T Swap < T , A > ( ref T value , A a , Func < T , A , T > getNewValue , int retryCountUntilThrow = RETRY_COUNT_UNTIL_THROW ) where T : class { if SUPPORTS_SPIN_WAIT var spinWait = new SpinWait ( ) ; endif var retryCount = 0 ; while ( true ) { var oldValue = value ; var newValue = getNewValue ( oldValue , a ) ; if ( Interlocked . CompareExchange ( ref value , newValue , oldValue ) == oldValue ) return oldValue ; if ( ++ retryCount > retryCountUntilThrow ) ThrowRetryCountExceeded ( retryCountUntilThrow ) ; if SUPPORTS_SPIN_WAIT spinWait . SpinOnce ( ) ; endif } }
Option without allocation for capturing `a` in closure of `getNewValue`
Option without allocation for capturing ` a ` in closure of ` getNewValue `
T
ref
a
A
getNewValue
Func<T, A, T>
retryCountUntilThrow
int