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