Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 6개 있습니다.)
(시리즈 글이 12개 있습니다.)
.NET Framework: 698. C# 컴파일러 대신 직접 구현하는 비동기(async/await) 코드
; https://www.sysnet.pe.kr/2/0/11351

.NET Framework: 716. async 메서드의 void 반환 타입 사용에 대하여
; https://www.sysnet.pe.kr/2/0/11414

.NET Framework: 717. Task를 포함하지 않는 async 메서드의 동작 방식
; https://www.sysnet.pe.kr/2/0/11415

.NET Framework: 719. Task를 포함하는 async 메서드의 동작 방식
; https://www.sysnet.pe.kr/2/0/11417

.NET Framework: 731. C# - await을 Task 타입이 아닌 사용자 정의 타입에 적용하는 방법
; https://www.sysnet.pe.kr/2/0/11456

.NET Framework: 737. C# - async를 Task 타입이 아닌 사용자 정의 타입에 적용하는 방법
; https://www.sysnet.pe.kr/2/0/11484

.NET Framework: 813. C# async 메서드에서 out/ref/in 유형의 인자를 사용하지 못하는 이유
; https://www.sysnet.pe.kr/2/0/11850

닷넷: 2138. C# - async 메서드 호출 원칙
; https://www.sysnet.pe.kr/2/0/13405

닷넷: 2147. C# - 비동기 메서드의 async 예약어 유무에 따른 차이
; https://www.sysnet.pe.kr/2/0/13421

닷넷: 2318. C# - (async Task가 아닌) async void 사용 시의 부작용
; https://www.sysnet.pe.kr/2/0/13884

닷넷: 2319. ASP.NET Core Web API / Razor 페이지에서 발생할 수 있는 async void 메서드의 부작용
; https://www.sysnet.pe.kr/2/0/13885

닷넷: 2321. Blazor에서 발생할 수 있는 async void 메서드의 부작용
; https://www.sysnet.pe.kr/2/0/13888




C# 컴파일러 대신 직접 구현하는 비동기(async/await) 코드

C# async/await 코드가 참 마법 같습니다. 단지 예약어 하나 썼을 뿐인데 어떻게 그걸 비동기로 처리해 주는지 신기할 따름인데요. 그저 그 신기함을 누리며 사용하는 것도 좋겠지만, 왠지 C# 컴파일러가 추상화한 부분을 걷어내고 싶어졌습니다. 혹시나 C# async/await 내부 동작을 이해하고 싶은 분들이 계시다면 이 글이 도움이 될 듯합니다. (또는, 수많은 소스코드 파일을 빌드하느라 힘에 겨운 C# 컴파일러의 일을 덜어주고 싶은, 착한 마음씨를 가진 개발자분들을 위해서도! ^^)




우선 다음과 같은 예제 코드를 보겠습니다.

using System;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        // C# 7.1 async Main
        static async Task Main(string[] args)
        {
            Program pg = new Program();

            await pg.CallAsync();
        }

        private async Task CallAsync()
        {
            string title = DateTime.Now.ToString();
            string text = await GetFileContents();
            Console.WriteLine(title + ": " + text);
        }

        private async Task<string> GetFileContents()
        {
            return await new TaskFactory().StartNew(() => { return "test"; });
        }
    }
}

보는 바와 같이 async/await을 사용한 전형적인 비동기 함수 호출인데요. 실행하면 다음과 같은 식의 결과가 출력됩니다.

2017-11-07 오후 9:05:15: test

자, 그럼 이 부분에서 GetFileContents 비동기 함수를 C# 컴파일러가 아닌 우리가 직접 비동기 처리로 바꿔보겠습니다. 방법은 사실 매우 쉽습니다. .NET Reflector와 같은 역어셈블러를 이용해 GetFileContents를 어떻게 바꿨는지 살펴보면 됩니다. 다음은 실제로 C# 컴파일러가 비동기 처리를 위해 만든 내부 클래스입니다.

[CompilerGenerated]
private sealed class <GetFileContents>d__2 : IAsyncStateMachine
{
    // Fields
    public int <>1__state;
    public Program <>4__this;
    private string <>s__3;
    public AsyncTaskMethodBuilder<string> <>t__builder;
    private TaskAwaiter<string> <>u__1;
    private Task<string> <getStringTask>5__1;
    private string <urlContents>5__2;

