From 755fd5dcd07ee80a8c7793c8e25b02e06b865d49 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 18 May 2023 22:25:42 +0800 Subject: [PATCH] 1 --- server/robotserver/comgr.h | 1 + server/robotserver/coroutine.cc | 35 +++++++++++++++++++++++++-------- server/robotserver/coroutine.h | 4 ++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/server/robotserver/comgr.h b/server/robotserver/comgr.h index 8bd9d99e..8c096163 100644 --- a/server/robotserver/comgr.h +++ b/server/robotserver/comgr.h @@ -19,4 +19,5 @@ class CoMgr : public a8::Singleton list_head co_list_; + friend class Coroutine; }; diff --git a/server/robotserver/coroutine.cc b/server/robotserver/coroutine.cc index 80eaf034..5f4f7e53 100644 --- a/server/robotserver/coroutine.cc +++ b/server/robotserver/coroutine.cc @@ -3,6 +3,7 @@ #include #include "coroutine.h" +#include "comgr.h" Coroutine::Coroutine(std::function cb) { @@ -16,30 +17,36 @@ Coroutine::Coroutine(std::function 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::push_type& sink) { sink_ = &sink; diff --git a/server/robotserver/coroutine.h b/server/robotserver/coroutine.h index 6abf6104..940e5551 100644 --- a/server/robotserver/coroutine.h +++ b/server/robotserver/coroutine.h @@ -18,6 +18,7 @@ class Coroutine : public Awaiter list_head entry; Coroutine(std::function cb); + ~Coroutine(); void Exec(); void CoSuspend(); @@ -28,6 +29,8 @@ class Coroutine : public Awaiter private: + void Attach(); + void Deatch(); void CallEnter(boost::coroutines2::coroutine::push_type& sink); void CallExit(boost::coroutines2::coroutine::push_type& sink); @@ -35,4 +38,5 @@ class Coroutine : public Awaiter std::shared_ptr::pull_type> source_; std::function cb_; boost::coroutines2::coroutine::push_type* sink_ = nullptr; + };