Microsoft MVP성태의 닷넷 이야기
.NET Framework: 718. AsyncTaskMethodBuilder.Create() 메서드 동작 방식 [링크 복사], [링크+제목 복사],
조회: 19231
글쓴 사람
정성태 (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일 수 없습니다.
정성태

1  2  3  4  5  6  7  8  [9]  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13718정성태8/27/20247442오류 유형: 921. Visual C++ - error C1083: Cannot open include file: 'float.h': No such file or directory [2]
13717정성태8/26/20247037VS.NET IDE: 192. Visual Studio 2022 - Windows XP / 2003용 C/C++ 프로젝트 빌드
13716정성태8/21/20246767C/C++: 167. Visual C++ - 윈도우 환경에서 _execv 동작 [1]
13715정성태8/19/20247386Linux: 78. 리눅스 C/C++ - 특정 버전의 glibc 빌드 (docker-glibc-builder)
13714정성태8/19/20246766닷넷: 2295. C# 12 - 기본 생성자(Primary constructors) (책 오타 수정) [3]
13713정성태8/16/20247494개발 환경 구성: 721. WSL 2에서의 Hyper-V Socket 연동
13712정성태8/14/20247228개발 환경 구성: 720. Synology NAS - docker 원격 제어를 위한 TCP 바인딩 추가
13711정성태8/13/20248076Linux: 77. C# / Linux - zombie process (defunct process) [1]파일 다운로드1
13710정성태8/8/20248007닷넷: 2294. C# 13 - (6) iterator 또는 비동기 메서드에서 ref와 unsafe 사용을 부분적으로 허용파일 다운로드1
13709정성태8/7/20247766닷넷: 2293. C# - safe/unsafe 문맥에 대한 C# 13의 (하위 호환을 깨는) 변화파일 다운로드1
13708정성태8/7/20247559개발 환경 구성: 719. ffmpeg / YoutubeExplode - mp4 동영상 파일로부터 Audio 파일 추출
13707정성태8/6/20247792닷넷: 2292. C# - 자식 프로세스의 출력이 4,096보다 많은 경우 Process.WaitForExit 호출 시 hang 현상파일 다운로드1
13706정성태8/5/20247899개발 환경 구성: 718. Hyper-V - 리눅스 VM에 새로운 디스크 추가
13705정성태8/4/20248170닷넷: 2291. C# 13 - (5) params 인자 타입으로 컬렉션 허용 [2]파일 다운로드1
13704정성태8/2/20248126닷넷: 2290. C# - 간이 dotnet-dump 프로그램 만들기파일 다운로드1
13703정성태8/1/20247451닷넷: 2289. "dotnet-dump ps" 명령어가 닷넷 프로세스를 찾는 방법
13702정성태7/31/20247861닷넷: 2288. Collection 식을 지원하는 사용자 정의 타입을 CollectionBuilder 특성으로 성능 보완파일 다운로드1
13701정성태7/30/20248131닷넷: 2287. C# 13 - (4) Indexer를 이용한 개체 초기화 구문에서 System.Index 연산자 허용파일 다운로드1
13700정성태7/29/20247759디버깅 기술: 200. DLL Export/Import의 Hint 의미
13699정성태7/27/20248251닷넷: 2286. C# 13 - (3) Monitor를 대체할 Lock 타입파일 다운로드1
13698정성태7/27/20248213닷넷: 2285. C# - async 메서드에서의 System.Threading.Lock 잠금 처리파일 다운로드1
13697정성태7/26/20247934닷넷: 2284. C# - async 메서드에서의 lock/Monitor.Enter/Exit 잠금 처리파일 다운로드1
13696정성태7/26/20247469오류 유형: 920. dotnet publish - error NETSDK1047: Assets file '...\obj\project.assets.json' doesn't have a target for '...'
13695정성태7/25/20247456닷넷: 2283. C# - Lock / Wait 상태에서도 STA COM 메서드 호출 처리파일 다운로드1
13694정성태7/25/20247920닷넷: 2282. C# - ASP.NET Core Web App의 Request 용량 상한값 (Kestrel, IIS)
13693정성태7/24/20247245개발 환경 구성: 717. Visual Studio - C# 프로젝트에서 레지스트리에 등록하지 않은 COM 개체 참조 및 사용 방법파일 다운로드1
1  2  3  4  5  6  7  8  [9]  10  11  12  13  14  15  ...