    // Methods
    private void MoveNext()
    {
        string str;
        int num = this.<>1__state;
        try
        {
            TaskAwaiter<string> awaiter;
            if (num != 0)
            {
                this.<getStringTask>5__1 = new TaskFactory().StartNew<string>(Program.<>c.<>9__2_0 ?? (Program.<>c.<>9__2_0 = new Func<string>(Program.<>c.<>9.<GetFileContents>b__2_0)));
                awaiter = this.<getStringTask>5__1.GetAwaiter();
                if (!awaiter.IsCompleted)
                {
                    this.<>1__state = num = 0;
                    this.<>u__1 = awaiter;
                    Program.<GetFileContents>d__2 stateMachine = this;
                    this.<>t__builder.AwaitUnsafeOnCompleted<TaskAwaiter<string>, Program.<GetFileContents>d__2>(ref awaiter, ref stateMachine);
                    return;
                }
            }
            else
            {
                awaiter = this.<>u__1;
                this.<>u__1 = new TaskAwaiter<string>();
                this.<>1__state = num = -1;
            }
            this.<>s__3 = awaiter.GetResult();
            this.<urlContents>5__2 = this.<>s__3;
            this.<>s__3 = null;
            str = this.<urlContents>5__2;
        }
        catch (Exception exception)
        {
            this.<>1__state = -2;
            this.<>t__builder.SetException(exception);
            return;
        }
        this.<>1__state = -2;
        this.<>t__builder.SetResult(str);
    }

    [DebuggerHidden]
    private void SetStateMachine(IAsyncStateMachine stateMachine)
    {
    }
}

위의 소스코드는 지저분하니, 다듬어서 재작성해보겠습니다.

GetFileContents 메서드는 단 한 줄로 작성되어 있지만 다음과 같이 나눠볼 수 있습니다.

private async Task<string> GetFileContents()
{
    Task<string> getStringTask = new TaskFactory().StartNew(() => { return "test"; });

    string urlContents = await getStringTask;
    return urlContents;
}

C# 컴파일러는 위와 같은 async 메서드를 만나면 다음과 같이 2가지 단계로 분할합니다.

[Part A - 현재 스레드에서 실행할 코드]

    Task<string> getStringTask = new TaskFactory().StartNew(() => { return "test"; });

[Part B - 별도의 스레드에서 실행할 코드]

    string urlContents = [getStringTask 작업의 반환값];
    return urlContents;

그리고 저 코드들을 나눠 담을 IAsyncStateMachine 인터페이스를 상속한 별도의 내부 클래스를 정의합니다.

/*

public interface IAsyncStateMachine
{
    void MoveNext();
    void SetStateMachine(IAsyncStateMachine stateMachine);
}

*/

class GetFileContents_StateMachine : IAsyncStateMachine
{
    // ... [생략]...
}

GetFileContents_StateMachine 타입에는 내부 필드를 다음과 같은 식으로 포함하고 있습니다.

// [async 동작을 위한 필드 3개]
public int _state;
public AsyncTaskMethodBuilder _builder;
TaskAwaiter<string> _awaiter;

// [async 메서드를 구현하고 있는 클래스의 this 보관 필드]
public Program _this;

// [async 메서드의 반환값을 임시 보관하는 필드]
string _result;

// [async 메서드의 반환값을 보관하는 필드]
string _urlContents;

// [Part A 코드의 변수들]
Task<string> _getStringTask;

이런 내부 필드 중에서 public 필드의 경우에는 C# 컴파일러가 async 메서드를 일반 메서드로 바꾸는 과정에서 다음과 같은 초기화를 해줍니다.

private Task<string> GetFileContents()
{
    GetFileContents_StateMachine stateMachine = new GetFileContents_StateMachine
    {
        _this = this,
        _builder = AsyncTaskMethodBuilder<string>.Create(),
        _state = -1,
    };

    stateMachine._builder.Start(ref stateMachine);
    return stateMachine._builder.Task as Task<string>;
}

