|
| 1 | +/* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */ |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include "base/application.hpp" |
| 6 | +#include "base/shared-object.hpp" |
| 7 | +#include "base/threadpool.hpp" |
| 8 | +#include <boost/asio/dispatch.hpp> |
| 9 | +#include <boost/asio/executor_work_guard.hpp> |
| 10 | +#include <boost/asio/spawn.hpp> |
| 11 | +#include <future> |
| 12 | + |
| 13 | +namespace boost::asio::detail { |
| 14 | + |
| 15 | +struct fixed_throw_tag |
| 16 | +{}; |
| 17 | + |
| 18 | +/** |
| 19 | + * Fixes the issue where operations crash the program that can throw exceptions but don't. |
| 20 | + * |
| 21 | + * The issues is that in the orginal version of this specialization, the exception_ptr is |
| 22 | + * never checked against nullptr, but only the pointer to the exception_ptr, which is likely |
| 23 | + * a mistake. |
| 24 | + */ |
| 25 | +template<typename Executor, typename R, typename T> |
| 26 | +class spawn_handler<Executor, R(std::exception_ptr, T, fixed_throw_tag)> : public spawn_handler_base<Executor> |
| 27 | +{ |
| 28 | +public: |
| 29 | + using return_type = T; |
| 30 | + |
| 31 | + struct result_type |
| 32 | + { |
| 33 | + std::exception_ptr ex_; |
| 34 | + return_type* value_; |
| 35 | + }; |
| 36 | + |
| 37 | + spawn_handler(const basic_yield_context<Executor>& yield, result_type& result) |
| 38 | + : spawn_handler_base<Executor>(yield), result_(result) |
| 39 | + { |
| 40 | + } |
| 41 | + |
| 42 | + void operator()(std::exception_ptr ex, T value) |
| 43 | + { |
| 44 | + result_.ex_ = ex; |
| 45 | + result_.value_ = &value; |
| 46 | + this->resume(); |
| 47 | + } |
| 48 | + |
| 49 | + static return_type on_resume(result_type& result) |
| 50 | + { |
| 51 | + if (result.ex_) { |
| 52 | + rethrow_exception(result.ex_); |
| 53 | + } |
| 54 | + return BOOST_ASIO_MOVE_CAST(return_type)(*result.value_); |
| 55 | + } |
| 56 | + |
| 57 | +private: |
| 58 | + result_type& result_; |
| 59 | +}; |
| 60 | + |
| 61 | +} // namespace boost::asio::detail |
| 62 | + |
| 63 | +namespace icinga { |
| 64 | + |
| 65 | +template<typename> |
| 66 | +class AsioPromise; |
| 67 | + |
| 68 | +/** |
| 69 | + * Implements a generic, asynchronously awaitable future. |
| 70 | + * |
| 71 | + * This allows to queue an CPU-intensive action on another thread without blocking any |
| 72 | + * IO-threads and pass back the result via the @c AsioPromise. |
| 73 | + * |
| 74 | + * Similar to @c std::future, this is single-use only. Once a value has been set by the |
| 75 | + * @c AsioPromise, the job is done. |
| 76 | + */ |
| 77 | +template<typename ValueType> |
| 78 | +class AsioFuture : public SharedObject |
| 79 | +{ |
| 80 | + template<typename> |
| 81 | + friend class AsioPromise; |
| 82 | + |
| 83 | +public: |
| 84 | + DECLARE_PTR_TYPEDEFS(AsioFuture); |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns the value held in the future, or waits for the promise to complete. |
| 88 | + * |
| 89 | + * If an exception has been stored in the future via AsioPromise::SetException(), it will be |
| 90 | + * thrown by this function. Simply passing `yc[ec]` as a token will not change this, even if |
| 91 | + * the exception that would be thrown is a @c boost::asio::system::system_error. |
| 92 | + */ |
| 93 | + template<typename CompletionToken> |
| 94 | + auto Get(CompletionToken&& token) |
| 95 | + { |
| 96 | + using Signature = void(std::exception_ptr, ValueType, boost::asio::detail::fixed_throw_tag); |
| 97 | + |
| 98 | + return boost::asio::async_initiate<CompletionToken, Signature>( |
| 99 | + [this](auto&& handler) { InitOperation(std::forward<decltype(handler)>(handler)); }, |
| 100 | + std::forward<CompletionToken>(token) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + // TODO: Add WaitFor and WaitUntil |
| 105 | + |
| 106 | +private: |
| 107 | + template<typename Handler> |
| 108 | + void CallHandler(Handler&& handler) |
| 109 | + { |
| 110 | + if (std::holds_alternative<ValueType>(m_Value)) { |
| 111 | + std::forward<Handler>(handler)(nullptr, std::get<ValueType>(m_Value)); |
| 112 | + } else { |
| 113 | + std::forward<Handler>(handler)(std::get<std::exception_ptr>(m_Value), {}); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + template<typename Handler> |
| 118 | + void InitOperation(Handler&& handler) |
| 119 | + { |
| 120 | + auto handlerPtr = std::make_shared<std::decay_t<decltype(handler)>>(std::forward<decltype(handler)>(handler)); |
| 121 | + |
| 122 | + auto handlerWrapper = [handler = handlerPtr, future = AsioFuture::Ptr{this}]() { |
| 123 | + if (std::holds_alternative<ValueType>(future->m_Value)) { |
| 124 | + (*handler)({}, std::get<ValueType>(future->m_Value)); |
| 125 | + } else { |
| 126 | + (*handler)(std::get<std::exception_ptr>(future->m_Value), {}); |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + std::unique_lock lock(m_Mutex); |
| 131 | + |
| 132 | + if (!std::holds_alternative<std::monostate>(m_Value)) { |
| 133 | + boost::asio::post(boost::asio::get_associated_executor(handler), handlerWrapper); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + auto work = boost::asio::make_work_guard(handler); |
| 138 | + m_Callback = [handler = std::move(handlerWrapper), work = std::move(work)]() mutable { |
| 139 | + boost::asio::dispatch(work.get_executor(), handler); |
| 140 | + work.reset(); |
| 141 | + }; |
| 142 | + } |
| 143 | + |
| 144 | + std::mutex m_Mutex; |
| 145 | + std::variant<std::monostate, std::exception_ptr, ValueType> m_Value; |
| 146 | + std::function<void()> m_Callback; |
| 147 | +}; |
| 148 | + |
| 149 | +/** |
| 150 | + * A promise type that can be passed to any other thread or coroutine. |
| 151 | + */ |
| 152 | +template<typename ValueType> |
| 153 | +class AsioPromise |
| 154 | +{ |
| 155 | +public: |
| 156 | + AsioPromise() : m_Future(new AsioFuture<ValueType>) {} |
| 157 | + |
| 158 | + template<typename ForwardingType> |
| 159 | + void SetValue(ForwardingType&& value) const |
| 160 | + { |
| 161 | + std::unique_lock lock{m_Future->m_Mutex}; |
| 162 | + |
| 163 | + if (!std::holds_alternative<std::monostate>(m_Future->m_Value)) { |
| 164 | + BOOST_THROW_EXCEPTION(std::future_error{std::future_errc::promise_already_satisfied}); |
| 165 | + } |
| 166 | + |
| 167 | + m_Future->m_Value = std::forward<ForwardingType>(value); |
| 168 | + if (m_Future->m_Callback) { |
| 169 | + m_Future->m_Callback(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + template<typename ExceptionType> |
| 174 | + void SetException(ExceptionType&& ex) const |
| 175 | + { |
| 176 | + std::unique_lock lock{m_Future->m_Mutex}; |
| 177 | + |
| 178 | + if (!std::holds_alternative<std::monostate>(m_Future->m_Value)) { |
| 179 | + BOOST_THROW_EXCEPTION(std::future_error{std::future_errc::promise_already_satisfied}); |
| 180 | + } |
| 181 | + |
| 182 | + m_Future->m_Value = std::make_exception_ptr(std::forward<ExceptionType>(ex)); |
| 183 | + if (m_Future->m_Callback) { |
| 184 | + m_Future->m_Callback(); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + auto GetFuture() const { return m_Future; } |
| 189 | + |
| 190 | +private: |
| 191 | + typename AsioFuture<ValueType>::Ptr m_Future; |
| 192 | +}; |
| 193 | + |
| 194 | +template<typename Callback> |
| 195 | +auto QueueAsioFutureCallback(Callback&& cb) |
| 196 | +{ |
| 197 | + AsioPromise<decltype(cb())> promise; |
| 198 | + auto future = promise.GetFuture(); |
| 199 | + Application::GetTP().Post( |
| 200 | + [cb = std::forward<Callback>(cb), promise = std::move(promise)]() { |
| 201 | + try { |
| 202 | + promise.SetValue(cb()); |
| 203 | + } catch (const std::exception&) { |
| 204 | + promise.SetException(std::current_exception()); |
| 205 | + } |
| 206 | + }, |
| 207 | + {} |
| 208 | + ); |
| 209 | + return future; |
| 210 | +}; |
| 211 | + |
| 212 | +} // namespace icinga |
0 commit comments