2021-11-01 17:59:08 +08:00
|
|
|
|
#include "librf/librf.h"
|
2020-03-12 16:56:23 +08:00
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
|
namespace librf
|
2020-03-12 16:56:23 +08:00
|
|
|
|
{
|
|
|
|
|
namespace detail
|
|
|
|
|
{
|
2021-11-02 11:01:24 +08:00
|
|
|
|
LIBRF_API state_when_t::state_when_t(intptr_t counter_)
|
2020-03-12 16:56:23 +08:00
|
|
|
|
:_counter(counter_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 11:01:24 +08:00
|
|
|
|
LIBRF_API void state_when_t::resume()
|
2020-03-12 16:56:23 +08:00
|
|
|
|
{
|
|
|
|
|
coroutine_handle<> handler = _coro;
|
|
|
|
|
if (handler)
|
|
|
|
|
{
|
|
|
|
|
_coro = nullptr;
|
|
|
|
|
_scheduler->del_final(this);
|
|
|
|
|
handler.resume();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 11:01:24 +08:00
|
|
|
|
LIBRF_API bool state_when_t::has_handler() const noexcept
|
2020-03-12 16:56:23 +08:00
|
|
|
|
{
|
|
|
|
|
return (bool)_coro;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 11:01:24 +08:00
|
|
|
|
LIBRF_API void state_when_t::on_cancel() noexcept
|
2020-03-12 16:56:23 +08:00
|
|
|
|
{
|
|
|
|
|
scoped_lock<lock_type> lock_(_lock);
|
|
|
|
|
|
|
|
|
|
_counter.store(0);
|
|
|
|
|
this->_coro = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 11:01:24 +08:00
|
|
|
|
LIBRF_API bool state_when_t::on_notify_one()
|
2020-03-12 16:56:23 +08:00
|
|
|
|
{
|
|
|
|
|
scoped_lock<lock_type> lock_(_lock);
|
|
|
|
|
|
|
|
|
|
if (_counter.fetch_sub(1, std::memory_order_acq_rel) == 1)
|
|
|
|
|
{
|
|
|
|
|
assert(this->_scheduler != nullptr);
|
|
|
|
|
if (this->_coro)
|
|
|
|
|
this->_scheduler->add_generator(this);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 11:01:24 +08:00
|
|
|
|
LIBRF_API bool state_when_t::on_timeout()
|
2020-03-12 16:56:23 +08:00
|
|
|
|
{
|
|
|
|
|
scoped_lock<lock_type> lock_(_lock);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|