This commit is contained in:
aozhiwei 2023-05-18 23:23:33 +08:00
parent 97fb0f0aa6
commit 10c9ce7c68
2 changed files with 16 additions and 6 deletions

View File

@ -12,14 +12,20 @@ void CoMgr::Init()
[] (Coroutine* co)
{
a8::XPrintf("call 1\n", {});
a8::XPrintf("call 2\n", {});
for (int i = 0; i < 10; ++i) {
co->CoYield();
a8::XPrintf("call A %d\n", {i});
}
});
CreateCo
(
[] (Coroutine* co)
{
a8::XPrintf("call 10\n", {});
a8::XPrintf("call 11\n", {});
for (int i = 0; i < 10; ++i) {
co->CoYield();
a8::XPrintf("call B %d\n", {i});
}
});
}
@ -31,7 +37,7 @@ void CoMgr::UnInit()
void CoMgr::Update()
{
Coroutine *co = nullptr, *tmp = nullptr;
list_for_each_entry_safe(co, tmp, &co_list_, exec_entry_) {
list_for_each_entry_safe(co, tmp, &exec_list_, exec_entry_) {
if (!co->Exec()) {
co->hold_self_ = nullptr;
}

View File

@ -7,7 +7,10 @@
Coroutine::Coroutine(std::function<void(Coroutine*)> cb)
{
INIT_LIST_HEAD(&co_entry_);
INIT_LIST_HEAD(&exec_entry_);
list_add_tail(&co_entry_, &CoMgr::Instance()->co_list_);
Attach();
cb_ = cb;
source_ = std::make_shared<boost::coroutines2::coroutine<void>::pull_type>
(
@ -17,12 +20,13 @@ Coroutine::Coroutine(std::function<void(Coroutine*)> cb)
cb_(this);
CallExit(sink);
});
list_add_tail(&co_entry_, &CoMgr::Instance()->co_list_);
Attach();
}
Coroutine::~Coroutine()
{
#ifdef DEBUG
a8::XPrintf("Coroutine::~Coroutine()\n", {});
#endif
Deatch();
list_del_init(&co_entry_);
}
@ -69,7 +73,7 @@ void Coroutine::Attach()
void Coroutine::Deatch()
{
if (list_empty(&exec_entry_)) {
if (!list_empty(&exec_entry_)) {
list_del_init(&exec_entry_);
}
}