Microsoft MVP성태의 닷넷 이야기
.NET Framework: 718. AsyncTaskMethodBuilder.Create() 메서드 동작 방식 [링크 복사], [링크+제목 복사],
조회: 19413
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 2개 있습니다.)

AsyncTaskMethodBuilder.Create() 메서드 동작 방식

일반 메서드에 async 예약어가 붙게 되면 다음과 같은 상태 머신 객체로 메서드의 구현이 바뀝니다.

[AsyncStateMachine(typeof(TaskDelay_StateMachine)), DebuggerStepThrough]
private Task TaskDelay()
{
    TaskDelay_StateMachine stateMachine = new TaskDelay_StateMachine {
        _this = this,
        _builder = AsyncTaskMethodBuilder.Create(),
        _state = -1
    };
    stateMachine._builder.Start<TaskDelay_StateMachine>(ref stateMachine);
    return stateMachine._builder.Task;
}

그중에서 AsyncTaskMethodBuilder.Create 메서드가 어떤 일을 하는지 궁금했습니다. Reflector 도구로 보면 다음과 같이 단순히 기본 생성자를 호출하는 것에 불과합니다.

[__DynamicallyInvokable]
public static AsyncTaskMethodBuilder Create()
{
    return new AsyncTaskMethodBuilder();
}

일단, public static 메서드이기 때문에 외부에서도 마음 편하게 직접 실행해 볼 수 있습니다.

AsyncTaskMethodBuilder asyncBuilder = AsyncTaskMethodBuilder.Create();

그리고 asyncBuilder 인스턴스를 Visual Studio의 디버거 watch 창으로 보면 다음과 같은 출력을 볼 수 있습니다.

asyncBuilder
+       Task    Id = 1, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
+       ObjectIdForDebugger Id = 1, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
-       m_builder   {System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.Threading.Tasks.VoidTaskResult>}
+		    Task	Id = 1, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
+		    m_coreState    {System.Runtime.CompilerServices.AsyncMethodBuilderCore}


위의 출력에서 기본 생성자만 호출했음에도 불구하고 AsyncTaskMethodBuilder 객체의 내부 변수 m_builder는 값이 채워져 있다는 점입니다. 이게 어떻게 가능할까요? ^^

왜냐하면 m_builder 필드의 타입인 AsyncTaskMethodBuilder<System.Threading.Tasks.VoidTaskResult>가 struct 타입이기 때문입니다. 마찬가지로 m_coreState의 객체도 null이 아닐 수 있는 것이 AsyncMethodBuilderCore 타입이 struct이기 때문입니다.

따라서, AsyncTaskMethodBuilder.Create() 메서드 호출 하나로 생성되는 객체는 AsyncTaskMethodBuilder, AsyncTaskMethodBuilder<VoidTaskResult>, AsyncMethodBuilderCore가 됩니다.

그런데, 여기서 asyncBuilder의 Task 속성도 값을 보여주고 있습니다.

Id = 1, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

왜냐하면, Task 속성은 다음과 같이 값이 없으면 기본 Task 객체를 생성해서 반환하기 때문입니다.

[__DynamicallyInvokable]
public Task<TResult> Task
{
    [__DynamicallyInvokable]
    get
    {
        Task<TResult> task = this.m_task;
        if (task == null)
        {
            this.m_task = task = new Task<TResult>();
        }
        return task;
    }
}

그런데, 이렇게 생성된 m_task의 디버거 출력값이 왜 Id = 1, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"로 되는 걸까요?

m_task의 타입인 Task<TResult>는 ToString에 대한 재정의도 없습니다. 대신 디버거가 값을 출력할 수 있도록 다음과 같은 특성을 달고 있습니다.

[...[생략]..., DebuggerDisplay("Id = {Id}, Status = {Status}, Method = {DebuggerDisplayMethodDescription}, Result = {DebuggerDisplayResultDescription}"), ...[생략]...)]
public class Task : Task
{
    // ...[생략]...
}

즉, Id, Status, Method, DebuggerDisplayMethodDescription, DebuggerDisplayResultDescription 값을 보여주게 됩니다.

Id 속성도 접근할 때 없으면 생성하는 방식이라,

[__DynamicallyInvokable]
public int get_Id()
{
    if (this.m_taskId == 0)
    {
        int num = NewId();
        Interlocked.CompareExchange(ref this.m_taskId, num, 0);
    }
    return this.m_taskId;
}

internal static int NewId()
{
    int taskID = 0;
    do
    {
        taskID = Interlocked.Increment(ref s_taskIdCounter);
    }
    while (taskID == 0);
    TplEtwProvider.Log.NewID(taskID);
    return taskID;
}

프로그램 시작 후 최초 0이었던 s_taskIdCounter 값이 1씩 증가하는 값을 반환합니다.

