mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-22 10:37:00 +08:00
refactor(world): add thread pool start/stop and auto-detect threads
Extract thread pool starting and stopping into dedicated methods. Set default pool threads to 0 to enable automatic detection of available cores, and ensure thread pool is properly managed during world rebuild.
This commit is contained in:
@@ -84,7 +84,7 @@ private:
|
|||||||
std::atomic<bool> m_tick_running{true};
|
std::atomic<bool> m_tick_running{true};
|
||||||
std::atomic<int> m_rendering_distance{24};
|
std::atomic<int> m_rendering_distance{24};
|
||||||
std::atomic<float> m_chunk_gen_fraction{0.0f};
|
std::atomic<float> m_chunk_gen_fraction{0.0f};
|
||||||
std::atomic<int> m_pool_threads{1};
|
std::atomic<int> m_pool_threads{0};
|
||||||
std::atomic<int> m_max_threads{1};
|
std::atomic<int> m_max_threads{1};
|
||||||
std::atomic<TickType> m_game_ticks{0};
|
std::atomic<TickType> m_game_ticks{0};
|
||||||
std::atomic<ChunkLoadStyle> m_chunk_load_style{ChunkLoadStyle::RANDOM};
|
std::atomic<ChunkLoadStyle> m_chunk_load_style{ChunkLoadStyle::RANDOM};
|
||||||
@@ -146,6 +146,8 @@ public:
|
|||||||
void start_server_thread();
|
void start_server_thread();
|
||||||
void stop_gen_thread();
|
void stop_gen_thread();
|
||||||
void stop_server_thread();
|
void stop_server_thread();
|
||||||
|
void stop_thread_pool();
|
||||||
|
void start_thread_pool();
|
||||||
void serever_run(std::stop_token stoken);
|
void serever_run(std::stop_token stoken);
|
||||||
|
|
||||||
CaveCarver& cave_carcer();
|
CaveCarver& cave_carcer();
|
||||||
|
|||||||
@@ -23,11 +23,7 @@ World::~World() {
|
|||||||
stop_gen_thread();
|
stop_gen_thread();
|
||||||
stop_server_thread();
|
stop_server_thread();
|
||||||
wait_all_chunk_tasks();
|
wait_all_chunk_tasks();
|
||||||
auto pool_ptr = m_gen_thread_pool.load();
|
stop_thread_pool();
|
||||||
if (pool_ptr) {
|
|
||||||
pool_ptr->stop();
|
|
||||||
}
|
|
||||||
m_gen_thread_pool.store(nullptr);
|
|
||||||
|
|
||||||
m_chunks.clear();
|
m_chunks.clear();
|
||||||
{
|
{
|
||||||
@@ -90,8 +86,7 @@ void World::init_world() {
|
|||||||
m_cave_carcer.init(ChunkGenerator::seed());
|
m_cave_carcer.init(ChunkGenerator::seed());
|
||||||
m_river_worm.init(ChunkGenerator::seed());
|
m_river_worm.init(ChunkGenerator::seed());
|
||||||
m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE * 4);
|
m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE * 4);
|
||||||
int max_thread = std::thread::hardware_concurrency();
|
start_thread_pool();
|
||||||
change_pool_threads(max_thread - RESERVED_THREADS);
|
|
||||||
|
|
||||||
auto t1 = std::chrono::system_clock::now();
|
auto t1 = std::chrono::system_clock::now();
|
||||||
|
|
||||||
@@ -139,11 +134,6 @@ void World::gen_chunks_internal() {
|
|||||||
// Logger::info("gen_chunks_internal");
|
// Logger::info("gen_chunks_internal");
|
||||||
m_chunk_gen_fraction = 0.0f;
|
m_chunk_gen_fraction = 0.0f;
|
||||||
m_chunk_gen_finished = false;
|
m_chunk_gen_finished = false;
|
||||||
/*
|
|
||||||
if (!new_chunks.empty()) {
|
|
||||||
submit_new_chunks();
|
|
||||||
return;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
ChunkPosSet required_chunks;
|
ChunkPosSet required_chunks;
|
||||||
ChunkPairVector temp_neighbor;
|
ChunkPairVector temp_neighbor;
|
||||||
@@ -366,6 +356,24 @@ void World::stop_server_thread() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void World::stop_thread_pool() {
|
||||||
|
auto pool_ptr = m_gen_thread_pool.load();
|
||||||
|
if (pool_ptr) {
|
||||||
|
pool_ptr->stop();
|
||||||
|
}
|
||||||
|
m_gen_thread_pool.store(nullptr);
|
||||||
|
Logger::info("Thread Pool Stopped");
|
||||||
|
}
|
||||||
|
|
||||||
|
void World::start_thread_pool() {
|
||||||
|
int max_thread = std::thread::hardware_concurrency();
|
||||||
|
if (m_pool_threads == 0) {
|
||||||
|
change_pool_threads(max_thread - RESERVED_THREADS);
|
||||||
|
} else {
|
||||||
|
change_pool_threads(m_pool_threads);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void World::serever_run(std::stop_token stoken) {
|
void World::serever_run(std::stop_token stoken) {
|
||||||
Logger::info("Server Thread Started!");
|
Logger::info("Server Thread Started!");
|
||||||
while (!stoken.stop_requested()) {
|
while (!stoken.stop_requested()) {
|
||||||
@@ -616,6 +624,7 @@ void World::rebuild_world() {
|
|||||||
}
|
}
|
||||||
m_is_rebuilding = true;
|
m_is_rebuilding = true;
|
||||||
stop_gen_thread();
|
stop_gen_thread();
|
||||||
|
stop_thread_pool();
|
||||||
m_cave_carcer.reload(ChunkGenerator::seed());
|
m_cave_carcer.reload(ChunkGenerator::seed());
|
||||||
m_river_worm.reload(ChunkGenerator::seed());
|
m_river_worm.reload(ChunkGenerator::seed());
|
||||||
{
|
{
|
||||||
@@ -625,6 +634,7 @@ void World::rebuild_world() {
|
|||||||
}
|
}
|
||||||
m_could_gen = true;
|
m_could_gen = true;
|
||||||
ChunkGenerator::reload();
|
ChunkGenerator::reload();
|
||||||
|
start_thread_pool();
|
||||||
start_gen_thread();
|
start_gen_thread();
|
||||||
need_gen();
|
need_gen();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user