|
10 | 10 | #include "sigslot/tasklet.h" |
11 | 11 |
|
12 | 12 | namespace sigslot { |
13 | | - template<typename Result, class... Args> |
14 | | - class co_thread { |
15 | | - public: |
16 | | - // Single argument version uses a bare T |
| 13 | + namespace cothread_internal { |
| 14 | + template<typename Result> |
17 | 15 | struct awaitable { |
18 | 16 | std::coroutine_handle<> awaiting = nullptr; |
19 | | - co_thread & wait_for; |
20 | 17 |
|
21 | | - explicit awaitable(co_thread & t) : wait_for(t) {} |
| 18 | + awaitable() = default; |
| 19 | + awaitable(awaitable && other) = delete; |
| 20 | + awaitable(awaitable const &) = delete; |
22 | 21 |
|
23 | 22 | bool await_ready() { |
24 | | - return wait_for.has_payload(); |
| 23 | + return has_payload(); |
25 | 24 | } |
26 | 25 |
|
27 | 26 | void await_suspend(std::coroutine_handle<> h) { |
28 | 27 | // The awaiting coroutine is already suspended. |
29 | 28 | awaiting = h; |
30 | | - wait_for.await(this); |
| 29 | + await(); |
31 | 30 | } |
32 | 31 |
|
33 | 32 | auto await_resume() { |
34 | | - return wait_for.payload(); |
| 33 | + return payload(); |
35 | 34 | } |
36 | 35 |
|
37 | 36 | void resolve() { |
38 | 37 | std::coroutine_handle<> a = nullptr; |
39 | 38 | std::swap(a, awaiting); |
40 | 39 | if (a) sigslot::resume_switch(a); |
41 | 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; |
42 | 92 | }; |
43 | | - private: |
44 | | - std::function<Result(Args...)> m_fn; |
45 | | - std::optional<std::jthread> m_thread; |
46 | | - std::optional<Result> m_payload; |
47 | | - std::recursive_mutex m_mutex; |
48 | | - awaitable * m_awaitable = nullptr; |
| 93 | + template<> |
| 94 | + struct awaitable<void> { |
| 95 | + std::coroutine_handle<> awaiting = nullptr; |
49 | 96 |
|
50 | | - public: |
| 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 | + } |
51 | 110 |
|
52 | | - explicit co_thread(std::function<Result(Args...)> && fn) : m_fn(std::move(fn)) {} |
| 111 | + void await_resume() { |
| 112 | + done(); |
| 113 | + } |
53 | 114 |
|
54 | | - co_thread & run(Args&&... args) { |
55 | | - auto wrapped_fn = [this](Args... a) { |
56 | | - auto result = m_fn(a...); |
57 | | - { |
58 | | - std::lock_guard l_(m_mutex); |
59 | | - m_payload.emplace(result); |
60 | | - if (m_awaitable) { |
61 | | - m_awaitable->resolve(); |
| 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(); |
62 | 135 | } |
63 | | - } |
64 | | - }; |
65 | | - m_thread.emplace(wrapped_fn, args...); |
66 | | - return *this; |
67 | | - } |
68 | | - auto & operator() (Args&&... args) { |
69 | | - return this->run(args...); |
70 | | - } |
| 136 | + }; |
| 137 | + m_thread.emplace(wrapped_fn, args...); |
| 138 | + } |
71 | 139 |
|
72 | | - awaitable operator co_await() { |
73 | | - if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
74 | | - return awaitable(*this); |
75 | | - } |
| 140 | + void check_await() { |
| 141 | + if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
| 142 | + } |
76 | 143 |
|
77 | | - bool has_payload() { |
78 | | - if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
79 | | - std::lock_guard l_(m_mutex); |
80 | | - return m_payload.has_value(); |
81 | | - } |
| 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 | + } |
82 | 149 |
|
83 | | - auto payload() { |
84 | | - if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
85 | | - m_thread->join(); |
86 | | - m_thread.reset(); |
87 | | - return *m_payload; |
88 | | - } |
| 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 | + } |
89 | 156 |
|
90 | | - void await(awaitable * a) { |
91 | | - if (!m_thread.has_value()) throw std::logic_error("No thread started"); |
92 | | - std::lock_guard l_(m_mutex); |
93 | | - m_awaitable = a; |
94 | | - if (m_payload.has_value()) { |
95 | | - a->resolve(); |
| 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 | + } |
96 | 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); |
97 | 197 | } |
| 198 | + |
| 199 | + explicit co_thread(Callable && fn) : m_fn(std::move(fn)) {} |
98 | 200 | }; |
99 | 201 | } |
100 | 202 |
|
|
0 commit comments