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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include <deque>
  6. #include "librf.h"
  7. using namespace resumef;
  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. while (!co_await g_lock.try_lock())
  52. co_await yield();
  53. ++g_counter;
  54. std::cout << "push:" << g_counter << " on " << idx << std::endl;
  55. co_await 50ms;
  56. co_await g_lock.unlock();
  57. }
  58. co_await 50ms;
  59. }
  60. }
  61. static future_t<> test_mutex_timeout_push(size_t idx)
  62. {
  63. for (size_t i = 0; i < N; ++i)
  64. {
  65. {
  66. while (!co_await g_lock.try_lock_for(10ms))
  67. co_await yield();
  68. ++g_counter;
  69. std::cout << "push:" << g_counter << " on " << idx << std::endl;
  70. co_await 50ms;
  71. co_await g_lock.unlock();
  72. }
  73. co_await 50ms;
  74. }
  75. }
  76. //🔒-50ms-🗝-50ms-🔒-50ms-🗝-50ms-|
  77. //---------........---------.......
  78. static std::thread test_mutex_async_push(size_t idx)
  79. {
  80. return std::thread([=]
  81. {
  82. char provide_unique_address = 0;
  83. for (size_t i = 0; i < N; ++i)
  84. {
  85. if (g_lock.try_lock_for(500ms, &provide_unique_address))
  86. {
  87. batch_unlock_t _locker(std::adopt_lock, &provide_unique_address, g_lock);
  88. ++g_counter;
  89. std::cout << "push:" << g_counter << " on " << idx << std::endl;
  90. std::this_thread::sleep_for(50ms);
  91. //g_lock.unlock(&provide_unique_address);
  92. }
  93. std::this_thread::sleep_for(50ms);
  94. }
  95. });
  96. }
  97. static void resumable_mutex_synch()
  98. {
  99. go test_mutex_timeout_push(0);
  100. go test_mutex_pop(1);
  101. this_scheduler()->run_until_notask();
  102. std::cout << "result:" << g_counter << std::endl;
  103. }
  104. static void resumable_mutex_async()
  105. {
  106. auto th = test_mutex_async_push(0);
  107. std::this_thread::sleep_for(25ms);
  108. go test_mutex_pop(1);
  109. this_scheduler()->run_until_notask();
  110. th.join();
  111. std::cout << "result:" << g_counter << std::endl;
  112. }
  113. static future_t<> resumable_mutex_range_push(size_t idx, mutex_t a, mutex_t b, mutex_t c)
  114. {
  115. for (int i = 0; i < 10000; ++i)
  116. {
  117. batch_unlock_t __lockers = co_await mutex_t::lock(a, b, c);
  118. assert(a.is_locked());
  119. assert(b.is_locked());
  120. assert(c.is_locked());
  121. ++g_counter;
  122. //std::cout << "push:" << g_counter << " on " << idx << std::endl;
  123. //co_await 5ms;
  124. }
  125. }
  126. static future_t<> resumable_mutex_range_pop(size_t idx, mutex_t a, mutex_t b, mutex_t c)
  127. {
  128. for (int i = 0; i < 10000; ++i)
  129. {
  130. co_await mutex_t::lock(adopt_manual_unlock, a, b, c);
  131. assert(a.is_locked());
  132. assert(b.is_locked());
  133. assert(c.is_locked());
  134. --g_counter;
  135. //std::cout << "pop :" << g_counter << " on " << idx << std::endl;
  136. //co_await 5ms;
  137. co_await mutex_t::unlock(a, b, c);
  138. }
  139. }
  140. static void resumable_mutex_lock_range()
  141. {
  142. mutex_t mtxA, mtxB, mtxC;
  143. //不同的线程里加锁也需要是线程安全的
  144. std::thread push_th([&]
  145. {
  146. local_scheduler __ls__;
  147. go resumable_mutex_range_push(10, mtxA, mtxB, mtxC);
  148. go resumable_mutex_range_push(11, mtxA, mtxC, mtxB);
  149. this_scheduler()->run_until_notask();
  150. });
  151. go resumable_mutex_range_pop(12, mtxC, mtxB, mtxA);
  152. go resumable_mutex_range_pop(13, mtxB, mtxA, mtxC);
  153. this_scheduler()->run_until_notask();
  154. push_th.join();
  155. std::cout << "result:" << g_counter << std::endl;
  156. }
  157. void resumable_main_mutex()
  158. {
  159. resumable_mutex_synch();
  160. std::cout << std::endl;
  161. resumable_mutex_async();
  162. std::cout << std::endl;
  163. resumable_mutex_lock_range();
  164. }