|
| 1 | +// |
| 2 | +// Created by dwd on 21/12/2021. |
| 3 | +// |
| 4 | + |
| 5 | +#ifndef SIGSLOT_COTHREAD_H |
| 6 | +#define SIGSLOT_COTHREAD_H |
| 7 | + |
| 8 | +#include <thread> |
| 9 | +#include "sigslot/sigslot.h" |
| 10 | +#include "sigslot/tasklet.h" |
| 11 | + |
| 12 | +namespace sigslot { |
| 13 | + namespace cothread_internal { |
| 14 | + template<typename Result> |
| 15 | + struct awaitable { |
| 16 | + std::coroutine_handle<> awaiting = nullptr; |
| 17 | + |
| 18 | + awaitable() = default; |
| 19 | + awaitable(awaitable && other) = delete; |
| 20 | + awaitable(awaitable const &) = delete; |
| 21 | + |
| 22 | + bool await_ready() { |
| 23 | + return has_payload(); |
| 24 | + } |
| 25 | + |
| 26 | + void await_suspend(std::coroutine_handle<> h) { |
| 27 | + // The awaiting coroutine is already suspended. |
| 28 | + awaiting = h; |
| 29 | + await(); |
| 30 | + } |
| 31 | + |
| 32 | + auto await_resume() { |
| 33 | + return payload(); |
| 34 | + } |
| 35 | + |
| 36 | + void resolve() { |
| 37 | + std::coroutine_handle<> a = nullptr; |
| 38 | + std::swap(a, awaiting); |
| 39 | + if (a) sigslot::resume_switch(a); |
| 40 | + } |
| 41 | + |
| 42 | + template<typename Fn, typename ...Args> |
| 43 | + void run(Fn & fn, Args&&... args) { |
| 44 | + auto wrapped_fn = [this, &fn](Args... a) { |
| 45 | + try { |
| 46 | + auto result = fn(a...); |
| 47 | + { |
| 48 | + std::lock_guard l_(m_mutex); |
| 49 | + m_payload.emplace(result); |
| 50 | + resolve(); |
| 51 | + } |
| 52 | + } catch(...) { |
| 53 | + std::lock_guard l_(m_mutex); |
| 54 | + m_eptr = std::current_exception(); |
| 55 | + resolve(); |
| 56 | + } |
| 57 | + }; |
| 58 | + m_thread.emplace(wrapped_fn, args...); |
| 59 | + } |
| 60 | + |
| 61 | + void check_await() { |
| 62 | + if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
| 63 | + } |
| 64 | + |
| 65 | + bool has_payload() { |
| 66 | + if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
| 67 | + std::lock_guard l_(m_mutex); |
| 68 | + return m_eptr || m_payload.has_value(); |
| 69 | + } |
| 70 | + |
| 71 | + auto payload() { |
| 72 | + if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
| 73 | + m_thread->join(); |
| 74 | + m_thread.reset(); |
| 75 | + if (m_eptr) std::rethrow_exception(m_eptr); |
| 76 | + return *m_payload; |
| 77 | + } |
| 78 | + |
| 79 | + void await() { |
| 80 | + if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
| 81 | + std::lock_guard l_(m_mutex); |
| 82 | + if (m_eptr || m_payload.has_value()) { |
| 83 | + resolve(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private: |
| 88 | + std::optional<std::jthread> m_thread; |
| 89 | + std::optional<Result> m_payload; |
| 90 | + std::recursive_mutex m_mutex; |
| 91 | + std::exception_ptr m_eptr; |
| 92 | + }; |
| 93 | + template<> |
| 94 | + struct awaitable<void> { |
| 95 | + std::coroutine_handle<> awaiting = nullptr; |
| 96 | + |
| 97 | + awaitable() = default; |
| 98 | + awaitable(awaitable && other) = delete; |
| 99 | + awaitable(awaitable const &) = delete; |
| 100 | + |
| 101 | + bool await_ready() { |
| 102 | + return is_done(); |
| 103 | + } |
| 104 | + |
| 105 | + void await_suspend(std::coroutine_handle<> h) { |
| 106 | + // The awaiting coroutine is already suspended. |
| 107 | + awaiting = h; |
| 108 | + await(); |
| 109 | + } |
| 110 | + |
| 111 | + void await_resume() { |
| 112 | + done(); |
| 113 | + } |
| 114 | + |
| 115 | + void resolve() { |
| 116 | + std::coroutine_handle<> a = nullptr; |
| 117 | + std::swap(a, awaiting); |
| 118 | + if (a) sigslot::resume_switch(a); |
| 119 | + } |
| 120 | + |
| 121 | + template<typename Fn, typename ...Args> |
| 122 | + void run(Fn & fn, Args&&... args) { |
| 123 | + auto wrapped_fn = [this, &fn](Args... a) { |
| 124 | + try { |
| 125 | + fn(a...); |
| 126 | + { |
| 127 | + std::lock_guard l_(m_mutex); |
| 128 | + m_done = true; |
| 129 | + resolve(); |
| 130 | + } |
| 131 | + } catch(...) { |
| 132 | + std::lock_guard l_(m_mutex); |
| 133 | + m_eptr = std::current_exception(); |
| 134 | + resolve(); |
| 135 | + } |
| 136 | + }; |
| 137 | + m_thread.emplace(wrapped_fn, args...); |
| 138 | + } |
| 139 | + |
| 140 | + void check_await() { |
| 141 | + if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
| 142 | + } |
| 143 | + |
| 144 | + bool is_done() { |
| 145 | + if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
| 146 | + std::lock_guard l_(m_mutex); |
| 147 | + return m_eptr || m_done; |
| 148 | + } |
| 149 | + |
| 150 | + void done() { |
| 151 | + if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
| 152 | + m_thread->join(); |
| 153 | + m_thread.reset(); |
| 154 | + if (m_eptr) std::rethrow_exception(m_eptr); |
| 155 | + } |
| 156 | + |
| 157 | + void await() { |
| 158 | + if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
| 159 | + std::lock_guard l_(m_mutex); |
| 160 | + if (m_eptr || m_done) { |
| 161 | + resolve(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private: |
| 166 | + std::optional<std::jthread> m_thread; |
| 167 | + bool m_done = false; |
| 168 | + std::exception_ptr m_eptr; |
| 169 | + std::recursive_mutex m_mutex; |
| 170 | + }; |
| 171 | + template<typename T> |
| 172 | + struct awaitable_ptr { |
| 173 | + std::unique_ptr<awaitable<T>> m_guts; |
| 174 | + |
| 175 | + awaitable_ptr() : m_guts(std::make_unique<awaitable<T>>()) {} |
| 176 | + awaitable_ptr(awaitable_ptr &&) = default; |
| 177 | + |
| 178 | + awaitable<T> & operator co_await() { |
| 179 | + m_guts->check_await(); |
| 180 | + return *m_guts; |
| 181 | + } |
| 182 | + }; |
| 183 | + } |
| 184 | + |
| 185 | + template<typename Callable> |
| 186 | + class co_thread { |
| 187 | + public: |
| 188 | + private: |
| 189 | + Callable m_fn; |
| 190 | + public: |
| 191 | + |
| 192 | + template<typename ...Args> |
| 193 | + [[nodiscard]] auto operator() (Args && ...args) { |
| 194 | + cothread_internal::awaitable_ptr<decltype(m_fn(args...))> awaitable; |
| 195 | + awaitable.m_guts->run(m_fn, args...); |
| 196 | + return std::move(awaitable); |
| 197 | + } |
| 198 | + |
| 199 | + explicit co_thread(Callable && fn) : m_fn(std::move(fn)) {} |
| 200 | + }; |
| 201 | +} |
| 202 | + |
| 203 | +#endif |
0 commit comments