基于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_event_timeout.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include "librf/librf.h"
  6. using namespace librf;
  7. future_t<> resumalbe_set_event(const event_t & e, std::chrono::milliseconds dt)
  8. {
  9. co_await librf::sleep_for(dt);
  10. e.signal();
  11. std::cout << "+";
  12. }
  13. void async_set_event(const event_t & e, std::chrono::milliseconds dt)
  14. {
  15. std::thread([=]
  16. {
  17. std::this_thread::sleep_for(dt);
  18. e.signal();
  19. }).detach();
  20. }
  21. void test_wait_timeout_one()
  22. {
  23. std::cout << __FUNCTION__ << std::endl;
  24. using namespace std::chrono;
  25. event_t evt;
  26. go [&evt]() -> future_t<>
  27. {
  28. intptr_t counter = 0;
  29. for (;;)
  30. {
  31. auto result = co_await evt.wait_for(100ms);
  32. if (result)
  33. break;
  34. ++counter;
  35. std::cout << ".";
  36. }
  37. std::cout << counter << std::endl;
  38. };
  39. async_set_event(evt, 2s + 50ms);
  40. //go resumalbe_set_event(evt, 2s + 50ms);
  41. this_scheduler()->run_until_notask();
  42. }
  43. #if 0
  44. void test_wait_timeout_any_invalid()
  45. {
  46. std::cout << __FUNCTION__ << std::endl;
  47. using namespace std::chrono;
  48. event_t evts[8];
  49. //无效的等待
  50. go[&]()-> future_t<>
  51. {
  52. intptr_t idx = co_await event_t::wait_any_for(500ms, std::begin(evts), std::end(evts));
  53. assert(idx < 0);
  54. (void)idx;
  55. std::cout << "invalid wait!" << std::endl;
  56. };
  57. this_scheduler()->run_until_notask();
  58. }
  59. void test_wait_timeout_any()
  60. {
  61. std::cout << __FUNCTION__ << std::endl;
  62. using namespace std::chrono;
  63. event_t evts[8];
  64. go[&]() -> future_t<>
  65. {
  66. intptr_t counter = 0;
  67. for (;;)
  68. {
  69. intptr_t idx = co_await event_t::wait_any_for(500ms, evts);
  70. if (idx >= 0)
  71. {
  72. std::cout << counter << std::endl;
  73. std::cout << "event " << idx << " signal!" << std::endl;
  74. break;
  75. }
  76. ++counter;
  77. std::cout << ".";
  78. }
  79. //取消剩下的定时器,以便于协程调度器退出来
  80. this_scheduler()->timer()->clear();
  81. };
  82. srand((int)time(nullptr));
  83. for (auto & e : evts)
  84. {
  85. //go resumalbe_set_event(e, 1ms * (1000 + rand() % 5000));
  86. async_set_event(e, 1ms * (1000 + rand() % 5000));
  87. }
  88. this_scheduler()->run_until_notask();
  89. }
  90. #endif
  91. void test_wait_timeout_all_invalid()
  92. {
  93. std::cout << __FUNCTION__ << std::endl;
  94. using namespace std::chrono;
  95. event_t evts[8];
  96. //无效的等待
  97. go[&]()-> future_t<>
  98. {
  99. bool result = co_await event_t::wait_all_for(500ms, std::begin(evts), std::end(evts));
  100. assert(!result);
  101. (void)result;
  102. std::cout << "invalid wait!" << std::endl;
  103. };
  104. this_scheduler()->run_until_notask();
  105. }
  106. void test_wait_timeout_all()
  107. {
  108. std::cout << __FUNCTION__ << std::endl;
  109. using namespace std::chrono;
  110. event_t evts[8];
  111. go[&]() -> future_t<>
  112. {
  113. intptr_t counter = 0;
  114. for (;;)
  115. {
  116. auto result = co_await event_t::wait_all_for(1500ms, evts);
  117. if (result)
  118. {
  119. std::cout << counter << std::endl;
  120. std::cout << "all event signal!" << std::endl;
  121. break;
  122. }
  123. ++counter;
  124. std::cout << ".";
  125. std::cout << "timeout!" << std::endl;
  126. break;
  127. }
  128. };
  129. srand((int)time(nullptr));
  130. for (auto & e : evts)
  131. {
  132. //go resumalbe_set_event(e, 1ms * (1000 + rand() % 5000));
  133. async_set_event(e, 1ms * (1000 + rand() % 1000));
  134. }
  135. this_scheduler()->run_until_notask();
  136. }
  137. void resumable_main_event_timeout()
  138. {
  139. using namespace std::chrono;
  140. test_wait_timeout_one();
  141. std::cout << std::endl;
  142. #if 0
  143. test_wait_timeout_any_invalid();
  144. std::cout << std::endl << std::endl;
  145. test_wait_timeout_any();
  146. std::cout << std::endl << std::endl;
  147. #endif
  148. test_wait_timeout_all_invalid();
  149. std::cout << std::endl;
  150. test_wait_timeout_all();
  151. std::cout << std::endl;
  152. }
  153. #if LIBRF_TUTORIAL_STAND_ALONE
  154. int main()
  155. {
  156. resumable_main_event_timeout();
  157. return 0;
  158. }
  159. #endif