From 2a14f765c4f07cbf838d6dce449e2d56bfeb4e74 Mon Sep 17 00:00:00 2001 From: sladro Date: Mon, 12 Jan 2026 15:53:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=961=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BC=96=E8=AF=91bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/graph_manager.h | 27 +++++++++++++++++++++++++++ src/graph_manager.cpp | 29 ++++++++++++++++++----------- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/include/graph_manager.h b/include/graph_manager.h index 139969e..6b92709 100644 --- a/include/graph_manager.h +++ b/include/graph_manager.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "node.h" #include "plugin_loader.h" @@ -103,6 +104,32 @@ private: }; struct NodeEntry { + NodeEntry() = default; + NodeEntry(const NodeEntry&) = delete; + NodeEntry& operator=(const NodeEntry&) = delete; + + NodeEntry(NodeEntry&& other) noexcept { + *this = std::move(other); + } + + NodeEntry& operator=(NodeEntry&& other) noexcept { + if (this == &other) return *this; + id = std::move(other.id); + type = std::move(other.type); + role = std::move(other.role); + enabled = other.enabled; + config = std::move(other.config); + context = std::move(other.context); + node = std::move(other.node); + cpu_affinity = std::move(other.cpu_affinity); + + sched_state.store(other.sched_state.load(std::memory_order_relaxed), std::memory_order_relaxed); + pool_index = other.pool_index; + + metrics = std::move(other.metrics); + return *this; + } + std::string id; std::string type; std::string role; diff --git a/src/graph_manager.cpp b/src/graph_manager.cpp index d5a85d1..0c86a67 100644 --- a/src/graph_manager.cpp +++ b/src/graph_manager.cpp @@ -209,10 +209,10 @@ public: key_to_pool_.clear(); auto add_pool = [&](const std::string& key, std::vector cpus, int threads) -> uint32_t { - Pool p; - p.key = key; - p.cpus = std::move(cpus); - p.thread_count = std::max(1, threads); + auto p = std::make_unique(); + p->key = key; + p->cpus = std::move(cpus); + p->thread_count = std::max(1, threads); pools_.push_back(std::move(p)); const uint32_t idx = static_cast(pools_.size() - 1); key_to_pool_[key] = idx; @@ -244,9 +244,11 @@ public: } // Start pool threads. - for (auto& p : pools_) { - for (int i = 0; i < p.thread_count; ++i) { - p.threads.emplace_back([this, &p]() { WorkerLoop(p); }); + for (auto& up : pools_) { + Pool* p = up.get(); + if (!p) continue; + for (int i = 0; i < p->thread_count; ++i) { + p->threads.emplace_back([this, p]() { WorkerLoop(*p); }); } } @@ -258,7 +260,9 @@ public: if (!started_) return; stop_.store(true); - for (auto& p : pools_) { + for (auto& up : pools_) { + if (!up) continue; + Pool& p = *up; { std::lock_guard lock(p.mu); p.ready.clear(); @@ -266,7 +270,9 @@ public: p.cv.notify_all(); } - for (auto& p : pools_) { + for (auto& up : pools_) { + if (!up) continue; + Pool& p = *up; for (auto& t : p.threads) { if (t.joinable()) t.join(); } @@ -310,7 +316,8 @@ public: if (prev & kRunning) return; const uint32_t idx = (n->pool_index < pools_.size()) ? n->pool_index : default_pool_index_; - Pool& p = pools_[idx]; + if (idx >= pools_.size() || !pools_[idx]) return; + Pool& p = *pools_[idx]; { std::lock_guard lock(p.mu); p.ready.push_back(n); @@ -433,7 +440,7 @@ private: std::atomic stop_{false}; bool started_ = false; uint32_t default_pool_index_ = 0; - std::vector pools_; + std::vector> pools_; std::unordered_map key_to_pool_; };