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

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