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

删除 awaitable_detached<> 类。减少代码重复 #40

Merged
merged 1 commit into from
Oct 19, 2024

Conversation

microcai
Copy link
Member

awaitable_detached 自从支持自身被 co_await 后,代码变得越来越像 awaitable
相似度极高。

既然如此,不如合并代码。 detach 的时候,创建一个新的 awaitable<>。

有没有 detach 其实根本原因在于有没有被 co_await.

因此,在 析构函数里,判断是不是 done() 即可知道有没有被 co_await 过了。
如果没有被 co_await 过,则是 detach 模式。在析构里把协程给 resume 起来。

由于析构的时候没有进行 destroy 操作,因此需要在 final_awaiter 里进行清理。
现在 final_awaiter 的部分清理逻辑本身就是从
awaitable_detached::promise_type::final_awaiter 里合并进来的。

同时detach 模式使用 awaitable 后,也修正了 detach 模式下吞异常的问题。

@EvinceMoi
Copy link
Member

少了这么多代码

@microcai
Copy link
Member Author

是的

@@ -371,19 +281,28 @@ namespace ucoro
{
using promise_type = awaitable_promise<T>;

explicit awaitable(std::coroutine_handle<promise_type> h) : current_coro_handle_(h)
explicit awaitable(std::coroutine_handle<promise_type> h)
: current_coro_handle_(h)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里混用了空格和tab

Copy link
Member

@EvinceMoi EvinceMoi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

除了格式和错别字,我看没别的问题

Copy link
Member

@EvinceMoi EvinceMoi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

格式和错别字

Copy link
Member Author

@microcai microcai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该是callee,错别字

是哦!

@microcai microcai force-pushed the drop_awaitable_detached branch from a3c3853 to 44b73eb Compare October 18, 2024 17:52
Copy link
Member Author

@microcai microcai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新了。

@microcai microcai requested a review from EvinceMoi October 18, 2024 17:53
@microcai microcai force-pushed the drop_awaitable_detached branch 3 times, most recently from 713bad9 to c836bc2 Compare October 18, 2024 18:00
awaitable_detached 自从支持自身被 co_await 后,代码变得越来越像 awaitable<T>
相似度极高。

既然如此,不如合并代码。 detach 的时候,创建一个新的 awaitable<>。

有没有 detach 其实根本原因在于有没有被 co_await.

因此,在 析构函数里,判断是不是 done() 即可知道有没有被 co_await 过了。
如果没有被 co_await 过,则是 detach 模式。在析构里把协程给 resume 起来。

由于析构的时候没有进行 destroy 操作,因此需要在 final_awaiter 里进行清理。
现在 final_awaiter<T> 的部分清理逻辑本身就是从
awaitable_detached::promise_type::final_awaiter 里合并进来的。

同时detach 模式使用 awaitable<T> 后,也修正了 detach 模式下吞异常的问题。
@microcai microcai force-pushed the drop_awaitable_detached branch from c836bc2 to 0898929 Compare October 18, 2024 18:08
@microcai microcai added bug Something isn't working enhancement New feature or request labels Oct 19, 2024
@EvinceMoi EvinceMoi merged commit 0bf511d into master Oct 19, 2024
12 checks passed
@EvinceMoi EvinceMoi deleted the drop_awaitable_detached branch October 19, 2024 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants