Skip to content

DetachedCoroutine 似乎没有必要? #147

@yanminhui

Description

@yanminhui

当前逻辑:

struct FinalAwaiter {
    bool await_ready() const noexcept { return false; }
    template <typename PromiseType>
    auto await_suspend(std::coroutine_handle<PromiseType> h) noexcept {
        return h.promise()._continuation;
    }
    void await_resume() noexcept {}
};

// Lazy:
template <typename F>
void start(F&& callback) {
    // callback should take a single Try<T> as parameter, return value will
    // be ignored. a detached coroutine will not suspend at initial/final
    // suspend point.
    auto launchCoro = [](Lazy lazy,
                         std::decay_t<F> cb) -> detail::DetachedCoroutine {
        cb(std::move(co_await lazy.coAwaitTry()));
    };
    [[maybe_unused]] auto detached =
        launchCoro(std::move(*this), std::forward<F>(callback));
}

是不是可以简化成如下:移除 DetachedCoroutine

struct FinalAwaiter {
    bool await_ready() const noexcept { return false; }
    template <typename PromiseType>
    void await_suspend(std::coroutine_handle<PromiseType> h) noexcept {
        if (h.promise()._continuation) { // 如果调用者是协程,激活它;否则无需挂起并由编译器释放内存)。
            h.promise()._continuation.resume();
        }
    }
    void await_resume() noexcept {}
};

// Lazy:
template <typename F>
void start(F&& callback) {
    if (!isReady()) {
        _coro.resume(); // 激活当前挂起的协程
    }
    callback(coAwaitTry().await_resume()); // 获取协程运行结果
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions