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 548c9d80a3
commit 0813fa50d8
5 changed files with 29 additions and 32 deletions
+4 -7
View File
@@ -4,19 +4,16 @@
namespace big
{
void menu_settings::destroy()
{
m_running = false;
}
void menu_settings::init(const file& save_file)
{
m_running = true;
m_save_file = save_file;
load();
g_thread_pool->push([this] {
while (m_running)
while (!g_running)
std::this_thread::yield();
while (g_running)
{
std::this_thread::sleep_for(100ms);
attempt_save();
-3
View File
@@ -47,7 +47,6 @@ namespace big
class menu_settings
{
public:
void destroy();
void init(const file& save_file);
void attempt_save();
@@ -59,8 +58,6 @@ namespace big
bool save();
private:
bool m_running;
file m_save_file;
nlohmann::json m_default_options;
+3 -9
View File
@@ -142,13 +142,10 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
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)
std::this_thread::sleep_for(500ms);
g_script_mgr.remove_all_scripts();
g_script_mgr.remove_all_scripts();
LOG(INFO) << "Scripts unregistered.";
lua_manager_instance.reset();
@@ -160,9 +157,6 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
native_hooks_instance.reset();
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
// otherwise make sure that they have stopped executing
thread_pool_instance->destroy();
@@ -200,8 +194,8 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
LOG(INFO) << "Custom Text Service reset.";
context_menu_service_instance.reset();
LOG(INFO) << "Context Service reset.";
xml_vehicles_service_instance.reset();
LOG(INFO) << "Xml Vehicles Service reset.";
xml_vehicles_service_instance.reset();
LOG(INFO) << "Xml Vehicles Service reset.";
LOG(INFO) << "Services uninitialized.";
hooking_instance.reset();
@@ -138,6 +138,9 @@ namespace big
m_file_path(g_file_manager.get_project_file("./players.json").get_path())
{
load();
start_update_loop();
g_player_database_service = this;
}
@@ -270,22 +273,26 @@ namespace big
void player_database_service::start_update_loop()
{
if (!g.player_db.update_player_online_states)
return;
g_thread_pool->push([this] {
while (!g_running)
std::this_thread::yield();
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 (cur - last_update > 45s && !updating)
if (g.player_db.update_player_online_states)
{
updating = true;
g_fiber_pool->queue_job([this] {
update_player_states(true);
updating = false;
last_update = std::chrono::high_resolution_clock::now();
});
const auto cur = std::chrono::high_resolution_clock::now();
if (cur - last_update > 45s && !updating)
{
updating = true;
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);
+3 -1
View File
@@ -540,10 +540,12 @@ namespace big::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;
};
outfit::components_t components;
for (auto& item : components.items)
{
int drawable_id_max = PED::GET_NUMBER_OF_PED_DRAWABLE_VARIATIONS(ped, item.id) - 1;