1
This commit is contained in:
parent
a1a58f1dee
commit
93c3442442
@ -6,6 +6,7 @@
|
||||
void CoMgr::Init()
|
||||
{
|
||||
INIT_LIST_HEAD(&co_list_);
|
||||
INIT_LIST_HEAD(&exec_list_);
|
||||
}
|
||||
|
||||
void CoMgr::UnInit()
|
||||
@ -17,11 +18,15 @@ void CoMgr::Update()
|
||||
{
|
||||
Coroutine *co = nullptr, *tmp = nullptr;
|
||||
list_for_each_entry_safe(co, tmp, &co_list_, entry) {
|
||||
co->Exec();
|
||||
if (!co->Exec()) {
|
||||
co->hold_self_ = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::weak_ptr<Coroutine> CoMgr::CreateCoroutine(std::function<void(Coroutine*)> cb)
|
||||
{
|
||||
return std::make_shared<Coroutine>(cb);
|
||||
auto co = std::make_shared<Coroutine>(cb);
|
||||
co->hold_self_ = co;
|
||||
return co;
|
||||
}
|
||||
|
@ -22,5 +22,7 @@ class CoMgr : public a8::Singleton<CoMgr>
|
||||
|
||||
list_head co_list_;
|
||||
|
||||
list_head exec_list_;
|
||||
|
||||
friend class Coroutine;
|
||||
};
|
||||
|
@ -17,20 +17,23 @@ Coroutine::Coroutine(std::function<void(Coroutine*)> cb)
|
||||
cb_(this);
|
||||
CallExit(sink);
|
||||
});
|
||||
list_add_tail(&entry, &CoMgr::Instance()->co_list_);
|
||||
Attach();
|
||||
}
|
||||
|
||||
Coroutine::~Coroutine()
|
||||
{
|
||||
Deatch();
|
||||
list_del_init(&entry);
|
||||
}
|
||||
|
||||
void Coroutine::Exec()
|
||||
bool Coroutine::Exec()
|
||||
{
|
||||
if (*source_) {
|
||||
(*source_)();
|
||||
return true;
|
||||
} else {
|
||||
Deatch();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,7 +64,7 @@ void Coroutine::CoAwait(Awaiter* awaiter)
|
||||
|
||||
void Coroutine::Attach()
|
||||
{
|
||||
list_add_tail(&entry, &CoMgr::Instance()->co_list_);
|
||||
list_add_tail(&entry, &CoMgr::Instance()->exec_list_);
|
||||
}
|
||||
|
||||
void Coroutine::Deatch()
|
||||
|
@ -12,7 +12,7 @@ class Promise : public Awaiter
|
||||
|
||||
};
|
||||
|
||||
class Coroutine : public Awaiter
|
||||
class Coroutine : public Awaiter, public std::enable_shared_from_this<Coroutine>
|
||||
{
|
||||
public:
|
||||
list_head entry;
|
||||
@ -20,7 +20,7 @@ class Coroutine : public Awaiter
|
||||
Coroutine(std::function<void(Coroutine*)> cb);
|
||||
~Coroutine();
|
||||
|
||||
void Exec();
|
||||
bool Exec();
|
||||
void CoSuspend();
|
||||
void CoResume();
|
||||
void CoYield();
|
||||
@ -35,8 +35,10 @@ class Coroutine : public Awaiter
|
||||
void CallExit(boost::coroutines2::coroutine<void>::push_type& sink);
|
||||
|
||||
private:
|
||||
std::shared_ptr<Coroutine> hold_self_;
|
||||
std::shared_ptr<boost::coroutines2::coroutine<void>::pull_type> source_;
|
||||
std::function<void(Coroutine* co)> cb_;
|
||||
boost::coroutines2::coroutine<void>::push_type* sink_ = nullptr;
|
||||
|
||||
friend class CoMgr;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user