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

event_v2.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "librf/librf.h"
  2. namespace librf
  3. {
  4. namespace detail
  5. {
  6. void state_event_base_t::resume()
  7. {
  8. coroutine_handle<> handler = _coro;
  9. if (handler)
  10. {
  11. _coro = nullptr;
  12. _scheduler->del_final(this);
  13. handler.resume();
  14. }
  15. }
  16. bool state_event_base_t::has_handler() const noexcept
  17. {
  18. return (bool)_coro;
  19. }
  20. void state_event_t::on_cancel() noexcept
  21. {
  22. event_v2_impl** oldValue = _value.load(std::memory_order_acquire);
  23. if (oldValue != nullptr && _value.compare_exchange_strong(oldValue, nullptr, std::memory_order_acq_rel))
  24. {
  25. *oldValue = nullptr;
  26. _thandler.stop();
  27. this->_coro = nullptr;
  28. }
  29. }
  30. bool state_event_t::on_notify(event_v2_impl* eptr)
  31. {
  32. event_v2_impl** oldValue = _value.load(std::memory_order_acquire);
  33. if (oldValue != nullptr && _value.compare_exchange_strong(oldValue, nullptr, std::memory_order_acq_rel))
  34. {
  35. *oldValue = eptr;
  36. _thandler.stop();
  37. assert(this->_scheduler != nullptr);
  38. if (this->_coro)
  39. this->_scheduler->add_generator(this);
  40. return true;
  41. }
  42. return false;
  43. }
  44. bool state_event_t::on_timeout()
  45. {
  46. event_v2_impl** oldValue = _value.load(std::memory_order_acquire);
  47. if (oldValue != nullptr && _value.compare_exchange_strong(oldValue, nullptr, std::memory_order_acq_rel))
  48. {
  49. *oldValue = nullptr;
  50. _thandler.reset();
  51. assert(this->_scheduler != nullptr);
  52. if (this->_coro)
  53. this->_scheduler->add_generator(this);
  54. return true;
  55. }
  56. return false;
  57. }
  58. void state_event_all_t::on_cancel() noexcept
  59. {
  60. intptr_t oldValue = _counter.load(std::memory_order_acquire);
  61. if (oldValue >= 0 && _counter.compare_exchange_strong(oldValue, -1, std::memory_order_acq_rel))
  62. {
  63. *_value = false;
  64. _thandler.stop();
  65. this->_coro = nullptr;
  66. }
  67. }
  68. bool state_event_all_t::on_notify(event_v2_impl*)
  69. {
  70. intptr_t oldValue = _counter.load(std::memory_order_acquire);
  71. if (oldValue <= 0) return false;
  72. oldValue = _counter.fetch_add(-1, std::memory_order_acq_rel);
  73. if (oldValue == 1)
  74. {
  75. *_value = true;
  76. _thandler.stop();
  77. assert(this->_scheduler != nullptr);
  78. if (this->_coro)
  79. this->_scheduler->add_generator(this);
  80. return true;
  81. }
  82. return oldValue >= 1;
  83. }
  84. bool state_event_all_t::on_timeout()
  85. {
  86. intptr_t oldValue = _counter.load(std::memory_order_acquire);
  87. if (oldValue >= 0 && _counter.compare_exchange_strong(oldValue, -1, std::memory_order_acq_rel))
  88. {
  89. *_value = false;
  90. _thandler.reset();
  91. assert(this->_scheduler != nullptr);
  92. if (this->_coro)
  93. this->_scheduler->add_generator(this);
  94. return true;
  95. }
  96. return false;
  97. }
  98. event_v2_impl::event_v2_impl(bool initially) noexcept
  99. : _counter(initially ? 1 : 0)
  100. {
  101. }
  102. template<class _Ty, class _Ptr>
  103. static auto try_pop_list(intrusive_link_queue<_Ty, _Ptr>& list)
  104. {
  105. return list.try_pop();
  106. }
  107. template<class _Ptr>
  108. static _Ptr try_pop_list(std::list<_Ptr>& list)
  109. {
  110. if (!list.empty())
  111. {
  112. _Ptr ptr = list.front();
  113. list.pop_front();
  114. return ptr;
  115. }
  116. return nullptr;
  117. }
  118. template<class _Ty, class _Ptr>
  119. static void clear_list(intrusive_link_queue<_Ty, _Ptr>& list)
  120. {
  121. for (; list.try_pop() != nullptr;);
  122. }
  123. template<class _Ptr>
  124. static void clear_list(std::list<_Ptr>& list)
  125. {
  126. list.clear();
  127. }
  128. event_v2_impl::~event_v2_impl()
  129. {
  130. clear_list(_wait_awakes);
  131. }
  132. void event_v2_impl::signal_all() noexcept
  133. {
  134. scoped_lock<lock_type> lock_(_lock);
  135. _counter.store(0, std::memory_order_release);
  136. state_event_ptr state;
  137. for (; (state = try_pop_list(_wait_awakes)) != nullptr;)
  138. {
  139. (void)state->on_notify(this);
  140. }
  141. }
  142. void event_v2_impl::signal() noexcept
  143. {
  144. scoped_lock<lock_type> lock_(_lock);
  145. state_event_ptr state;
  146. for (; (state = try_pop_list(_wait_awakes)) != nullptr;)
  147. {
  148. if (state->on_notify(this))
  149. return;
  150. }
  151. _counter.fetch_add(1, std::memory_order_acq_rel);
  152. }
  153. }
  154. event_t::event_t(bool initially)
  155. :_event(std::make_shared<detail::event_v2_impl>(initially))
  156. {
  157. }
  158. event_t::event_t(std::adopt_lock_t)
  159. {
  160. }
  161. event_t::~event_t()
  162. {
  163. }
  164. }