Пример #1
0
        /* This sample illustrates lexical scope vs lifetime (thanks to JIT's eager root collection).
         * Both local and sc are collected much earlier than their lexical scope.
         * !u -gcinfo 00007ff81c4c8598
         * Normal JIT generated code
         * CoreCLR.CollectScenarios.Scenarios.EagerRootCollection.LexicalScopeExample(Int32)
         * Begin 00007ff81c5e3310, size 71
         * 00007ff8`1c5e3310 57              push    rdi
         * 00007ff8`1c5e3311 56              push    rsi
         * 00007ff8`1c5e3312 4883ec28        sub     rsp,28h
         * 00007ff8`1c5e3316 8bf2            mov     esi,edx
         * 00007ff8`1c5e3318 48b908ad691cf87f0000 mov rcx,7FF81C69AD08h (MT: CoreCLR.CollectScenarios.Scenarios.EagerRootCollection+BigClass)
         * 00007ff8`1c5e3322 e8596af65e      call    CoreCLR!JIT_New (00007ff8`7b549d80)
         * 00000017 is a safepoint:
         * 00007ff8`1c5e3327 488bf8          mov     rdi,rax
         * 00007ff8`1c5e332a 488bcf          mov     rcx,rdi
         * 00007ff8`1c5e332d e8be5d0b5e      call    System_Private_CoreLib+0xc890f0 (00007ff8`7a6990f0) (System.Object..ctor(), mdToken: 0000000006000117)
         * 00000022 is a safepoint:
         * 00000021 +rdi
         * 00007ff8`1c5e3332 897708          mov     dword ptr [rdi+8],esi
         * 00007ff8`1c5e3335 488bcf          mov     rcx,rdi
         * 00007ff8`1c5e3338 e87bf8ffff      call    00007ff8`1c5e2bb8 (CoreCLR.CollectScenarios.Scenarios.EagerRootCollection+BigClass.Check(), mdToken: 000000000600005a)
         * 0000002d is a safepoint:
         * 00007ff8`1c5e333d 85c0            test    eax,eax
         * 00007ff8`1c5e333f 7437            je      00007ff8`1c5e3378
         * 00007ff8`1c5e3341 48b9e8af691cf87f0000 mov rcx,7FF81C69AFE8h (MT: CoreCLR.CollectScenarios.Scenarios.EagerRootCollection+SomeClass)
         * 00007ff8`1c5e334b e8b0e14b5f      call    CoreCLR!JIT_TrialAllocSFastMP_InlineGetThread (00007ff8`7baa1500)
         * 00000040 is a safepoint:
         * 00007ff8`1c5e3350 488bf8          mov     rdi,rax
         * 00007ff8`1c5e3353 488bcf          mov     rcx,rdi
         * 00007ff8`1c5e3356 e8955d0b5e      call    System_Private_CoreLib+0xc890f0 (00007ff8`7a6990f0) (System.Object..ctor(), mdToken: 0000000006000117)
         * 0000004b is a safepoint:
         * 0000004a +rdi
         * 00007ff8`1c5e335b 488bcf          mov     rcx,rdi
         * 00007ff8`1c5e335e 8bd6            mov     edx,esi
         * 00007ff8`1c5e3360 e87bf8ffff      call    00007ff8`1c5e2be0 (CoreCLR.CollectScenarios.Scenarios.EagerRootCollection+SomeClass.CalculateSomething(Int32), mdToken: 0000000006000054)
         * 00000055 is a safepoint:
         * 00007ff8`1c5e3365 8bc8            mov     ecx,eax
         * 00007ff8`1c5e3367 e8047c195e      call    System_Private_CoreLib+0xd6af70 (00007ff8`7a77af70) (Internal.Runtime.Augments.RuntimeThread.Sleep(Int32), mdToken: 00000000060000e7)
         * 0000005c is a safepoint:
         * 00007ff8`1c5e336c b801000000      mov     eax,1
         * 00007ff8`1c5e3371 4883c428        add     rsp,28h
         * 00007ff8`1c5e3375 5e              pop     rsi
         * 00007ff8`1c5e3376 5f              pop     rdi
         * 00007ff8`1c5e3377 c3              ret
         * 00007ff8`1c5e3378 33c0            xor     eax,eax
         * 00007ff8`1c5e337a 4883c428        add     rsp,28h
         * 00007ff8`1c5e337e 5e              pop     rsi
         * 00007ff8`1c5e337f 5f              pop     rdi
         * 00007ff8`1c5e3380 c3              ret
         */

        ///////////////////////////////////////////////////////////////////////
        // Listing 8-5
        private int LexicalScopeExample(int value)
        {
            BigClass local = new BigClass()
            {
                Field = value
            };

            if (local.Check())
            {
                SomeClass sc   = new SomeClass();
                int       data = sc.CalculateSomething(value);
                Thread.Sleep(data); // or DoSomeLongRunningCall(data);
                return(1);
            }
            return(0);
        }