Small refactor: main start/cleanup threads logic (#2142)

This commit is contained in:
Quentin 2023-09-18 23:13:46 +02:00 committed by GitHub
parent 347f33fb2d
commit 5259d249f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 32 deletions

View File

@ -4,19 +4,16 @@
namespace big namespace big
{ {
void menu_settings::destroy()
{
m_running = false;
}
void menu_settings::init(const file& save_file) void menu_settings::init(const file& save_file)
{ {
m_running = true;
m_save_file = save_file; m_save_file = save_file;
load(); load();
g_thread_pool->push([this] { g_thread_pool->push([this] {
while (m_running) while (!g_running)
std::this_thread::yield();
while (g_running)
{ {
std::this_thread::sleep_for(100ms); std::this_thread::sleep_for(100ms);
attempt_save(); attempt_save();

View File

@ -47,7 +47,6 @@ namespace big
class menu_settings class menu_settings
{ {
public: public:
void destroy();
void init(const file& save_file); void init(const file& save_file);
void attempt_save(); void attempt_save();
@ -59,8 +58,6 @@ namespace big
bool save(); bool save();
private: private:
bool m_running;
file m_save_file; file m_save_file;
nlohmann::json m_default_options; nlohmann::json m_default_options;

View File

@ -142,13 +142,10 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
g_running = true; g_running = true;
// start update loop after setting g_running to true to prevent it from exiting instantly
g_player_database_service->start_update_loop();
while (g_running) while (g_running)
std::this_thread::sleep_for(500ms); std::this_thread::sleep_for(500ms);
g_script_mgr.remove_all_scripts(); g_script_mgr.remove_all_scripts();
LOG(INFO) << "Scripts unregistered."; LOG(INFO) << "Scripts unregistered.";
lua_manager_instance.reset(); lua_manager_instance.reset();
@ -160,9 +157,6 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
native_hooks_instance.reset(); native_hooks_instance.reset();
LOG(INFO) << "Dynamic native hooker uninitialized."; LOG(INFO) << "Dynamic native hooker uninitialized.";
// cleans up the thread responsible for saving settings
g.destroy();
// Make sure that all threads created don't have any blocking loops // Make sure that all threads created don't have any blocking loops
// otherwise make sure that they have stopped executing // otherwise make sure that they have stopped executing
thread_pool_instance->destroy(); thread_pool_instance->destroy();
@ -200,8 +194,8 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
LOG(INFO) << "Custom Text Service reset."; LOG(INFO) << "Custom Text Service reset.";
context_menu_service_instance.reset(); context_menu_service_instance.reset();
LOG(INFO) << "Context Service reset."; LOG(INFO) << "Context Service reset.";
xml_vehicles_service_instance.reset(); xml_vehicles_service_instance.reset();
LOG(INFO) << "Xml Vehicles Service reset."; LOG(INFO) << "Xml Vehicles Service reset.";
LOG(INFO) << "Services uninitialized."; LOG(INFO) << "Services uninitialized.";
hooking_instance.reset(); hooking_instance.reset();

View File

@ -138,6 +138,9 @@ namespace big
m_file_path(g_file_manager.get_project_file("./players.json").get_path()) m_file_path(g_file_manager.get_project_file("./players.json").get_path())
{ {
load(); load();
start_update_loop();
g_player_database_service = this; g_player_database_service = this;
} }
@ -270,22 +273,26 @@ namespace big
void player_database_service::start_update_loop() void player_database_service::start_update_loop()
{ {
if (!g.player_db.update_player_online_states)
return;
g_thread_pool->push([this] { g_thread_pool->push([this] {
while (!g_running)
std::this_thread::yield();
static auto last_update = std::chrono::high_resolution_clock::now() - 45s; static auto last_update = std::chrono::high_resolution_clock::now() - 45s;
while (g_running && g.player_db.update_player_online_states)
while (g_running)
{ {
const auto cur = std::chrono::high_resolution_clock::now(); if (g.player_db.update_player_online_states)
if (cur - last_update > 45s && !updating)
{ {
updating = true; const auto cur = std::chrono::high_resolution_clock::now();
g_fiber_pool->queue_job([this] { if (cur - last_update > 45s && !updating)
update_player_states(true); {
updating = false; updating = true;
last_update = std::chrono::high_resolution_clock::now(); g_fiber_pool->queue_job([this] {
}); update_player_states(true);
updating = false;
last_update = std::chrono::high_resolution_clock::now();
});
}
} }
std::this_thread::sleep_for(1s); std::this_thread::sleep_for(1s);

View File

@ -540,10 +540,12 @@ namespace big::ped
inline void set_ped_random_component_variation(Ped ped) inline void set_ped_random_component_variation(Ped ped)
{ {
auto range = [](int lower_bound, int upper_bound) -> int { constexpr auto range = [](int lower_bound, int upper_bound) -> int {
return std::rand() % (upper_bound - lower_bound + 1) + lower_bound; return std::rand() % (upper_bound - lower_bound + 1) + lower_bound;
}; };
outfit::components_t components; outfit::components_t components;
for (auto& item : components.items) for (auto& item : components.items)
{ {
int drawable_id_max = PED::GET_NUMBER_OF_PED_DRAWABLE_VARIATIONS(ped, item.id) - 1; int drawable_id_max = PED::GET_NUMBER_OF_PED_DRAWABLE_VARIATIONS(ped, item.id) - 1;