보는 바와 같이 C# 컴파일러는 async 메서드를 일반 메서드로 바꾸면서 내부 동작을 GetFileContents_StateMachine 타입 내에 넣어두고는 stateMachine._builder.Start 메서드를 호출하는 걸로 마무리를 합니다.

여기서 _builder.Start는 비동기 호출이 아닙니다. 현재 스레드에서 시작하는 동기 호출에 불과합니다. AsyncTaskMethodBuilder 타입의 _builder 인스턴스는 Start 메서드 내에서 인자로 들어온 stateMachine의 MoveNext 메서드를 실행하는데, 이 때문에 Part A로 분리한 코드들은 이때 실행이 되도록 MoveNext가 구성되어 있습니다.

void IAsyncStateMachine.MoveNext()
{
    string str;
    int num = this._state;

    try
    {
        TaskAwaiter<string> awaiter;
        // 원래는 if 문이지만 명확한 분리를 위해 switch로 바꿨습니다.
        switch (num)
        {
            case 0:
                // ...[생략]...
                break;

            default:
                this._getStringTask = new TaskFactory().StartNew(() => { return "test"; });
                awaiter = this._getStringTask.GetAwaiter();
                if (awaiter.IsCompleted == false)
                {
                    this._state = num = 0;
                    this._awaiter = awaiter;
                    GetFileContents_StateMachine stateMachine = this;
                    this._builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
                    return;
                }
                break;
        }

        // ...[생략]...
    }
    catch (Exception e)
    {
        // ...[생략]...
        return;
    }

    // ...[생략]...
}

C# 컴파일러가 GetFileContents 메서드를 async에서 일반 메서드로 변경하는 중에 _state 필드를 -1로 초기화했기 때문에 위의 MoveNext 메서드는 동기적으로 default 영역의 코드를 실행하게 됩니다.

보는 바와 같이 Part A로 분리되었던 영역의 코드가 default 영역에 추가되어 있고 await 코드의 대상이었던 _getStringTask에 대해 GetAwaiter()를 호출해 TaskAwaiter를 보관한 다음 해당 Task가 금방 끝나서 IsCompleted == true가 되면 더 이상 동작을 하지 않고 MoveNext를 반환합니다. 즉, 이런 경우에는 단순히 동기 메서드 호출한 것과 다를 바가 없습니다.

반면, 대개의 경우 awaiter.IsCompleted == false로 나오는데, 그럴 때는 다시 _builder.AwaitUnsafeOnCompleted 메서드를 호출해 작업이 완료된 경우의 알림을 등록합니다. 결국 stateMachine 객체의 MoveNext를 다시 호출하도록 만들고는 동기 호출을 마무리합니다. 그리고 이때의 _state 값은 0으로 설정했기 때문에 다음번 MoveNext가 호출될 때는 switch의 case 0 영역의 코드가 실행됩니다.

그래서 Task의 작업이 완료되었을 때 실행되는 MoveNext의 _state == 0으로 실행할 코드에는 C# 컴파일러가 분리한 Part B 영역의 코드를 포함하게 됩니다.

void IAsyncStateMachine.MoveNext()
{
    string str;
    int num = this._state;

    try
    {
        TaskAwaiter<string> awaiter;
        switch (num)
        {
            case 0: // 작업 완료 후 실행되는 코드
                awaiter = this._awaiter;
                this._awaiter = new TaskAwaiter<string>();
                this._state = num = -1;
                break;

            default:
                // ...[생략]...
                return;
        }

        this._result = awaiter.GetResult();
        this._urlContents = this._result; // Part B의 코드
        this._result = null;

        str = this._urlContents;
    }
    catch (Exception e)
    {
        this._state = -2;
        this._builder.SetException(e);
        return;
    }

    this._state = -2;
    this._builder.SetResult(str);
}

이렇게 만들어두고 실행해 보면, C# 컴파일러가 제공했던 async/await 치환 코드와 정확히 동일한 결과를 얻게 됩니다.




하는 김에 CallAsync 메서드도 바꿔 볼까요? ^^

이것도 메서드를 다음과 같이 2부분으로 나눌 수 있습니다.

