From bdf67845295bb96dddb5df0285890e82a5f22e14 Mon Sep 17 00:00:00 2001 From: FutureRave Date: Mon, 15 May 2023 22:00:13 +0100 Subject: [PATCH] thread safety is important --- src/client/component/game_event.cpp | 34 ++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/client/component/game_event.cpp b/src/client/component/game_event.cpp index 27d4370d..2925cfd5 100644 --- a/src/client/component/game_event.cpp +++ b/src/client/component/game_event.cpp @@ -4,31 +4,39 @@ #include "game_event.hpp" +#include #include namespace game_event { namespace { - std::vector> g_init_game_tasks; - std::vector> g_shutdown_game_tasks; + using event_task = std::vector>; + utils::concurrency::container g_init_game_tasks; + utils::concurrency::container g_shutdown_game_tasks; void rope_init_ropes_stub() { - for (const auto& func : g_init_game_tasks) + g_init_game_tasks.access([](event_task& tasks) { - func(); - } + for (const auto& func : tasks) + { + func(); + } + }); game::Rope_InitRopes(); } void mantle_shutdown_anims_stub() { - for (const auto& func : g_shutdown_game_tasks) + g_shutdown_game_tasks.access([](event_task& tasks) { - func(); - } + for (const auto& func : tasks) + { + func(); + } + }); game::Mantle_ShutdownAnims(); } @@ -36,12 +44,18 @@ namespace game_event void on_g_init_game(const std::function& callback) { - g_init_game_tasks.emplace_back(callback); + g_init_game_tasks.access([&callback](event_task& tasks) + { + tasks.emplace_back(callback); + }); } void on_g_shutdown_game(const std::function& callback) { - g_shutdown_game_tasks.emplace_back(callback); + g_shutdown_game_tasks.access([&callback](event_task& tasks) + { + tasks.emplace_back(callback); + }); } class component final : public generic_component