mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(tools): add RecentQueue to replace std::deque in server_world
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
#include "Cubed/gameplay/river_worm.hpp"
|
#include "Cubed/gameplay/river_worm.hpp"
|
||||||
#include "Cubed/gameplay/server_chunk.hpp"
|
#include "Cubed/gameplay/server_chunk.hpp"
|
||||||
#include "Cubed/gameplay/server_player.hpp"
|
#include "Cubed/gameplay/server_player.hpp"
|
||||||
|
#include "Cubed/tools/recent_queue.hpp"
|
||||||
#include "Cubed/tools/thread_pool.hpp"
|
#include "Cubed/tools/thread_pool.hpp"
|
||||||
#include "world/block_change.pb.h"
|
#include "world/block_change.pb.h"
|
||||||
|
|
||||||
@@ -128,7 +129,7 @@ private:
|
|||||||
std::mutex m_need_gen_queue_mutex;
|
std::mutex m_need_gen_queue_mutex;
|
||||||
std::condition_variable_any m_gen_cv;
|
std::condition_variable_any m_gen_cv;
|
||||||
|
|
||||||
std::deque<std::string> m_need_gen_queue;
|
RecentQueue<std::string> m_need_gen_queue;
|
||||||
|
|
||||||
std::atomic<std::shared_ptr<ThreadPool>> m_gen_thread_pool;
|
std::atomic<std::shared_ptr<ThreadPool>> m_gen_thread_pool;
|
||||||
|
|
||||||
|
|||||||
55
include/Cubed/tools/recent_queue.hpp
Normal file
55
include/Cubed/tools/recent_queue.hpp
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <list>
|
||||||
|
#include <unordered_map>
|
||||||
|
namespace Cubed {
|
||||||
|
template <typename T> class RecentQueue {
|
||||||
|
private:
|
||||||
|
std::list<T> m_list;
|
||||||
|
std::unordered_map<T, typename std::list<T>::iterator> m_map;
|
||||||
|
|
||||||
|
public:
|
||||||
|
void enqueue(T key) {
|
||||||
|
auto it = m_map.find(key);
|
||||||
|
if (it != m_map.end()) {
|
||||||
|
m_list.splice(m_list.end(), m_list, it->second);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_list.emplace_back(std::move(key));
|
||||||
|
auto iter = std::prev(m_list.end());
|
||||||
|
m_map.emplace(*iter, iter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pop() {
|
||||||
|
if (m_list.empty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_map.erase(m_list.front());
|
||||||
|
m_list.pop_front();
|
||||||
|
}
|
||||||
|
void clear() {
|
||||||
|
m_list.clear();
|
||||||
|
m_map.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& front() const {
|
||||||
|
assert(!empty());
|
||||||
|
return m_list.front();
|
||||||
|
}
|
||||||
|
const T& back() const {
|
||||||
|
assert(!empty());
|
||||||
|
return m_list.back();
|
||||||
|
}
|
||||||
|
[[nodiscard]]
|
||||||
|
bool empty() const {
|
||||||
|
return m_list.empty();
|
||||||
|
}
|
||||||
|
[[nodiscard]]
|
||||||
|
size_t size() const {
|
||||||
|
return m_list.size();
|
||||||
|
}
|
||||||
|
[[nodiscard]]
|
||||||
|
bool contains(const T& key) const {
|
||||||
|
return m_map.find(key) != m_map.end();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace Cubed
|
||||||
@@ -249,7 +249,7 @@ void ServerWorld::start_gen_thread() {
|
|||||||
std::optional<std::string> uuid{std::nullopt};
|
std::optional<std::string> uuid{std::nullopt};
|
||||||
if (!m_need_gen_queue.empty()) {
|
if (!m_need_gen_queue.empty()) {
|
||||||
uuid = m_need_gen_queue.front();
|
uuid = m_need_gen_queue.front();
|
||||||
m_need_gen_queue.pop_front();
|
m_need_gen_queue.pop();
|
||||||
}
|
}
|
||||||
lk.unlock();
|
lk.unlock();
|
||||||
gen_chunks_internal(uuid);
|
gen_chunks_internal(uuid);
|
||||||
@@ -327,7 +327,7 @@ void ServerWorld::need_gen(std::optional<std::string> uuid) {
|
|||||||
|
|
||||||
if (uuid) {
|
if (uuid) {
|
||||||
std::lock_guard lock(m_need_gen_queue_mutex);
|
std::lock_guard lock(m_need_gen_queue_mutex);
|
||||||
m_need_gen_queue.push_back(*uuid);
|
m_need_gen_queue.enqueue(*uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
// m_gen_player_pos = get_player("TestPlayer").get_player_pos();
|
// m_gen_player_pos = get_player("TestPlayer").get_player_pos();
|
||||||
|
|||||||
Reference in New Issue
Block a user