Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion for "C++ Coroutines: Understanding the Compiler Transform" #6

Open
lewissbaker opened this issue Aug 27, 2022 · 6 comments

Comments

@lewissbaker
Copy link
Owner

lewissbaker commented Aug 27, 2022

Discussion for the post at https://lewissbaker.github.io/2022/08/27/understanding-the-compiler-transform

Also see:
Reddit: https://www.reddit.com/r/cpp/comments/x0bj5z/c_coroutines_understanding_the_compiler_transform/
HackerNews: https://news.ycombinator.com/item?id=32642832

@brevzin
Copy link

brevzin commented Aug 29, 2022

Can you add permalinks to the section headers? e.g. I can manually link to, let's say, Step 6 by going through the html source but it'd be nice if there were just a link right there. Something like this?.

Also, given the length, a table of contents would be 👌. I'm sure there's some plugin somewhere you can add to do that easily, I just don't know what that is.

@lewissbaker
Copy link
Owner Author

@brevzin Thanks for the tip.

I've added anchor links and a table-of-contents now.

@bilosikia
Copy link

very good post!

Step 13: Implementing symmetric-transfer and the noop-coroutine

this is a loop in coroutine_handle resume function,so how can the compiler implement with tail-call?
or just for introduce the principles, there is no loop in real completed generated code?

@lewissbaker
Copy link
Owner Author

this is a loop in coroutine_handle resume function,so how can the compiler implement with tail-call?
or just for introduce the principles, there is no loop in real completed generated code?

There is no guaranteed tail-calls with C++ at the moment.
However, we can emulate a tail-call using a trampolining loop.
A compiler that supports native tail-calls (i.e. can jmp to the next continuation) would not need this loop.

@yangxiaopo
Copy link

Hello lewissbaker,
Can resume_point be a code address?
How does Awaitable.suspend trigger the resume?

@huang-jl
Copy link

I think there is a conflict with your previous post. In the section "Handling exceptions that propagate out of the coroutine body" of the post Understanding the promise type , you said

My current interpretation of the wording is that if control exits the coroutine-body, either via exception propagating out of co_await promise.initial_suspend(), promise.unhandled_exception() or co_await promise.final_suspend() or by the ... then the coroutine frame is automatically destroyed before execution returns to the caller/resumer.

But in this post, when you put the unhandled_exception in the another try-catch block like following:

try {
  // ...
} catch (...) {
    try {
        state->__promise.unhandled_exception();
    } catch (...) {
        state->__suspend_point = 2;
        state->__resume = nullptr; // mark as final-suspend-point
        throw;
    }
}

This will not cause the coroutine automatically destroyed before return to the caller/resumer. It makes this coroutine in the 'suspended' state and propagate the exception to the caller/resumer.

So which one is the current Coroutine TS wording to the behavior of rethrow exception in unhandled_exception?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants