mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
feat(server-world): use priority thread pool for chunk generation
Introduce PriorityThreadPool to process chunk generation tasks with priorities based on their distance from the player. Closer chunks receive higher priority, improving responsiveness.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "Cubed/gameplay/river_worm.hpp"
|
||||
#include "Cubed/gameplay/server_chunk.hpp"
|
||||
#include "Cubed/gameplay/server_player.hpp"
|
||||
#include "Cubed/tools/priority_thread_pool.hpp"
|
||||
#include "Cubed/tools/recent_queue.hpp"
|
||||
#include "Cubed/tools/thread_pool.hpp"
|
||||
#include "world/block_change.pb.h"
|
||||
@@ -147,7 +148,7 @@ private:
|
||||
|
||||
RecentQueue<std::string> m_need_gen_queue;
|
||||
|
||||
std::atomic<std::shared_ptr<ThreadPool>> m_gen_thread_pool;
|
||||
std::atomic<std::shared_ptr<PriorityThreadPool>> m_gen_thread_pool;
|
||||
std::atomic<std::shared_ptr<ThreadPool>> m_net_thread_pool;
|
||||
|
||||
std::atomic<ChunkLoadStyle> m_chunk_load_style{ChunkLoadStyle::CENTER};
|
||||
@@ -178,6 +179,9 @@ private:
|
||||
int
|
||||
change_pool_threads(std::atomic<std::shared_ptr<ThreadPool>>& thread_pool,
|
||||
int threads);
|
||||
int change_pool_threads(
|
||||
std::atomic<std::shared_ptr<PriorityThreadPool>>& thread_pool,
|
||||
int threads);
|
||||
void send_server_stop();
|
||||
};
|
||||
} // namespace Cubed
|
||||
|
||||
@@ -330,12 +330,17 @@ void ServerWorld::submit_new_chunks(const std::string& uuid,
|
||||
[&dist2](const auto& a, const auto& b) {
|
||||
return dist2(a.first) < dist2(b.first);
|
||||
});
|
||||
for (auto& [pos, task] : tasks) {
|
||||
|
||||
pool_ptr->enqueue([this, chunk = std::move(task->chunk)]() mutable {
|
||||
chunk->gen_chunk();
|
||||
m_finished_queue.push(std::move(chunk));
|
||||
});
|
||||
const int CHUNKS_PER_PRIORITY = m_gen_pool_threads;
|
||||
|
||||
for (size_t i = 0; i < tasks.size(); ++i) {
|
||||
int priority = 10 + static_cast<int>(i / CHUNKS_PER_PRIORITY);
|
||||
auto* task = tasks[i].second;
|
||||
pool_ptr->enqueue(priority,
|
||||
[this, chunk = std::move(task->chunk)]() mutable {
|
||||
chunk->gen_chunk();
|
||||
m_finished_queue.push(std::move(chunk));
|
||||
});
|
||||
}
|
||||
} break;
|
||||
}
|
||||
@@ -777,6 +782,20 @@ int ServerWorld::change_pool_threads(
|
||||
return used_thread;
|
||||
}
|
||||
|
||||
int ServerWorld::change_pool_threads(
|
||||
std::atomic<std::shared_ptr<PriorityThreadPool>>& 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 = 1;
|
||||
}
|
||||
int used_thread = std::clamp(threads, 1, m_max_threads.load());
|
||||
Logger::info("Create New Thread Pool Use {} Threads", used_thread);
|
||||
thread_pool.store(std::make_shared<PriorityThreadPool>(used_thread));
|
||||
return used_thread;
|
||||
}
|
||||
|
||||
void ServerWorld::send_server_stop() {
|
||||
Arena arena;
|
||||
auto* rsp = Arena::Create<LogoutRsp>(&arena);
|
||||
|
||||
Reference in New Issue
Block a user