This commit is contained in:
aozhiwei 2023-05-18 22:50:06 +08:00
parent 93c3442442
commit a3c22feb24
3 changed files with 10 additions and 10 deletions

View File

@ -17,7 +17,7 @@ void CoMgr::UnInit()
void CoMgr::Update() void CoMgr::Update()
{ {
Coroutine *co = nullptr, *tmp = nullptr; Coroutine *co = nullptr, *tmp = nullptr;
list_for_each_entry_safe(co, tmp, &co_list_, entry) { list_for_each_entry_safe(co, tmp, &co_list_, exec_entry_) {
if (!co->Exec()) { if (!co->Exec()) {
co->hold_self_ = nullptr; co->hold_self_ = nullptr;
} }

View File

@ -7,7 +7,7 @@
Coroutine::Coroutine(std::function<void(Coroutine*)> cb) Coroutine::Coroutine(std::function<void(Coroutine*)> cb)
{ {
INIT_LIST_HEAD(&entry); INIT_LIST_HEAD(&exec_entry_);
cb_ = cb; cb_ = cb;
source_ = std::make_shared<boost::coroutines2::coroutine<void>::pull_type> source_ = std::make_shared<boost::coroutines2::coroutine<void>::pull_type>
( (
@ -17,14 +17,14 @@ Coroutine::Coroutine(std::function<void(Coroutine*)> cb)
cb_(this); cb_(this);
CallExit(sink); CallExit(sink);
}); });
list_add_tail(&entry, &CoMgr::Instance()->co_list_); list_add_tail(&co_entry_, &CoMgr::Instance()->co_list_);
Attach(); Attach();
} }
Coroutine::~Coroutine() Coroutine::~Coroutine()
{ {
Deatch(); Deatch();
list_del_init(&entry); list_del_init(&co_entry_);
} }
bool Coroutine::Exec() bool Coroutine::Exec()
@ -64,13 +64,13 @@ void Coroutine::CoAwait(Awaiter* awaiter)
void Coroutine::Attach() void Coroutine::Attach()
{ {
list_add_tail(&entry, &CoMgr::Instance()->exec_list_); list_add_tail(&exec_entry_, &CoMgr::Instance()->exec_list_);
} }
void Coroutine::Deatch() void Coroutine::Deatch()
{ {
if (list_empty(&entry)) { if (list_empty(&exec_entry_)) {
list_del_init(&entry); list_del_init(&exec_entry_);
} }
} }

View File

@ -15,12 +15,9 @@ class Promise : public Awaiter
class Coroutine : public Awaiter, public std::enable_shared_from_this<Coroutine> class Coroutine : public Awaiter, public std::enable_shared_from_this<Coroutine>
{ {
public: public:
list_head entry;
Coroutine(std::function<void(Coroutine*)> cb); Coroutine(std::function<void(Coroutine*)> cb);
~Coroutine(); ~Coroutine();
bool Exec();
void CoSuspend(); void CoSuspend();
void CoResume(); void CoResume();
void CoYield(); void CoYield();
@ -29,12 +26,15 @@ class Coroutine : public Awaiter, public std::enable_shared_from_this<Coroutine>
private: private:
bool Exec();
void Attach(); void Attach();
void Deatch(); void Deatch();
void CallEnter(boost::coroutines2::coroutine<void>::push_type& sink); void CallEnter(boost::coroutines2::coroutine<void>::push_type& sink);
void CallExit(boost::coroutines2::coroutine<void>::push_type& sink); void CallExit(boost::coroutines2::coroutine<void>::push_type& sink);
private: private:
list_head co_entry_;
list_head exec_entry_;
std::shared_ptr<Coroutine> hold_self_; std::shared_ptr<Coroutine> hold_self_;
std::shared_ptr<boost::coroutines2::coroutine<void>::pull_type> source_; std::shared_ptr<boost::coroutines2::coroutine<void>::pull_type> source_;
std::function<void(Coroutine* co)> cb_; std::function<void(Coroutine* co)> cb_;