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