Status는 m_stateFlags의 값에 따라 TaskStatus 열 값을 반환합니다.

[__DynamicallyInvokable]
public TaskStatus get_Status()
{
    int stateFlags = this.m_stateFlags;
    if ((stateFlags & 0x200000) != 0)
    {
        return TaskStatus.Faulted;
    }
    if ((stateFlags & 0x400000) != 0)
    {
        return TaskStatus.Canceled;
    }
    if ((stateFlags & 0x1000000) != 0)
    {
        return TaskStatus.RanToCompletion;
    }
    if ((stateFlags & 0x800000) != 0)
    {
        return TaskStatus.WaitingForChildrenToComplete;
    }
    if ((stateFlags & 0x20000) != 0)
    {
        return TaskStatus.Running;
    }
    if ((stateFlags & 0x10000) != 0)
    {
        return TaskStatus.WaitingToRun;
    }
    if ((stateFlags & 0x2000000) != 0)
    {
        return TaskStatus.WaitingForActivation;
    }
    return TaskStatus.Created;
}

디버거에 보이는 Status 값이 WaitingForActivation이므로 m_stateFlags 값에 0x2000000로 설정되어 있음을 추측하게 됩니다. 이 값은 Task 생성자에서 초기화한 값입니다.

// System.Threading.Tasks.Task
internal Task()
{
    this.m_stateFlags = 0x2000400;
}

다음으로 DebuggerDisplayMethodDescription은,

private string DebuggerDisplayMethodDescription
{
    get
    {
        Delegate action = (Delegate) this.m_action;
        if (action == null)
        {
            return "{null}";
        }
        return action.Method.ToString();
    }
}

기본 생성자를 거친 객체이므로 m_action이 null이라 "{null}"로 출력된 것이 맞습니다. 마지막으로 Result는 DebuggerDisplayResultDescription 속성과 연결되어 있는데,

private string DebuggerDisplayResultDescription
{
    get
    {
        if (!base.IsRanToCompletion)
        {
            return Environment.GetResourceString("TaskT_DebuggerNoResult");
        }
        return (this.m_result);
    }
}

internal bool IsRanToCompletion
{
    get
    {
        return ((this.m_stateFlags & 0x1600000) == 0x1000000);
    }
}

IsRanToCompletion == false이므로 Environment.GetResourceString("TaskT_DebuggerNoResult"); 코드를 실행하게 되고, 이는 mscorlib.dll의 mscorlib.resources 리소스에 담긴 "TaskT_DebuggerNoResult" Name의 값이 "{Not yet computed}"이기 때문에 출력된 것입니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 12/22/2017]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2023-09-11 09시50분
"m_builder, m_coreState는 struct 타입이기 때문에 null이 아닙니다."라는 문장이 확인하기 위해서
(선생님을 의심하는 것이 아닙니다 ^^...)아래 간단한 예제를 구현했습니다.
// Location.cs
public class Location
{
    public int x;
    public int y;
}

// Person.cs
public class Person
{
    public string name;
    public int age;
    public Location location;    
}

// Program.cs
static void Main(string[] args)
{
    // Location이 class인 경우 Person 객체를 생성하면 location = null이고
    // Location이 struct인 경우 location = {ConsoleApp1.Location}가 된다.
    Person mijung = new Person();
}
궁금한 점이 Location이 class라면 null이고
struct라면 null이 아니라 객체를 생성해서 할당하도록 C# 언어를 설계한 이유가 있을까요?
선생님 교재 219 ~ 226쪽을 참고해서 추론하면
구조체는 스택에 할당되고 클래스는 힙에 할당되는데
힙은 가비지 컬렉션에 의해 관리되기 때문에
사용하지 않는 객체는 수거되므로
미리 생성하는 것이 의미가 없다고 생각해서 class는 null이라고 생각하는데 너무 단편적으로 생각했을까요?
한예지
2023-09-11 10시46분
해당 질문은 결국 "참조 형식" 이외에 왜 "값 형식"을 만들었느냐가 됩니다.

사실, C# 언어를 그렇게 안 만들어도 됐을 것입니다. 실제로 Java의 경우에는 언어 차원에서 "값 형식"을 만들 수 있는 구문을 제공하지 않습니다. 굳이 값 형식을 만든 주된 이유는, 가능한 "GC"의 부담을 줄이는데 있습니다. 스택을 쓸 수 있는 수단이 생긴 것이므로 상황에 따라 GC의 구동을 지연시키는 것이 가능할 수 있습니다.

참고로, 말씀하신 예제에서는 "class Person" 내에 "struct Location"이 있다면, 그런 경우 Location 정보는 스택에 할당되지 않고 Person의 다른 필드와 함께 보관이 됩니다. 이에 대해서는 다음의 글을 참고하세요.

