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

when_v2.cpp 1016B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "librf/librf.h"
  2. namespace librf
  3. {
  4. namespace detail
  5. {
  6. LIBRF_API state_when_t::state_when_t(intptr_t counter_)
  7. :_counter(counter_)
  8. {
  9. }
  10. LIBRF_API void state_when_t::resume()
  11. {
  12. coroutine_handle<> handler = _coro;
  13. if (handler)
  14. {
  15. _coro = nullptr;
  16. _scheduler->del_final(this);
  17. handler.resume();
  18. }
  19. }
  20. LIBRF_API bool state_when_t::has_handler() const noexcept
  21. {
  22. return (bool)_coro;
  23. }
  24. LIBRF_API void state_when_t::on_cancel() noexcept
  25. {
  26. scoped_lock<lock_type> lock_(_lock);
  27. _counter.store(0);
  28. this->_coro = nullptr;
  29. }
  30. LIBRF_API bool state_when_t::on_notify_one()
  31. {
  32. scoped_lock<lock_type> lock_(_lock);
  33. if (_counter.fetch_sub(1, std::memory_order_acq_rel) == 1)
  34. {
  35. assert(this->_scheduler != nullptr);
  36. if (this->_coro)
  37. this->_scheduler->add_generator(this);
  38. return true;
  39. }
  40. return false;
  41. }
  42. LIBRF_API bool state_when_t::on_timeout()
  43. {
  44. scoped_lock<lock_type> lock_(_lock);
  45. return false;
  46. }
  47. }
  48. }