1
This commit is contained in:
parent
edcd30b9e0
commit
755fd5dcd0
@ -19,4 +19,5 @@ class CoMgr : public a8::Singleton<CoMgr>
|
||||
|
||||
list_head co_list_;
|
||||
|
||||
friend class Coroutine;
|
||||
};
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "coroutine.h"
|
||||
#include "comgr.h"
|
||||
|
||||
Coroutine::Coroutine(std::function<void(Coroutine*)> cb)
|
||||
{
|
||||
@ -16,30 +17,36 @@ Coroutine::Coroutine(std::function<void(Coroutine*)> cb)
|
||||
cb_(this);
|
||||
CallExit(sink);
|
||||
});
|
||||
while (*source_) {
|
||||
a8::XPrintf("xxxxx\n", {});
|
||||
(*source_)();
|
||||
}
|
||||
Attach();
|
||||
}
|
||||
|
||||
Coroutine::~Coroutine()
|
||||
{
|
||||
Deatch();
|
||||
}
|
||||
|
||||
void Coroutine::Exec()
|
||||
{
|
||||
|
||||
if (*source_) {
|
||||
(*source_)();
|
||||
} else {
|
||||
Deatch();
|
||||
}
|
||||
}
|
||||
|
||||
void Coroutine::CoSuspend()
|
||||
{
|
||||
|
||||
Deatch();
|
||||
}
|
||||
|
||||
void Coroutine::CoResume()
|
||||
{
|
||||
|
||||
Attach();
|
||||
}
|
||||
|
||||
void Coroutine::CoYield()
|
||||
{
|
||||
|
||||
(*sink_)();
|
||||
}
|
||||
|
||||
void Coroutine::CoAwait(Awaiter& awaiter)
|
||||
@ -52,6 +59,18 @@ void Coroutine::CoAwait(Awaiter* awaiter)
|
||||
|
||||
}
|
||||
|
||||
void Coroutine::Attach()
|
||||
{
|
||||
list_add_tail(&entry, &CoMgr::Instance()->co_list_);
|
||||
}
|
||||
|
||||
void Coroutine::Deatch()
|
||||
{
|
||||
if (list_empty(&entry)) {
|
||||
list_del_init(&entry);
|
||||
}
|
||||
}
|
||||
|
||||
void Coroutine::CallEnter(boost::coroutines2::coroutine<void>::push_type& sink)
|
||||
{
|
||||
sink_ = &sink;
|
||||
|
@ -18,6 +18,7 @@ class Coroutine : public Awaiter
|
||||
list_head entry;
|
||||
|
||||
Coroutine(std::function<void(Coroutine*)> cb);
|
||||
~Coroutine();
|
||||
|
||||
void Exec();
|
||||
void CoSuspend();
|
||||
@ -28,6 +29,8 @@ class Coroutine : public Awaiter
|
||||
|
||||
private:
|
||||
|
||||
void Attach();
|
||||
void Deatch();
|
||||
void CallEnter(boost::coroutines2::coroutine<void>::push_type& sink);
|
||||
void CallExit(boost::coroutines2::coroutine<void>::push_type& sink);
|
||||
|
||||
@ -35,4 +38,5 @@ class Coroutine : public Awaiter
|
||||
std::shared_ptr<boost::coroutines2::coroutine<void>::pull_type> source_;
|
||||
std::function<void(Coroutine* co)> cb_;
|
||||
boost::coroutines2::coroutine<void>::push_type* sink_ = nullptr;
|
||||
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user