[Part A - 현재 스레드에서 실행할 코드]

    string title = DateTime.Now.ToString();
    [Task 객체 반환 = ]GetFileContents();

[Part B - 별도의 스레드에서 실행할 코드]

    string text = [GetFileContents 메서드의 반환값];
    Console.WriteLine(title + ": " + text);

마찬가지로 Part A, B 영역의 처리에 필요한 변수와 StateMachine 구현을 위한 필드를 가진 타입을 정의하고,

class CallAsync_StateMachine : IAsyncStateMachine
{
    public int _state;
    public AsyncTaskMethodBuilder _builder;
    TaskAwaiter<string> _awaiter;

    public Program _this;

    string _result;

    // 반환값
    string _text;

    // Part A 코드의 지역 변수
    string _title;

    //...[생략]...
}

마지막으로 Part A, B의 코드를 나눠서 실행할 MoveNext 메서드를 다음과 같이 구현해 주면 끝입니다.

void IAsyncStateMachine.MoveNext()
{
    int num = this._state;

    try
    {
        TaskAwaiter<string> awaiter;
        switch (num)
        {
            case 0: // 작업 완료 후 Task 스레드에서 실행
                awaiter = this._awaiter;
                this._awaiter = new TaskAwaiter<string>();
                this._state = num = -1;
                break;

            default: // 동기적으로 실행될 코드 - Part A 코드를 포함
                this._title = DateTime.Now.ToString();

                awaiter = _this.GetFileContents().GetAwaiter();
                if (awaiter.IsCompleted == false)
                {
                    this._state = num = 0;
                    this._awaiter = awaiter;
                    CallAsync_StateMachine stateMachine = this;
                    this._builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
                    return;
                }
                break;
        }

        // 작업 완료 후 Task 스레드에서 실행 - Part B 코드를 포함
        this._result = awaiter.GetResult();
        this._text = this._result;
        this._result = null;
        Console.WriteLine(this._title + ": " + this._text);
    }
    catch (Exception e)
    {
        this._state = -2;
        this._builder.SetException(e);
        return;
    }

    this._state = -2;
    this._builder.SetResult();
}

이것으로 완벽하게 C# 컴파일러가 대신 만들어 주었던 코드와 일치하므로 실행해 보면 정상적으로 다음과 같은 식의 결과를 볼 수 있습니다.

2017-11-07 오후 9:05:15: test

이렇게 풀어놓고 보니까... C# async/await 코드가 그다지 신기하지 않게 보입니다. ^^

(첨부 파일은 이 글의 모든 예제 코드를 포함합니다.)




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 7/12/2021]

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

비밀번호

댓글 작성자
 



2021-03-12 09시12분
Exploring the async/await State Machine – Concrete Implementation
; https://vkontech.com/exploring-the-async-await-state-machine-concrete-implementation/
정성태
2023-02-24 03시21분
[김기헌] 선생님 잘 읽고 공부하였습니다 감사합니다
[guest]
2023-09-05 08시54분
선생님, 보통 void MoveNext()로 함수 선언하는데
특이하게 void IAsyncStateMachine.MoveNext() 함수 이름 앞에 인터페이스명을 명시했네요..
혹시 자바의 중첩 인터페이스와 비슷한 개념인가요?
한예지
2023-09-05 09시26분
제 책을 가지고 계시니, "4.5.1.4 인터페이스"를 읽어보시면 인터페이스의 메서드를 "public" 접근 제한자 없이 인터페이스 명을 붙이는 유형에 대해 설명합니다. 질문하신 것은 그와 동일한 사례입니다.
정성태
2023-09-06 08시39분
감사합니다!
--------------------------------------------------------------------
인터페이스의 메서드를 자식 클래스에서 구현할 때는
반드시 public 접근 제한자를 명시해야 한다.
아니면 인터페이스명을 직접 붙이는 경우 public 접근 제한자를 생략해도 된다.
[출처] 시작하세요! C# 8.0 프로그래밍[정성태] 205쪽
한예지
2023-09-07 12시11분
너무 자세하게 설명되있는걸 이제 이해했습니다. 감사합니다.
조한빈

