refactor(server_world): separate gen and net thread pools

Introduce a second thread pool for network operations and a `ThreadPoolKind` enum to distinguish between gen and net pools. Rename `pool_threads()` to `gen_pool_threads()`, add `change_pool_threads()` overload that accepts the pool kind, and update the dev panel to use the gen pool. Adjust logging and initialization to handle both pools.
This commit is contained in:
2026-06-28 14:46:42 +08:00
parent 3d4c41a76e
commit d2636189a4
3 changed files with 54 additions and 19 deletions

View File

@@ -23,6 +23,7 @@ namespace Cubed {
class Session;
class ServerWorld {
public:
enum class ThreadPoolKind { NET, GEN };
ServerWorld();
~ServerWorld();
void handle_player_exit(const std::string& uuid);
@@ -58,9 +59,10 @@ public:
bool is_tick_running() const;
void tick_running(bool run);
int pool_threads() const;
int gen_pool_threads() const;
int max_threads() const;
void change_pool_threads(int threads);
void change_pool_threads(ThreadPoolKind kind, int threads);
int chunk_load_style() const;
void set_chunk_load_style(int id);
@@ -135,7 +137,8 @@ private:
std::atomic<bool> m_is_rebuilding{false};
std::atomic<bool> m_init{false};
std::atomic<int> m_rendering_distance{24};
std::atomic<int> m_pool_threads{0};
std::atomic<int> m_gen_pool_threads{0};
std::atomic<int> m_net_pool_threads{0};
std::atomic<int> m_max_threads{1};
std::atomic<TickType> m_game_ticks{0};
@@ -152,6 +155,7 @@ private:
RecentQueue<std::string> m_need_gen_queue;
std::atomic<std::shared_ptr<ThreadPool>> m_gen_thread_pool;
std::atomic<std::shared_ptr<ThreadPool>> m_net_thread_pool;
std::atomic<ChunkLoadStyle> m_chunk_load_style{ChunkLoadStyle::CENTER};
@@ -177,5 +181,9 @@ private:
void send_time();
void send_chunk(int task_id, const std::string& uuid, ChunkPos pos);
int
change_pool_threads(std::atomic<std::shared_ptr<ThreadPool>>& thread_pool,
int threads);
};
} // namespace Cubed

View File

@@ -504,13 +504,14 @@ void DevPanel::show_server_world_table_bar() {
}
ImGui::Text("Pool Threads %d Max Support Threads %d Reserved Threads %d",
m_app.server_world().pool_threads(),
m_app.server_world().gen_pool_threads(),
m_app.server_world().max_threads(), RESERVED_THREADS);
ImGui::SliderInt("Set Pool Threads", &m_threads, 1,
m_app.server_world().max_threads());
ImGui::SameLine();
if (ImGui::Button("Set")) {
m_app.server_world().change_pool_threads(m_threads);
m_app.server_world().change_pool_threads(
ServerWorld::ThreadPoolKind::GEN, m_threads);
}
if (m_threads > m_app.server_world().max_threads() - RESERVED_THREADS) {
ImGui::TextColored(

View File

@@ -390,10 +390,19 @@ void ServerWorld::start_server_thread() {
void ServerWorld::start_thread_pool() {
int max_thread = std::thread::hardware_concurrency();
if (m_pool_threads == 0) {
change_pool_threads(max_thread - RESERVED_THREADS);
if (m_gen_pool_threads == 0) {
m_gen_pool_threads = change_pool_threads(m_gen_thread_pool,
max_thread - RESERVED_THREADS);
} else {
change_pool_threads(m_pool_threads);
m_gen_pool_threads =
change_pool_threads(m_gen_thread_pool, m_gen_pool_threads);
}
if (m_net_pool_threads == 0) {
m_net_pool_threads = change_pool_threads(m_net_thread_pool, 4);
} else {
m_net_pool_threads =
change_pool_threads(m_net_thread_pool, m_net_pool_threads);
}
}
@@ -420,7 +429,14 @@ void ServerWorld::stop_thread_pool() {
pool_ptr->stop();
}
m_gen_thread_pool.store(nullptr);
Logger::info("Thread Pool Stopped");
Logger::info("Gen Thread Pool Stopped");
auto p = m_net_thread_pool.load();
if (p) {
p->stop();
}
m_net_thread_pool.store(nullptr);
Logger::info("Net Thread Pool Stopped");
}
void ServerWorld::serever_run(std::stop_token stoken) {
@@ -536,10 +552,7 @@ void ServerWorld::update() {
{
std::lock_guard lk(m_chunks_mutex);
bool consumed = false;
auto size = m_new_finished_chunk.size();
if (size != 0) {
Logger::info("New generated {} chunks", size);
}
for (auto& x : m_new_finished_chunk) {
auto it = m_chunks.find(x.pos);
if (it == m_chunks.end()) {
@@ -704,7 +717,7 @@ void ServerWorld::handle_chunk_req(int task_id, const std::string& uuid,
it->second.task_id(task_id);
}
}
auto pool = m_gen_thread_pool.load();
auto pool = m_net_thread_pool.load();
pool->enqueue(
[task_id, uuid, pos, this]() { send_chunk(task_id, uuid, pos); });
}
@@ -757,18 +770,31 @@ void ServerWorld::per_tick_time(int ms) { m_per_tick_time = ms; }
bool ServerWorld::is_tick_running() const { return m_tick_running.load(); }
void ServerWorld::tick_running(bool run) { m_tick_running = run; }
int ServerWorld::pool_threads() const { return m_pool_threads.load(); }
int ServerWorld::gen_pool_threads() const { return m_gen_pool_threads.load(); }
int ServerWorld::max_threads() const { return m_max_threads.load(); }
void ServerWorld::change_pool_threads(int threads) {
void ServerWorld::change_pool_threads(ThreadPoolKind kind, int threads) {
switch (kind) {
case ThreadPoolKind::NET:
m_net_pool_threads = change_pool_threads(m_net_thread_pool, threads);
break;
case ThreadPoolKind::GEN:
m_gen_pool_threads = change_pool_threads(m_gen_thread_pool, threads);
break;
}
}
int ServerWorld::change_pool_threads(
std::atomic<std::shared_ptr<ThreadPool>>& thread_pool, int threads) {
m_max_threads = std::thread::hardware_concurrency();
if (m_max_threads < 1) {
Logger::warn("Can't Get Max Support Threads, Set Max Threads to 4");
m_max_threads = 4;
m_max_threads = 1;
}
int used_thread = std::clamp(threads, 1, m_max_threads.load());
Logger::info("Create New Thread Pool Use {} Threads", used_thread);
m_gen_thread_pool.store(std::make_shared<ThreadPool>(used_thread));
m_pool_threads = used_thread;
thread_pool.store(std::make_shared<ThreadPool>(used_thread));
return used_thread;
}
int ServerWorld::chunk_load_style() const {
return std::to_underlying(m_chunk_load_style.load());