Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
as_future.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright (C) 2022-present ScyllaDB
20 */
21
22#pragma once
23
24#include <seastar/core/coroutine.hh>
25
26namespace seastar {
27
28namespace internal {
29
30template <bool CheckPreempt, typename T>
31class [[nodiscard]] as_future_awaiter {
32 seastar::future<T> _future;
33
34public:
35 explicit as_future_awaiter(seastar::future<T>&& f) noexcept : _future(std::move(f)) {}
36
37 as_future_awaiter(const as_future_awaiter&) = delete;
38 as_future_awaiter(as_future_awaiter&&) = delete;
39
40 bool await_ready() const noexcept {
41 return _future.available() && (!CheckPreempt || !need_preempt());
42 }
43
44 template<typename U>
45 void await_suspend(std::coroutine_handle<U> hndl) noexcept {
46 if (!CheckPreempt || !_future.available()) {
47 _future.set_coroutine(hndl.promise());
48 } else {
49 schedule(&hndl.promise());
50 }
51 }
52
53 seastar::future<T> await_resume() {
54 return std::move(_future);
55 }
56};
57
58} // namespace seastar::internal
59
60namespace coroutine {
61
85template<typename T = void>
86class [[nodiscard]] as_future : public seastar::internal::as_future_awaiter<true, T> {
87public:
88 explicit as_future(seastar::future<T>&& f) noexcept : seastar::internal::as_future_awaiter<true, T>(std::move(f)) {}
89};
90
98template<typename T = void>
99class [[nodiscard]] as_future_without_preemption_check : public seastar::internal::as_future_awaiter<false, T> {
100public:
101 explicit as_future_without_preemption_check(seastar::future<T>&& f) noexcept : seastar::internal::as_future_awaiter<false, T>(std::move(f)) {}
102};
103
104} // namespace seastar::coroutine
105
106} // namespace seastar
co_await:s a future, returning it as result, without checking if preemption is needed.
Definition: as_future.hh:99
co_await:s a future, returning it as result.
Definition: as_future.hh:86
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
bool available() const noexcept
Checks whether the future is available.
Definition: future.hh:1394
Seastar API namespace.
Definition: abort_on_ebadf.hh:26