简体   繁体   English

调用时将内联Thread.SpinWait吗?

[英]Will Thread.SpinWait be inlined when called?

I have following code: 我有以下代码:

while(flag)
{
  Thread.SpinWait(1);
}

following is implementation of SpinWait in Rotor(sscli20\\clr\\src\\vm\\comsynchronizable.cpp) 以下是在Rotor(sscli20\\clr\\src\\vm\\comsynchronizable.cpp)实现SpinWait Rotor(sscli20\\clr\\src\\vm\\comsynchronizable.cpp)

FCIMPL1(void, ThreadNative::SpinWait, int iterations)
{
    WRAPPER_CONTRACT;
    STATIC_CONTRACT_SO_TOLERANT;

    for(int i = 0; i < iterations; i++)
        YieldProcessor();
}
FCIMPLEND

Will Thread.SpinWait be inlined when called? 调用时将内联Thread.SpinWait吗?

if not, in each loop cycle, it will spend more time on stack operations(push and pop) and consume more execution resource of CPU. 如果不是这样,则在每个循环周期中,它将在stack operations(push and pop)上花费更多的时间,并消耗更多的CPU执行资源。

if yes, how does clr accomplish that while ThreadNative::SpinWait is implemented as standard function instruction sequence including stack operations(push and pop)? 如果是,那么当ThreadNative::SpinWait被实现为标准函数指令序列(包括堆栈操作(push和pop))时, clr如何实现?

By testing of Eren, no inline occurs in debug mode. 通过测试Eren,在调试模式下不会发生任何内联。 Is it possible to clr optimize and produce inlined code? 是否有可能进行clr优化并生成内联代码?

Summary : thanks for your answer. 摘要 :谢谢您的回答。 I wish one day clr can inline pre-compiled code by one mechanism such as MethodImplOptions.InternalCall. 我希望有一天clr可以通过诸如MethodImplOptions.InternalCall之类的一种机制内联预编译的代码。 Then it can eliminate stack operations and spend most time on check of flag and spinning-wait(consuming less cpu resource than nop). 然后,它可以消除堆栈操作,并花费大量时间检查标志和旋转等待(比nop消耗更少的cpu资源)。

Best to try and see. 最好尝试看看。 Sample code: 样例代码:

static void Main(string[] args)
{
    while (true) 
        Thread.SpinWait(1);
} 

The optimized disassembly shows: 优化的拆卸显示:

x86: 86:

00000000  push        ebp 
00000001  mov         ebp,esp 
00000003  mov         ecx,1 
00000008  call        6F11D3FE 
0000000d  jmp         00000003 

x64: 64位:

00000000  sub         rsp,28h 
00000004  mov         ecx,1 
00000009  call        000000005F815434 
0000000e  jmp         0000000000000004 
00000010  add         rsp,28h 
00000014  ret 

So there is no inlining in either case. 因此,在任何情况下都没有内联

Maybe I'm missing something but I don't quite understand why you care about the stack operations as spinning the CPU consumes cycles anyway (the whole purpose is to not yield). 也许我缺少了一些东西,但是我不太明白为什么您关心堆栈操作,因为旋转CPU总是消耗周期(整个目的是屈服)。

No, the jitter is not capable of inlining pre-compiled C++ code, only managed code that started as IL. 不,抖动不能内联预编译的C ++代码,只能内嵌以IL开头的托管代码。

This is entirely irrelevant for a SpinWait() call. 这与SpinWait()调用完全无关。 The point of spin-waiting is to have the processor execute code rather then paying the cost of a thread-context switch. 等待旋转的目的是让处理器执行代码,而不是支付线程上下文切换的成本。 With the expectation that flag will turn false in 10,000 cpu cycles or less. 期望该标志将在10,000 cpu或更短的时间内变为false It doesn't matter what kind of code. 什么样的代码都没有关系。 CALL is a fine way to execute code. CALL是执行代码的一种好方法。

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

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