C# - struct/class가 스택/힙에 할당되는 사례 정리
; https://www.sysnet.pe.kr/2/0/12624

------------------------------

그리고 할 수만 있었다면 값 형식도 참조 형식처럼 null 상태를 갖고 싶었을 것입니다. 하지만, 스택은 함수의 실행 순간에 사용할 공간이 정해져야 하므로 null 상태를 갖기보다는 해당 타입의 크기만큼 미리 점유해 놓을 수밖에 없고, 이로 인해 자연스럽게 null일 수 없습니다.
정성태

... 106  107  108  109  110  111  112  113  114  115  116  117  [118]  119  120  ...
NoWriterDateCnt.TitleFile(s)
10975정성태5/20/201623234Math: 17. C# - 복소수 타입의 승수를 지원하는 Power 메서드파일 다운로드1
10974정성태5/20/201623755.NET Framework: 588. C# - OxyPlot 라이브러리로 복소수 표현파일 다운로드1
10973정성태5/20/201628773.NET Framework: 587. C# Plotting 라이브러리 OxyPlot [3]파일 다운로드1
10972정성태5/19/201627888Math: 16. C# - 갈루아 필드 GF(2) 연산 [3]파일 다운로드1
10971정성태5/19/201620689오류 유형: 334. Visual Studio - 빌드 시 경고 warning MSB3884: Could not find rule set file "...". [2]
10970정성태5/19/201625050오류 유형: 333. OxyPlot 라이브러리의 컨트롤을 Toolbox에 등록 시 오류 [2]
10969정성태5/18/201624337.NET Framework: 586. C# - 파일 확장자에 연결된 프로그램을 등록하는 방법 (3) - "Open with" 목록에 등록파일 다운로드1
10968정성태5/18/201619354오류 유형: 332. Visual Studio - 단위 테스트 생성 시 "Design time expression evaluation" 오류 메시지
10967정성태5/12/201624431.NET Framework: 585. C# - 파일 확장자에 연결된 프로그램을 등록하는 방법 (2) - 웹 브라우저가 다운로드 후 자동 실행
10966정성태5/12/201632077.NET Framework: 584. C# - 파일 확장자에 연결된 프로그램을 등록하는 방법 (1) - 기본 [1]파일 다운로드1
10965정성태5/12/201624091디버깅 기술: 81. try/catch로 조용히 사라진 예외를 파악하고 싶다면?
10964정성태5/12/201622704오류 유형: 331. ASP.NET에서 System.BadImageFormatException 예외가 발생하는 경우
10963정성태5/11/201624968VS.NET IDE: 107. Visual Studio 2015의 "DTAR_..." 특수 폴더가 생성되는 문제파일 다운로드2
10962정성태5/11/201624995오류 유형: 330. Visual Studio 단위 테스트 시 DisconnectedContext 예외 발생
10961정성태5/11/201624855.NET Framework: 583. 문제 재현 - Managed Debugging Assistant 'DisconnectedContext' has detected a problem in '...'파일 다운로드1
10960정성태5/10/201622329오류 유형: 329. ATL 메서드 추가 마법사 창에서 8ce0000b 오류 발생
10959정성태5/9/201624883.NET Framework: 582. CLR Profiler - 별도 정의한 .NET 코드를 호출하도록 IL 코드 변경파일 다운로드1
10958정성태5/6/201651903개발 환경 구성: 284. "Let's Encrypt"에서 제공하는 무료 SSL 인증서를 IIS에 적용하는 방법 (1) [3]
10957정성태5/3/201627214오류 유형: 328. 윈도우 백업 시 오류 - 0x80780166 두 번째 이야기 [1]
10956정성태5/3/201622746Windows: 117. BitLocker - This device can't use a Trusted Platform Module.
10955정성태5/3/201629419.NET Framework: 581. C# - 순열(Permutation) 예제 코드파일 다운로드2
10954정성태5/3/201630379.NET Framework: 580. C# - 조합(Combination) 예제 코드 [2]파일 다운로드1
10953정성태5/2/201619884.NET Framework: 579. Assembly.LoadFrom으로 로드된 어셈블리의 JIT 컴파일 코드 공유?파일 다운로드1
10952정성태5/2/201622064.NET Framework: 578. 도메인 중립적인 어셈블리가 비-도메인 중립적인 어셈블리를 참조하는 경우파일 다운로드1
10951정성태5/2/201619954.NET Framework: 577. CLR Profiler로 살펴보는 SharedDomain의 모듈 로드 동작파일 다운로드1
10950정성태5/2/201626359.NET Framework: 576. 기본적인 CLR Profiler 소스 코드 설명 [2]파일 다운로드2
... 106  107  108  109  110  111  112  113  114  115  116  117  [118]  119  120  ...