diff --git a/include/Cubed/gameplay/server_world.hpp b/include/Cubed/gameplay/server_world.hpp index d0f9758..0baea37 100644 --- a/include/Cubed/gameplay/server_world.hpp +++ b/include/Cubed/gameplay/server_world.hpp @@ -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 m_need_gen_queue; - std::atomic> m_gen_thread_pool; + std::atomic> m_gen_thread_pool; std::atomic> m_net_thread_pool; std::atomic m_chunk_load_style{ChunkLoadStyle::CENTER}; @@ -178,6 +179,9 @@ private: int change_pool_threads(std::atomic>& thread_pool, int threads); + int change_pool_threads( + std::atomic>& thread_pool, + int threads); void send_server_stop(); }; } // namespace Cubed diff --git a/src/gameplay/server_world.cpp b/src/gameplay/server_world.cpp index c3e10cb..cbee30f 100644 --- a/src/gameplay/server_world.cpp +++ b/src/gameplay/server_world.cpp @@ -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(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>& 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(used_thread)); + return used_thread; +} + void ServerWorld::send_server_stop() { Arena arena; auto* rsp = Arena::Create(&arena);