... 61  62  63  64  65  66  67  68  [69]  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12211정성태4/27/202019275개발 환경 구성: 486. WSL에서 Makefile로 공개된 리눅스 환경의 C/C++ 소스 코드 빌드
12210정성태4/20/202020720.NET Framework: 903. .NET Framework의 Strong-named 어셈블리 바인딩 (1) - app.config을 이용한 바인딩 리디렉션 [1]파일 다운로드1
12209정성태4/13/202017424오류 유형: 614. 리눅스 환경에서 C/C++ 프로그램이 Segmentation fault 에러가 발생한 경우 (2)
12208정성태4/12/202015994Linux: 29. 리눅스 환경에서 C/C++ 프로그램이 Segmentation fault 에러가 발생한 경우
12207정성태4/2/202015849스크립트: 19. Windows PowerShell의 NonInteractive 모드
12206정성태4/2/202018448오류 유형: 613. 파일 잠금이 바로 안 풀린다면? - The process cannot access the file '...' because it is being used by another process.
12205정성태4/2/202015114스크립트: 18. Powershell에서는 cmd.exe의 명령어를 지원하진 않습니다.
12204정성태4/1/202015129스크립트: 17. Powershell 명령어에 ';' (semi-colon) 문자가 포함된 경우
12203정성태3/18/202017965오류 유형: 612. warning: 'C:\ProgramData/Git/config' has a dubious owner: '...'.
12202정성태3/18/202021214개발 환경 구성: 486. .NET Framework 프로젝트를 위한 GitLab CI/CD Runner 구성
12201정성태3/18/202018454오류 유형: 611. git-credential-manager.exe: Using credentials for username "Personal Access Token". [1]
12200정성태3/18/202018542VS.NET IDE: 145. NuGet + Github 라이브러리 디버깅 관련 옵션 3가지 - "Enable Just My Code" / "Enable Source Link support" / "Suppress JIT optimization on module load (Managed only)"
12199정성태3/17/202016182오류 유형: 610. C# - CodeDomProvider 사용 시 Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path '...\f2_6uod0.tmp'.
12198정성태3/17/202019537오류 유형: 609. SQL 서버 접속 시 "Cannot open user default database. Login failed."
12197정성태3/17/202018837VS.NET IDE: 144. .NET Core 콘솔 응용 프로그램을 배포(publish) 시 docker image 자동 생성 - 두 번째 이야기 [1]
12196정성태3/17/202015961오류 유형: 608. The ServicedComponent being invoked is not correctly configured (Use regsvcs to re-register).
12195정성태3/16/202018277.NET Framework: 902. C# - 프로세스의 모든 핸들을 열람 - 세 번째 이야기
12194정성태3/16/202021004오류 유형: 607. PostgreSQL - Npgsql.NpgsqlException: sorry, too many clients already
12193정성태3/16/202017946개발 환경 구성: 485. docker - SAP Adaptive Server Enterprise 컨테이너 실행 [1]
12192정성태3/14/202019979개발 환경 구성: 484. docker - Sybase Anywhere 16 컨테이너 실행
12191정성태3/14/202021064개발 환경 구성: 483. docker - OracleXE 컨테이너 실행 [1]
12190정성태3/14/202015642오류 유형: 606. Docker Desktop 업그레이드 시 "The process cannot access the file 'C:\Program Files\Docker\Docker\resources\dockerd.exe' because it is being used by another process."
12189정성태3/13/202021248개발 환경 구성: 482. Facebook OAuth 처리 시 상태 정보 전달 방법과 "유효한 OAuth 리디렉션 URI" 설정 규칙
12188정성태3/13/202026035Windows: 169. 부팅 시점에 실행되는 chkdsk 결과를 확인하는 방법
12187정성태3/12/202015620오류 유형: 605. NtpClient was unable to set a manual peer to use as a time source because of duplicate error on '...'.
12186정성태3/12/202017408오류 유형: 604. The SysVol Permissions for one or more GPOs on this domain controller and not in sync with the permissions for the GPOs on the Baseline domain controller.
... 61  62  63  64  65  66  67  68  [69]  70  71  72  73  74  75  ...