You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of feel free to ask me(@MBkkt) any questions about this task, often the task may not be described in enough detail for devs who don't have the full context.
Sometimes we want to return one coroutine result from another coroutine (pseudo-code):
yaclib::Future<int> SomeAsync(...) {
... some async code ...
yaclib::Future<int> f = yaclib::Run(...) // run another coroutine
... some async code ...
co_return f;
}
Now it can be written as
yaclib::Future<int> SomeAsync(...) {
... some async code ...
yaclib::Future<int> f = yaclib::Run(...) // run another coroutine
... some async code ...
co_awaitAwait(f);
co_returnstd::move(f).Get();
}
yaclib::Future<int> SomeAsync(...) {
... some async code ...
yaclib::Future<int> f = yaclib::Run(...) // run another coroutine
... some async code ...
co_returnco_awaitstd::move(f);
}
We cannot overload promise_type::return_value for this, because after compiler call return_value it call final_suspend, but we don't want it (because we want call SetResult only in Here call see below).
So we need to create special Awaiter for this task.
I think it can be created with function named yaclib::Return or yaclib::CoReturn:
yaclib::Future<int> SomeAsync(...) {
... some async code ...
yaclib::Future<int> f = yaclib::Run(...) // run another coroutine
... some async code ...
co_awaitReturn(std::move(f));
... unreachable ...
}
Why is it better than 3rd code snippet?
Because we can make it cheaper:
In third snippet we create awaiter on stack set it as callback to f.
Then we suspend
After this callback will be called we resumed and make move value from f to our coroutine
and then we make final suspend.
With this suggestion we can make single suspend instead of two for this return.
How to make this Awaiter(pseudo-code)
structReturnAwaiter {
explicitReturnAwaiter(Future<T>&&);
constexprboolawait_ready() const { returnfalse; } // we want to make return so always falseconstexprvoidawait_resume() const {};
template<typename Promise>
voidawait_suspend(std::coroutine_handle<Promise> handle) {
// here we set callback to future that we want to return.// we set our coroutine (handle) to callback// so we need to implement this callback for PromiseType<V, E>
future.SetHere(handle.promise(), kHereCall);
}
};
How to implement callback for PromiseType<V, E>:
This callback named virtual void BaseCore::Here(...)
void PromiseType<V, E>::Here(...) noexceptfinal {
this->Store(core.Get()); // we move value from future that we want return to our coroutine storagethis->SetResult(); // we mark this coroutine as done
}
First of feel free to ask me(@MBkkt) any questions about this task, often the task may not be described in enough detail for devs who don't have the full context.
Sometimes we want to return one coroutine result from another coroutine (pseudo-code):
Now it can be written as
After this task #187:
We cannot overload
promise_type::return_value
for this, because after compiler call return_value it call final_suspend, but we don't want it (because we want call SetResult only in Here call see below).So we need to create special Awaiter for this task.
I think it can be created with function named
yaclib::Return
oryaclib::CoReturn
:Why is it better than 3rd code snippet?
Because we can make it cheaper:
In third snippet we create awaiter on stack set it as callback to f.
Then we suspend
After this callback will be called we resumed and make move value from f to our coroutine
and then we make final suspend.
With this suggestion we can make single suspend instead of two for this return.
How to make this Awaiter(pseudo-code)
How to implement callback for
PromiseType<V, E>
:This callback named
virtual void BaseCore::Here(...)
https://github.com/YACLib/YACLib/blob/main/include/yaclib/coroutine/detail/promise_type.hpp#L40
The text was updated successfully, but these errors were encountered: