8000 Do not destroy co-routines too early. by rgriebl · Pull Request #26 · qcoro/qcoro · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Do not destroy co-routines too early. #26

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 oc 8000 casionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions qcoro/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ class TaskFinalSuspend {
void await_suspend(QCORO_STD::coroutine_handle<_Promise> finishedCoroutine) noexcept {
auto &promise = finishedCoroutine.promise();

promise.mInFinalSuspend = true;

if (promise.mResumeAwaiter.exchange(true, std::memory_order_acq_rel)) {
promise.mAwaitingCoroutine.resume();
}
finishedCoroutine.destroy();
if (promise.mDestroyOnFinalSuspend) {
finishedCoroutine.destroy();
} else {
promise.mInFinalSuspend = false;
}
}

//! Called by the compiler when the just-finished coroutine should be resumed.
Expand Down Expand Up @@ -228,13 +234,23 @@ class TaskPromiseBase {
return mAwaitingCoroutine != nullptr;
}

bool isInFinalSuspend() const {
return mInFinalSuspend;
}

void setDestroyOnFinalSuspend() {
mDestroyOnFinalSuspend = true;
}

private:
friend class TaskFinalSuspend;

//! Handle of the coroutine that is currently co_awaiting this Awaitable
QCORO_STD::coroutine_handle<> mAwaitingCoroutine;
//! Indicates whether the awaiter should be resumed when it tries to co_await on us.
std::atomic<bool> mResumeAwaiter{false};
std::atomic<bool> mInFinalSuspend{false};
std::atomic<bool> mDestroyOnFinalSuspend{false};
};

//! The promise_type for Task<T>
Expand Down Expand Up @@ -469,7 +485,14 @@ class Task {
}

//! Destructor.
~Task() = default;
~Task() {
if (mCoroutine) {
if (mCoroutine.done() && !mCoroutine.promise().isInFinalSuspend())
mCoroutine.destroy();
else
mCoroutine.promise().setDestroyOnFinalSuspend();
Comment on lines +490 to +493
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add braces, even for one-line if blocks.

}
}

//! Returns whether the task has finished.
/*!
Expand Down
0