繁体   English   中英

C#代码优化导致Interlocked.Exchange()出现问题

[英]C# Code optimization causes problems with Interlocked.Exchange()

我有一些代码令人沮丧的问题,不知道为什么会出现这个问题。

//
// .NET FRAMEWORK v4.6.2 Console App

static void Main( string[] args )
{
    var list = new List<string>{ "aa", "bbb", "cccccc", "dddddddd", "eeeeeeeeeeeeeeee", "fffff", "gg" };

    foreach( var item in list )
    {
        Progress( item );
    }
}

private static int _cursorLeft = -1;
private static int _cursorTop = -1;
public static void Progress( string value = null )
{
    lock( Console.Out )
    {
        if( !string.IsNullOrEmpty( value ) )
        {
            Console.Write( value );
            var left = Console.CursorLeft;
            var top = Console.CursorTop;
            Interlocked.Exchange( ref _cursorLeft, Console.CursorLeft );
            Interlocked.Exchange( ref _cursorTop, Console.CursorTop );
            Console.WriteLine();
            Console.WriteLine( "Left: {0} _ {1}", _cursorLeft, left );
            Console.WriteLine( "Top: {0} _ {1}", _cursorTop, top );
        }
    }
}

没有 代码优化的 情况下运行时,结果如预期。 _cursorLeft和最 _cursorTop顶部都是平等的。

aa
Left: 2 _ 2
Top: 0 _ 0
bbb
Left: 3 _ 3
Top: 3 _ 3

但是当我使用 代码优化运行它时两个值_cursorLeft_cursorTop变成了bizzare:

aa
Left: -65534 _ 2
Top: -65536 _ 0
bb
Left: -65533 _ 3
Top: -65533 _ 3

我找到了2个解决方法:

  1. _cursorLeft_cursorTop设置为0而不是-1
  2. 让Interlocked.Exchange取左边的值。 最佳

由于解决方法#1与我的需求不符,我最终得到了解决方法#2:

private static int _cursorLeft = -1;
private static int _cursorTop = -1;
public static void Progress( string value = null )
{
    lock( Console.Out )
    {
        if( !string.IsNullOrEmpty( value ) )
        {
            Console.Write( value );

            // OLD - does NOT work!
            //Interlocked.Exchange( ref _cursorLeft, Console.CursorLeft );
            //Interlocked.Exchange( ref _cursorTop, Console.CursorTop );

            // NEW - works great!
            var left = Console.CursorLeft;
            var top = Console.CursorTop;
            Interlocked.Exchange( ref _cursorLeft, left );  // new
            Interlocked.Exchange( ref _cursorTop, top );  // new
        }
    }
}

但这种奇怪的行为来自哪里?
有没有更好的解决方法/解决方案?


[Matthew Watson编辑:添加简化复制品:]

class Program
{
    static void Main()
    {
        int actual = -1;
        Interlocked.Exchange(ref actual, Test.AlwaysReturnsZero);
        Console.WriteLine("Actual value: {0}, Expected 0", actual);
    }
}

static class Test
{
    static short zero;
    public static int AlwaysReturnsZero => zero;
}

[由我编辑:]
我想出了另一个更短的例子:

class Program
{
    private static int _intToExchange = -1;
    private static short _innerShort = 2;

    // [MethodImpl(MethodImplOptions.NoOptimization)]
    static void Main( string[] args )
    {
        var oldValue = Interlocked.Exchange(ref _intToExchange, _innerShort);
        Console.WriteLine( "It was:   {0}", oldValue );
        Console.WriteLine( "It is:    {0}", _intToExchange );
        Console.WriteLine( "Expected: {0}", _innerShort );
    }
}

除非您不使用优化或将_intToExchange设置为ushort范围内的值,否则您将无法识别该问题。

您正确诊断了问题,这是一个优化器错误。 它特定于64位抖动(又名RyuJIT),这是首次开始在VS2015中发布的抖动。 您只能通过查看生成的机器代码来查看它。 在我的机器上看起来像这样:

00000135  movsx       rcx,word ptr [rbp-7Ch]       ; Cursor.Left
0000013a  mov         r8,7FF9B92D4754h             ; ref _cursorLeft
00000144  xchg        cx,word ptr [r8]             ; Interlocked.Exchange

XCHG指令错误,它使用16位操作数(cx和word ptr)。 但是变量类型需要32位操作数。 因此,变量的高16位保持在0xffff,使整个值为负。

表征这个bug有点棘手,隔离并不容易。 获取内联的Cursor.Left属性getter似乎有助于触发错误,在它访问16位字段的引擎盖下。 显然,以某种方式使优化器决定16位交换将完成工作。 并且您的变通方法代码解决它的原因是,使用32位变量来存储Cursor.Left / Top属性会使优化器陷入良好的代码路径。

这种情况下的解决方法非常简单,除了您找到的解决方法之外,您根本不需要Interlocked,因为lock语句已经使代码成为线程安全的。 请在connect.microsoft.com上报告错误,如果您不想花时间让我知道,我会照顾它。

我没有确切的解释,但仍想分享我的发现。 它似乎是与Interlocked.Exchange结合使用的x64抖动内联中的一个错误,它在本机代码中实现。 这是一个重现的简短版本,不使用Console类。

class Program {
    private static int _intToExchange = -1;

    static void Main(string[] args) {
        _innerShort = 2;
        var left = GetShortAsInt();
        var oldLeft = Interlocked.Exchange(ref _intToExchange, GetShortAsInt());
        Console.WriteLine("Left: new {0} current {1} old {2}", _intToExchange, left, oldLeft);
        Console.ReadKey();
    }

    private static short _innerShort;
    static int GetShortAsInt() => _innerShort;
}

所以我们有一个int字段和一个返回int的方法,但实际上返回'short'(就像Console.LeftCursor一样)。 如果我们在发布模式下使用优化AND和x64编译它,它将输出:

new -65534 current 2 old 65535

会发生什么事情是抖动内联GetShortAsInt但这样做不正确。 我不确定为什么出了问题。 编辑:正如Hans在他的回答中指出的那样 - 优化器在这种情况下使用不正确的xchg instuction来执行交换。

如果你这样改变:

[MethodImpl(MethodImplOptions.NoInlining)]
static int GetShortAsInt() => _innerShort;

它将按预期工作:

new 2 current 2 old -1

对于非负值,它似乎在第一个站点工作,但实际上没有 - 当_intToExchange超过ushort.MaxValue - 它再次中断:

private static int _intToExchange = ushort.MaxValue + 2;
new 65538 current 2 old 1

所以这一切 - 你的解决方法看起来很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM