|
| 1 | +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
| 2 | +// vim: ts=8 sw=2 smarttab |
| 3 | +/* |
| 4 | + * Ceph - scalable distributed file system |
| 5 | + * |
| 6 | + * This is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License version 2.1, as published by the Free Software |
| 9 | + * Foundation. See file COPYING. |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +#pragma once |
| 14 | + |
| 15 | +#include <exception> |
| 16 | +#include <optional> |
| 17 | +#include <boost/asio/append.hpp> |
| 18 | +#include <boost/asio/async_result.hpp> |
| 19 | +#include <boost/asio/dispatch.hpp> |
| 20 | +#include <boost/asio/execution/executor.hpp> |
| 21 | +#include <boost/asio/use_awaitable.hpp> |
| 22 | +#include "include/ceph_assert.h" |
| 23 | + |
| 24 | +namespace ceph::async { |
| 25 | + |
| 26 | +/// Captures an awaitable handler for deferred completion or cancellation. |
| 27 | +template <typename Ret, boost::asio::execution::executor Executor> |
| 28 | +class co_waiter { |
| 29 | + using signature = void(std::exception_ptr, Ret); |
| 30 | + using token_type = boost::asio::use_awaitable_t<Executor>; |
| 31 | + using handler_type = typename boost::asio::async_result< |
| 32 | + token_type, signature>::handler_type; |
| 33 | + std::optional<handler_type> handler; |
| 34 | + |
| 35 | + struct op_cancellation { |
| 36 | + co_waiter* self; |
| 37 | + op_cancellation(co_waiter* self) : self(self) {} |
| 38 | + void operator()(boost::asio::cancellation_type_t type) { |
| 39 | + if (type != boost::asio::cancellation_type::none) { |
| 40 | + self->cancel(); |
| 41 | + } |
| 42 | + } |
| 43 | + }; |
| 44 | + public: |
| 45 | + co_waiter() = default; |
| 46 | + |
| 47 | + // copy and move are disabled because the cancellation handler captures 'this' |
| 48 | + co_waiter(const co_waiter&) = delete; |
| 49 | + co_waiter& operator=(const co_waiter&) = delete; |
| 50 | + |
| 51 | + /// Returns true if there's a handler awaiting completion. |
| 52 | + bool waiting() const { return handler.has_value(); } |
| 53 | + |
| 54 | + /// Returns an awaitable that blocks until complete() or cancel(). |
| 55 | + boost::asio::awaitable<Ret, Executor> get() |
| 56 | + { |
| 57 | + ceph_assert(!handler); |
| 58 | + token_type token; |
| 59 | + return boost::asio::async_initiate<token_type, signature>( |
| 60 | + [this] (handler_type h) { |
| 61 | + auto slot = boost::asio::get_associated_cancellation_slot(h); |
| 62 | + if (slot.is_connected()) { |
| 63 | + slot.template emplace<op_cancellation>(this); |
| 64 | + } |
| 65 | + handler.emplace(std::move(h)); |
| 66 | + }, token); |
| 67 | + } |
| 68 | + |
| 69 | + /// Schedule the completion handler with the given arguments. |
| 70 | + void complete(std::exception_ptr eptr, Ret value) |
| 71 | + { |
| 72 | + ceph_assert(handler); |
| 73 | + auto h = boost::asio::append(std::move(*handler), eptr, std::move(value)); |
| 74 | + handler.reset(); |
| 75 | + boost::asio::dispatch(std::move(h)); |
| 76 | + } |
| 77 | + |
| 78 | + /// Cancel the coroutine with an operation_aborted exception. |
| 79 | + void cancel() |
| 80 | + { |
| 81 | + if (handler) { |
| 82 | + auto eptr = std::make_exception_ptr( |
| 83 | + boost::system::system_error( |
| 84 | + boost::asio::error::operation_aborted)); |
| 85 | + complete(eptr, Ret{}); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// Destroy the completion handler. |
| 90 | + void shutdown() |
| 91 | + { |
| 92 | + handler.reset(); |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +// specialization for Ret=void |
| 97 | +template <boost::asio::execution::executor Executor> |
| 98 | +class co_waiter<void, Executor> { |
| 99 | + using signature = void(std::exception_ptr); |
| 100 | + using token_type = boost::asio::use_awaitable_t<Executor>; |
| 101 | + using handler_type = typename boost::asio::async_result< |
| 102 | + token_type, signature>::handler_type; |
| 103 | + std::optional<handler_type> handler; |
| 104 | + |
| 105 | + struct op_cancellation { |
| 106 | + co_waiter* self; |
| 107 | + op_cancellation(co_waiter* self) : self(self) {} |
| 108 | + void operator()(boost::asio::cancellation_type_t type) { |
| 109 | + if (type != boost::asio::cancellation_type::none) { |
| 110 | + self->cancel(); |
| 111 | + } |
| 112 | + } |
| 113 | + }; |
| 114 | + public: |
| 115 | + co_waiter() = default; |
| 116 | + |
| 117 | + // copy and move are disabled because the cancellation handler captures 'this' |
| 118 | + co_waiter(const co_waiter&) = delete; |
| 119 | + co_waiter& operator=(const co_waiter&) = delete; |
| 120 | + |
| 121 | + /// Returns true if there's a handler awaiting completion. |
| 122 | + bool waiting() const { return handler.has_value(); } |
| 123 | + |
| 124 | + /// Returns an awaitable that blocks until complete() or cancel(). |
| 125 | + boost::asio::awaitable<void, Executor> get() |
| 126 | + { |
| 127 | + ceph_assert(!handler); |
| 128 | + token_type token; |
| 129 | + return boost::asio::async_initiate<token_type, signature>( |
| 130 | + [this] (handler_type h) { |
| 131 | + auto slot = boost::asio::get_associated_cancellation_slot(h); |
| 132 | + if (slot.is_connected()) { |
| 133 | + slot.template emplace<op_cancellation>(this); |
| 134 | + } |
| 135 | + handler.emplace(std::move(h)); |
| 136 | + }, token); |
| 137 | + } |
| 138 | + |
| 139 | + /// Schedule the completion handler with the given arguments. |
| 140 | + void complete(std::exception_ptr eptr) |
| 141 | + { |
| 142 | + ceph_assert(handler); |
| 143 | + auto h = boost::asio::append(std::move(*handler), eptr); |
| 144 | + handler.reset(); |
| 145 | + boost::asio::dispatch(std::move(h)); |
| 146 | + } |
| 147 | + |
| 148 | + /// Cancel the coroutine with an operation_aborted exception. |
| 149 | + void cancel() |
| 150 | + { |
| 151 | + if (handler) { |
| 152 | + auto eptr = std::make_exception_ptr( |
| 153 | + boost::system::system_error( |
| 154 | + boost::asio::error::operation_aborted)); |
| 155 | + complete(eptr); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /// Destroy the completion handler. |
| 160 | + void shutdown() |
| 161 | + { |
| 162 | + handler.reset(); |
| 163 | + } |
| 164 | +}; |
| 165 | + |
| 166 | +} // namespace ceph::async |
0 commit comments