基于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.

test_async_memory_layout.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include "librf.h"
  6. using namespace resumef;
  7. template<class _Ctype>
  8. static void callback_get_long(int64_t a, int64_t b, _Ctype&& cb)
  9. {
  10. std::cout << std::endl << __FUNCTION__ << " - begin" << std::endl;
  11. //编译失败。因为这个函数不是"可恢复函数(resumeable function)",甚至都不是"可等待函数(awaitable function)"
  12. //void* frame_ptr = _coro_frame_ptr();
  13. using namespace std::chrono;
  14. std::thread([=, cb = std::forward<_Ctype>(cb)]
  15. {
  16. std::this_thread::sleep_for(500ms);
  17. cb(a + b);
  18. }).detach();
  19. std::cout << __FUNCTION__ << " - end" << std::endl;
  20. }
  21. //这种情况下,没有生成 frame-context,因此,并没有promise_type被内嵌在frame-context里
  22. future_t<int64_t> awaitable_get_long(int64_t a, int64_t b)
  23. {
  24. std::cout << std::endl << __FUNCTION__ << " - begin" << std::endl;
  25. //编译失败。因为这个函数不是"可恢复函数(resumeable function)",仅仅是"可等待函数(awaitable function)"
  26. //void* frame_ptr = _coro_frame_ptr();
  27. resumef::awaitable_t<int64_t> awaitable;
  28. callback_get_long(a, b, [awaitable](int64_t val)
  29. {
  30. awaitable.set_value(val);
  31. });
  32. std::cout << __FUNCTION__ << " - end" << std::endl;
  33. return awaitable.get_future();
  34. }
  35. future_t<int64_t> resumeable_get_long(int64_t x, int64_t y)
  36. {
  37. std::cout << std::endl << __FUNCTION__ << " - begin" << std::endl;
  38. using future_type = future_t<int64_t>;
  39. using promise_type = typename future_type::promise_type;
  40. using state_type = typename future_type::state_type;
  41. void* frame_ptr = _coro_frame_ptr();
  42. auto handler = coroutine_handle<promise_type>::from_address(frame_ptr);
  43. promise_type* promise = &handler.promise();
  44. state_type* state = handler.promise().get_state();
  45. std::cout << " future size=" << sizeof(future_type) << " / " << _Align_size<future_type>() << std::endl;
  46. std::cout << " promise size=" << sizeof(promise_type) << " / " << _Align_size<promise_type>() << std::endl;
  47. std::cout << " state size=" << sizeof(state_type) << " / "<< _Align_size<state_type>() << std::endl;
  48. std::cout << " frame size=" << _coro_frame_size() << ", alloc size=" << state->get_alloc_size() << std::endl;
  49. std::cout << " frame ptr=" << frame_ptr << "," << (void*)&frame_ptr << std::endl;
  50. std::cout << " frame end=" << (void*)((char*)(frame_ptr)+_coro_frame_size()) << std::endl;
  51. std::cout << " promise ptr=" << promise << "," << (void*)&promise << std::endl;
  52. std::cout << " handle ptr=" << handler.address() << "," << (void*)&handler << std::endl;
  53. std::cout << " state ptr=" << state << "," << (void*)&state << std::endl;
  54. std::cout << " parent ptr=" << state->get_parent() << std::endl;
  55. std::cout << " x=" << x << ", &x=" << std::addressof(x) << std::endl;
  56. std::cout << " y=" << y << ", &y=" << std::addressof(y) << std::endl;
  57. int64_t val = co_await awaitable_get_long(x, y);
  58. std::cout << " val=" << val << ", &val=" << std::addressof(val) << std::endl;
  59. std::cout << __FUNCTION__ << " - end" << std::endl;
  60. co_return val;
  61. }
  62. //这种情况下,会生成对应的 frame-context,一个promise_type被内嵌在frame-context里
  63. future_t<> resumable_get_long_2(int64_t a, int64_t b, int64_t c)
  64. {
  65. int64_t v1, v2, v3;
  66. std::cout << std::endl << __FUNCTION__ << " - begin" << std::endl;
  67. using future_type = future_t<>;
  68. using promise_type = typename future_type::promise_type;
  69. using state_type = typename future_type::state_type;
  70. void* frame_ptr = _coro_frame_ptr();
  71. auto handler = coroutine_handle<promise_type>::from_address(frame_ptr);
  72. promise_type * promise = &handler.promise();
  73. state_type * state = handler.promise().get_state();
  74. std::cout << " future size=" << sizeof(future_type) << " / " << _Align_size<future_type>() << std::endl;
  75. std::cout << " promise size=" << sizeof(promise_type) << " / " << _Align_size<promise_type>() << std::endl;
  76. std::cout << " state size=" << sizeof(state_type) << " / "<< _Align_size<state_type>() << std::endl;
  77. std::cout << " frame size=" << _coro_frame_size() << ", alloc size=" << state->get_alloc_size() << std::endl;
  78. std::cout << " frame ptr=" << frame_ptr << ","<< (void*)&frame_ptr << std::endl;
  79. std::cout << " frame end=" << (void *)((char*)(frame_ptr) + _coro_frame_size()) << std::endl;
  80. std::cout << " promise ptr=" << promise << "," << (void *)&promise << std::endl;
  81. std::cout << " handle ptr=" << handler.address() << "," << (void*)&handler << std::endl;
  82. std::cout << " state ptr=" << state << "," << (void*)&state << std::endl;
  83. std::cout << " parent ptr=" << state->get_parent() << std::endl;
  84. std::cout << " a=" << a << ", &a=" << std::addressof(a) << std::endl;
  85. std::cout << " b=" << b << ", &b=" << std::addressof(b) << std::endl;
  86. std::cout << " c=" << c << ", &c=" << std::addressof(c) << std::endl;
  87. v1 = co_await resumeable_get_long(a, b);
  88. std::cout << " v1=" << v1 << ", &v1=" << std::addressof(v1) << std::endl;
  89. v2 = co_await resumeable_get_long(b, c);
  90. std::cout << " v2=" << v2 << ", &v2=" << std::addressof(v2) << std::endl;
  91. v3 = co_await resumeable_get_long(v1, v2);
  92. std::cout << " v3=" << v3 << ", &v3=" << std::addressof(v3) << std::endl;
  93. int64_t v4 = v1 * v2 * v3;
  94. std::cout << " v4=" << v4 << ", &v4=" << std::addressof(v4) << std::endl;
  95. std::cout << __FUNCTION__ << " - end" << std::endl;
  96. }
  97. void resumable_main_layout()
  98. {
  99. std::cout << std::endl << __FUNCTION__ << " - begin" << std::endl;
  100. go resumable_get_long_2(1, 2, 5);
  101. resumef::this_scheduler()->run_until_notask();
  102. std::cout << __FUNCTION__ << " - end" << std::endl;
  103. }