基于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_mutex.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include <deque>
  6. #include "librf/librf.h"
  7. using namespace librf;
  8. using namespace std::chrono;
  9. static mutex_t g_lock;
  10. static intptr_t g_counter = 0;
  11. static const size_t N = 10;
  12. //🔒-50ms-🔒🗝🗝-150ms-|
  13. //-------------.........
  14. static future_t<> test_mutex_pop(size_t idx)
  15. {
  16. for (size_t i = 0; i < N / 2; ++i)
  17. {
  18. {
  19. batch_unlock_t _locker = co_await g_lock.lock(); //_locker析构后,会调用对应的unlock()函数。
  20. --g_counter;
  21. std::cout << "pop :" << g_counter << " on " << idx << std::endl;
  22. co_await 50ms;
  23. batch_unlock_t _locker_2 = co_await g_lock;
  24. --g_counter;
  25. std::cout << "pop :" << g_counter << " on " << idx << std::endl;
  26. }
  27. co_await 150ms;
  28. }
  29. }
  30. //🔒-50ms-🗝-50ms-🔒-50ms-🗝-50ms-|
  31. //---------........---------.......
  32. //方法之一
  33. static future_t<> test_mutex_push(size_t idx)
  34. {
  35. for (size_t i = 0; i < N; ++i)
  36. {
  37. {
  38. batch_unlock_t _locker = co_await g_lock.lock();
  39. ++g_counter;
  40. std::cout << "push:" << g_counter << " on " << idx << std::endl;
  41. co_await 50ms;
  42. }
  43. co_await 50ms;
  44. }
  45. }
  46. static future_t<> test_mutex_try_push(size_t idx)
  47. {
  48. for (size_t i = 0; i < N; ++i)
  49. {
  50. {
  51. for (;;)
  52. {
  53. auto result = co_await g_lock.try_lock();
  54. if (result) break;
  55. co_await yield();
  56. }
  57. ++g_counter;
  58. std::cout << "push:" << g_counter << " on " << idx << std::endl;
  59. co_await 50ms;
  60. co_await g_lock.unlock();
  61. }
  62. co_await 50ms;
  63. }
  64. }
  65. static future_t<> test_mutex_timeout_push(size_t idx)
  66. {
  67. for (size_t i = 0; i < N; ++i)
  68. {
  69. {
  70. for (;;)
  71. {
  72. auto result = co_await g_lock.try_lock_for(10ms);
  73. if (result) break;
  74. co_await yield();
  75. }
  76. ++g_counter;
  77. std::cout << "push:" << g_counter << " on " << idx << std::endl;
  78. co_await 50ms;
  79. co_await g_lock.unlock();
  80. }
  81. co_await 50ms;
  82. }
  83. }
  84. //🔒-50ms-🗝-50ms-🔒-50ms-🗝-50ms-|
  85. //---------........---------.......
  86. static std::thread test_mutex_async_push(size_t idx)
  87. {
  88. return std::thread([=]
  89. {
  90. char provide_unique_address = 0;
  91. for (size_t i = 0; i < N; ++i)
  92. {
  93. if (g_lock.try_lock_for(500ms, &provide_unique_address))
  94. {
  95. batch_unlock_t _locker(std::adopt_lock, &provide_unique_address, g_lock);
  96. ++g_counter;
  97. std::cout << "push:" << g_counter << " on " << idx << std::endl;
  98. std::this_thread::sleep_for(50ms);
  99. //g_lock.unlock(&provide_unique_address);
  100. }
  101. std::this_thread::sleep_for(50ms);
  102. }
  103. });
  104. }
  105. static void resumable_mutex_synch()
  106. {
  107. go test_mutex_timeout_push(0);
  108. go test_mutex_pop(1);
  109. this_scheduler()->run_until_notask();
  110. std::cout << "result:" << g_counter << std::endl;
  111. }
  112. static void resumable_mutex_async()
  113. {
  114. auto th = test_mutex_async_push(0);
  115. std::this_thread::sleep_for(25ms);
  116. go test_mutex_pop(1);
  117. this_scheduler()->run_until_notask();
  118. th.join();
  119. std::cout << "result:" << g_counter << std::endl;
  120. }
  121. static future_t<> resumable_mutex_range_push(size_t idx, mutex_t a, mutex_t b, mutex_t c)
  122. {
  123. for (int i = 0; i < 10000; ++i)
  124. {
  125. batch_unlock_t __lockers = co_await mutex_t::lock(a, b, c);
  126. assert(a.is_locked());
  127. assert(b.is_locked());
  128. assert(c.is_locked());
  129. ++g_counter;
  130. //std::cout << "push:" << g_counter << " on " << idx << std::endl;
  131. //co_await 5ms;
  132. }
  133. }
  134. static future_t<> resumable_mutex_range_pop(size_t idx, mutex_t a, mutex_t b, mutex_t c)
  135. {
  136. for (int i = 0; i < 10000; ++i)
  137. {
  138. batch_unlock_t __lockers = co_await mutex_t::lock(a, b, c);
  139. assert(a.is_locked());
  140. assert(b.is_locked());
  141. assert(c.is_locked());
  142. --g_counter;
  143. //std::cout << "pop :" << g_counter << " on " << idx << std::endl;
  144. //co_await 5ms;
  145. }
  146. }
  147. static void resumable_mutex_lock_range()
  148. {
  149. mutex_t mtxA, mtxB, mtxC;
  150. //不同的线程里加锁也需要是线程安全的
  151. std::thread push_th([&]
  152. {
  153. local_scheduler_t __ls__;
  154. go resumable_mutex_range_push(10, mtxA, mtxB, mtxC);
  155. go resumable_mutex_range_push(11, mtxA, mtxC, mtxB);
  156. this_scheduler()->run_until_notask();
  157. });
  158. go resumable_mutex_range_pop(12, mtxC, mtxB, mtxA);
  159. go resumable_mutex_range_pop(13, mtxB, mtxA, mtxC);
  160. this_scheduler()->run_until_notask();
  161. push_th.join();
  162. std::cout << "result:" << g_counter << std::endl;
  163. }
  164. void resumable_main_mutex()
  165. {
  166. std::cout << "begin resumable_mutex_synch()" << std::endl;
  167. resumable_mutex_synch();
  168. std::cout << std::endl;
  169. std::cout << "begin resumable_mutex_async()" << std::endl;
  170. resumable_mutex_async();
  171. std::cout << std::endl;
  172. std::cout << "begin resumable_mutex_lock_range()" << std::endl;
  173. resumable_mutex_lock_range();
  174. }
  175. int main()
  176. {
  177. resumable_main_mutex();
  178. return 0;
  179. }