基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

librf.cpp 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include "librf.h"
  2. #include <experimental/resumable>
  3. #include <experimental/generator>
  4. #include <optional>
  5. extern void resumable_main_yield_return();
  6. extern void resumable_main_timer();
  7. extern void resumable_main_suspend_always();
  8. extern void resumable_main_sleep();
  9. extern void resumable_main_routine();
  10. extern void resumable_main_resumable();
  11. extern void resumable_main_mutex();
  12. extern void resumable_main_exception();
  13. extern void resumable_main_event();
  14. extern void resumable_main_event_timeout();
  15. extern void resumable_main_dynamic_go();
  16. extern void resumable_main_channel();
  17. extern void resumable_main_cb();
  18. extern void resumable_main_modern_cb();
  19. extern void resumable_main_multi_thread();
  20. extern void resumable_main_channel_mult_thread();
  21. extern void resumable_main_when_all();
  22. extern void resumable_main_benchmark_mem();
  23. extern void resumable_main_benchmark_asio_server();
  24. extern void resumable_main_benchmark_asio_client(intptr_t nNum);
  25. namespace coro = std::experimental;
  26. namespace librf2
  27. {
  28. struct scheduler_t;
  29. template<class _Ty = void>
  30. struct future_t;
  31. template<class _Ty = void>
  32. struct promise_t;
  33. template<class _PromiseT>
  34. struct is_promise : std::false_type {};
  35. template<class _Ty>
  36. struct is_promise<promise_t<_Ty>> : std::true_type {};
  37. template<class _Ty>
  38. _INLINE_VAR constexpr bool is_promise_v = is_promise<_Ty>::value;
  39. struct state_base_t
  40. {
  41. scheduler_t* _scheduler = nullptr;
  42. coro::coroutine_handle<> _coro;
  43. std::exception_ptr _exception;
  44. virtual ~state_base_t() {}
  45. virtual bool has_value() const = 0;
  46. void resume()
  47. {
  48. auto handler = _coro;
  49. _coro = nullptr;
  50. handler();
  51. }
  52. };
  53. template<class _Ty = void>
  54. struct state_t : public state_base_t
  55. {
  56. using value_type = _Ty;
  57. std::optional<value_type> _value;
  58. virtual bool has_value() const override
  59. {
  60. return _value.has_value();
  61. }
  62. };
  63. template<>
  64. struct state_t<void> : public state_base_t
  65. {
  66. bool _has_value = false;
  67. virtual bool has_value() const override
  68. {
  69. return _has_value;
  70. }
  71. };
  72. struct scheduler_t
  73. {
  74. using state_sptr = std::shared_ptr<state_base_t>;
  75. using state_vector = std::vector<state_sptr>;
  76. private:
  77. state_vector _runing_states;
  78. public:
  79. void add_initial(state_sptr sptr)
  80. {
  81. sptr->_scheduler = this;
  82. assert(sptr->_coro != nullptr);
  83. _runing_states.emplace_back(std::move(sptr));
  84. }
  85. void add_await(state_sptr sptr, coro::coroutine_handle<> handler)
  86. {
  87. sptr->_scheduler = this;
  88. sptr->_coro = handler;
  89. if (sptr->has_value() || sptr->_exception != nullptr)
  90. _runing_states.emplace_back(std::move(sptr));
  91. }
  92. void add_ready(state_sptr sptr)
  93. {
  94. assert(sptr->_scheduler == this);
  95. if (sptr->_coro != nullptr)
  96. _runing_states.emplace_back(std::move(sptr));
  97. }
  98. void run()
  99. {
  100. for (;;)
  101. {
  102. state_vector states = std::move(_runing_states);
  103. for (state_sptr& sptr : states)
  104. sptr->resume();
  105. }
  106. }
  107. };
  108. template<class _Ty>
  109. struct future_t
  110. {
  111. using value_type = _Ty;
  112. using state_type = state_t<value_type>;
  113. using promise_type = promise_t<value_type>;
  114. using future_type = future_t<value_type>;
  115. std::shared_ptr<state_type> _state;
  116. future_t(std::shared_ptr<state_type> _st)
  117. :_state(std::move(_st)) {}
  118. future_t(const future_t&) = default;
  119. future_t(future_t&&) = default;
  120. future_t& operator = (const future_t&) = default;
  121. future_t& operator = (future_t&&) = default;
  122. bool await_ready()
  123. {
  124. return _state->has_value();
  125. }
  126. template<class _PromiseT, typename = std::enable_if_t<is_promise_v<_PromiseT>>>
  127. void await_suspend(coro::coroutine_handle<_PromiseT> handler)
  128. {
  129. _PromiseT& promise = handler.promise();
  130. scheduler_t * sch = promise._state->_scheduler;
  131. sch->add_await(_state, handler);
  132. }
  133. value_type await_resume()
  134. {
  135. if (_state->_exception)
  136. std::rethrow_exception(std::move(_state->_exception));
  137. return std::move(_state->_value.value());
  138. }
  139. void resume() const
  140. {
  141. auto coro_handle = _state->_coro;
  142. _state->_coro = nullptr;
  143. coro_handle();
  144. }
  145. };
  146. struct suspend_on_initial
  147. {
  148. std::shared_ptr<state_base_t> _state;
  149. bool await_ready() noexcept
  150. {
  151. return false;
  152. }
  153. void await_suspend(coro::coroutine_handle<> handler) noexcept
  154. {
  155. _state->_coro = handler;
  156. }
  157. void await_resume() noexcept
  158. {
  159. }
  160. };
  161. template<class _Ty>
  162. struct promise_impl_t
  163. {
  164. using value_type = _Ty;
  165. using state_type = state_t<value_type>;
  166. using promise_type = promise_t<value_type>;
  167. using future_type = future_t<value_type>;
  168. std::shared_ptr<state_type> _state = std::make_shared<state_type>();
  169. promise_impl_t() {}
  170. promise_impl_t(const promise_impl_t&) = delete;
  171. promise_impl_t(promise_impl_t&&) = delete;
  172. promise_impl_t& operator = (const promise_impl_t&) = delete;
  173. promise_impl_t& operator = (promise_impl_t&&) = delete;
  174. auto initial_suspend() noexcept
  175. {
  176. return suspend_on_initial{ _state };
  177. }
  178. auto final_suspend() noexcept
  179. {
  180. return coro::suspend_never{};
  181. }
  182. void set_exception(std::exception_ptr e)
  183. {
  184. _state->_exception = std::move(e);
  185. if (_state->_scheduler != nullptr)
  186. _state->_scheduler->add_ready(_state);
  187. }
  188. future_t<value_type> get_return_object()
  189. {
  190. return { _state };
  191. }
  192. void cancellation_requested()
  193. {
  194. }
  195. };
  196. template<class _Ty>
  197. struct promise_t : public promise_impl_t<_Ty>
  198. {
  199. void return_value(value_type val)
  200. {
  201. _state->_value = std::move(val);
  202. if (_state->_scheduler != nullptr)
  203. _state->_scheduler->add_ready(_state);
  204. }
  205. void yield_value(value_type val)
  206. {
  207. _state->_value = std::move(val);
  208. if (_state->_scheduler != nullptr)
  209. _state->_scheduler->add_ready(_state);
  210. }
  211. };
  212. template<>
  213. struct promise_t<void> : public promise_impl_t<void>
  214. {
  215. void return_void()
  216. {
  217. _state->_has_value = true;
  218. if (_state->_scheduler != nullptr)
  219. _state->_scheduler->add_ready(_state);
  220. }
  221. void yield_value()
  222. {
  223. _state->_has_value = true;
  224. if (_state->_scheduler != nullptr)
  225. _state->_scheduler->add_ready(_state);
  226. }
  227. };
  228. template<class _Ty>
  229. struct awaitable_t
  230. {
  231. using value_type = _Ty;
  232. using state_type = state_t<value_type>;
  233. using future_type = future_t<value_type>;
  234. private:
  235. mutable std::shared_ptr<state_type> _state = std::make_shared<state_type>();
  236. public:
  237. awaitable_t() {}
  238. awaitable_t(const awaitable_t&) = default;
  239. awaitable_t(awaitable_t&&) = default;
  240. awaitable_t& operator = (const awaitable_t&) = default;
  241. awaitable_t& operator = (awaitable_t&&) = default;
  242. void set_value(value_type value) const
  243. {
  244. _state->_value = std::move(value);
  245. if (_state->_scheduler != nullptr)
  246. _state->_scheduler->add_ready(_state);
  247. _state = nullptr;
  248. }
  249. void set_exception(std::exception_ptr e)
  250. {
  251. _state->_exception = std::move(e);
  252. if (_state->_scheduler != nullptr)
  253. _state->_scheduler->add_ready(_state);
  254. _state = nullptr;
  255. }
  256. future_type get_future()
  257. {
  258. return future_type{ _state };
  259. }
  260. };
  261. template<>
  262. struct awaitable_t<void>
  263. {
  264. using value_type = void;
  265. using state_type = state_t<>;
  266. using future_type = future_t<>;
  267. mutable std::shared_ptr<state_type> _state = std::make_shared<state_type>();
  268. awaitable_t() {}
  269. awaitable_t(const awaitable_t&) = default;
  270. awaitable_t(awaitable_t&&) = default;
  271. awaitable_t& operator = (const awaitable_t&) = default;
  272. awaitable_t& operator = (awaitable_t&&) = default;
  273. void set_value() const
  274. {
  275. _state->_has_value = true;
  276. if (_state->_scheduler != nullptr)
  277. _state->_scheduler->add_ready(_state);
  278. _state = nullptr;
  279. }
  280. void set_exception(std::exception_ptr e)
  281. {
  282. _state->_exception = std::move(e);
  283. if (_state->_scheduler != nullptr)
  284. _state->_scheduler->add_ready(_state);
  285. _state = nullptr;
  286. }
  287. future_type get_future()
  288. {
  289. return future_type{ _state };
  290. }
  291. };
  292. }
  293. void async_get_long(int64_t val, std::function<void(int64_t)> cb)
  294. {
  295. using namespace std::chrono;
  296. std::thread([val, cb = std::move(cb)]
  297. {
  298. std::this_thread::sleep_for(10s);
  299. cb(val * val);
  300. }).detach();
  301. }
  302. //这种情况下,没有生成 frame-context,因此,并没有promise_type被内嵌在frame-context里
  303. librf2::future_t<int64_t> co_get_long(int64_t val)
  304. {
  305. librf2::awaitable_t<int64_t > st;
  306. std::cout << "co_get_long@1" << std::endl;
  307. async_get_long(val, [st](int64_t value)
  308. {
  309. std::cout << "co_get_long@2" << std::endl;
  310. st.set_value(value);
  311. });
  312. std::cout << "co_get_long@3" << std::endl;
  313. return st.get_future();
  314. }
  315. //这种情况下,会生成对应的 frame-context,一个promise_type被内嵌在frame-context里
  316. librf2::future_t<> test_librf2()
  317. {
  318. auto f = co_await co_get_long(2);
  319. std::cout << f << std::endl;
  320. }
  321. int main(int argc, const char* argv[])
  322. {
  323. librf2::scheduler_t sch;
  324. librf2::future_t<> ft = test_librf2();
  325. sch.add_initial(ft._state);
  326. sch.run();
  327. resumable_main_resumable();
  328. //resumable_main_benchmark_mem();
  329. /*
  330. if (argc > 1)
  331. resumable_main_benchmark_asio_client(atoi(argv[1]));
  332. else
  333. resumable_main_benchmark_asio_server();
  334. */
  335. //return 0;
  336. resumable_main_when_all();
  337. resumable_main_multi_thread();
  338. resumable_main_yield_return();
  339. resumable_main_timer();
  340. resumable_main_suspend_always();
  341. resumable_main_sleep();
  342. resumable_main_routine();
  343. resumable_main_resumable();
  344. resumable_main_mutex();
  345. resumable_main_event();
  346. resumable_main_event_timeout();
  347. resumable_main_dynamic_go();
  348. resumable_main_channel();
  349. resumable_main_cb();
  350. resumable_main_exception();
  351. return 0;
  352. }