-
Notifications
You must be signed in to change notification settings - Fork 1.6k
coroutine: introduce try_future #2973
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
Open
denesb
wants to merge
1
commit into
scylladb:master
Choose a base branch
from
denesb:try-future
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+306
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * This file is open source software, licensed to you under the terms | ||
| * of the Apache License, Version 2.0 (the "License"). See the NOTICE file | ||
| * distributed with this work for additional information regarding copyright | ||
| * ownership. You may not use this file except in compliance with the License. | ||
| * | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| /* | ||
| * Copyright (C) 2025-present ScyllaDB | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <seastar/core/coroutine.hh> | ||
|
|
||
| namespace seastar::internal { | ||
|
|
||
| template <typename T, typename U> | ||
| void try_future_resume_or_destroy_coroutine(seastar::future<T>& fut, seastar::task& coroutine_task) { | ||
| auto promise_ptr = static_cast<U*>(&coroutine_task); | ||
| auto hndl = std::coroutine_handle<U>::from_promise(*promise_ptr); | ||
|
|
||
| if (fut.failed()) { | ||
| hndl.promise().set_exception(std::move(fut).get_exception()); | ||
| hndl.destroy(); | ||
| } else { | ||
| hndl.resume(); | ||
| } | ||
| } | ||
|
|
||
| template <bool CheckPreempt, typename T> | ||
| class [[nodiscard]] try_future_awaiter : public seastar::task { | ||
| seastar::future<T> _future; | ||
| void (*_resume_or_destroy)(seastar::future<T>&, seastar::task&){}; | ||
| seastar::task* _coroutine_task{}; | ||
| seastar::task* _waiting_task{}; | ||
|
|
||
| public: | ||
| explicit try_future_awaiter(seastar::future<T>&& f) noexcept : _future(std::move(f)) {} | ||
|
|
||
| try_future_awaiter(const try_future_awaiter&) = delete; | ||
| try_future_awaiter(try_future_awaiter&&) = delete; | ||
|
|
||
| bool await_ready() const noexcept { | ||
| // Will suspend+schedule for ready failed futures too. | ||
| return _future.available() && !_future.failed() && (!CheckPreempt || !need_preempt()); | ||
| } | ||
|
|
||
| template<typename U> | ||
| void await_suspend(std::coroutine_handle<U> hndl) noexcept { | ||
| _resume_or_destroy = try_future_resume_or_destroy_coroutine<T, U>; | ||
| _coroutine_task = &hndl.promise(); | ||
| _waiting_task = hndl.promise().waiting_task(); | ||
|
|
||
| if (!_future.available()) { | ||
| _future.set_coroutine(*this); | ||
| } else { | ||
| schedule(this); | ||
| } | ||
| } | ||
|
|
||
| T await_resume() { | ||
| if constexpr (std::is_void_v<T>) { | ||
| _future.get(); | ||
| } else { | ||
| return std::move(_future).get(); | ||
| } | ||
| } | ||
|
|
||
| virtual void run_and_dispose() noexcept override { | ||
| _resume_or_destroy(_future, *_coroutine_task); | ||
| } | ||
|
|
||
| virtual task* waiting_task() noexcept override { | ||
| return _waiting_task; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace seastar::internal | ||
|
|
||
| namespace seastar::coroutine { | ||
|
|
||
| /// \brief co_await:s a \ref future and returns the wrapped result if successful, | ||
| /// terminates the coroutine otherwise, propagating the exception directly to the | ||
| /// waiter. | ||
| /// | ||
| /// If the future was successful, this is identical to co_await-ing the future | ||
| /// directly. If the future failed, the coroutine is not resumed and instead the | ||
| /// exception from the future is forwarded to the waiter directly and the | ||
| /// coroutine is destroyed. | ||
| /// | ||
| /// For example: | ||
| /// ``` | ||
| /// // Function careful to not throw exceptions, instead returning failed futures. | ||
| /// future<int> bar() { | ||
| /// if (something_bad_happened) { | ||
| /// return make_exception_future<>(std::runtime_error("error")); | ||
| /// } | ||
| /// return result; | ||
| /// } | ||
| /// | ||
| /// future<> foo() { | ||
| /// auto result = co_await coroutine::try_future(bar()); | ||
| /// // This code is only executed if bar() returned a successful future. | ||
| /// // Otherwise the exception is forwarded to the waiter future directly | ||
| /// // and the coroutine is destroyed. | ||
| /// check_result(result); | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// Note that by default, `try_future` checks for if the task quota is depleted, | ||
| /// which means that it will yield if the future is ready and \ref seastar::need_preempt() | ||
| /// returns true. Use \ref coroutine::try_future_without_preemption_check | ||
| /// to disable preemption checking. | ||
| template<typename T = void> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I s |
||
| class [[nodiscard]] try_future : public seastar::internal::try_future_awaiter<true, T> { | ||
| public: | ||
| explicit try_future(seastar::future<T>&& f) noexcept | ||
| : seastar::internal::try_future_awaiter<true, T>(std::move(f)) | ||
| {} | ||
| }; | ||
|
|
||
| /// \brief co_await:s a \ref future, returns the wrapped result if successful, | ||
| /// terminates the coroutine otherwise, propagating the exception to the waiter. | ||
| /// | ||
| /// Same as \ref coroutine::try_future, but does not check for preemption. | ||
| template<typename T = void> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too |
||
| class [[nodiscard]] try_future_without_preemption_check : public seastar::internal::try_future_awaiter<false, T> { | ||
| public: | ||
| explicit try_future_without_preemption_check(seastar::future<T>&& f) noexcept | ||
| : seastar::internal::try_future_awaiter<false, T>(std::move(f)) | ||
| {} | ||
| }; | ||
|
|
||
| } // namespace seastar::coroutine | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would
foo()look like if there were notry_future()? If I understand it correctly -- like thisand the result is the same -- if bar() resolves into exceptional future, the foo() is interrupted and results into exceptional future either, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but in that case an exception will be thrown. Currently, if you want to avoid that, you have to do this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, thanks for clarification.
Would you please add this "comparison" example somewhere? This doc-comment or tutorial.md maybe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to explain both bad alternatives.