1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Fix potential concurrenccy issues

This commit is contained in:
nordsoft 2023-08-28 01:10:04 +04:00
parent 2022d55d40
commit 6226ddf4a7
2 changed files with 10 additions and 0 deletions

View File

@ -32,6 +32,7 @@ void TurnTimerHandler::onGameplayStart(PlayerColor player)
{
if(si->turnTimerInfo.isEnabled())
{
std::lock_guard<std::recursive_mutex> guard(mx);
timers[player] = si->turnTimerInfo;
timers[player].turnTimer = 0;
}
@ -44,6 +45,7 @@ void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
{
if(si->turnTimerInfo.isEnabled())
{
std::lock_guard<std::recursive_mutex> guard(mx);
timers[player].baseTimer += timers[player].turnTimer;
timers[player].turnTimer = si->turnTimerInfo.turnTimer;
@ -66,6 +68,7 @@ void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
if(state.human && si->turnTimerInfo.isEnabled() && !gs->curB)
{
std::lock_guard<std::recursive_mutex> guard(mx);
if(timers[player].turnTimer > 0)
{
timers[player].turnTimer -= waitTime;
@ -98,6 +101,8 @@ void TurnTimerHandler::onBattleStart()
if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
return;
std::lock_guard<std::recursive_mutex> guard(mx);
auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
@ -123,6 +128,8 @@ void TurnTimerHandler::onBattleNextStack(const CStack & stack)
if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
return;
std::lock_guard<std::recursive_mutex> guard(mx);
auto player = stack.getOwner();
if(!player.isValidPlayer())
@ -145,6 +152,8 @@ void TurnTimerHandler::onBattleLoop(int waitTime)
if(!si || !gs || !gs->curB)
return;
std::lock_guard<std::recursive_mutex> guard(mx);
const auto * stack = gs->curB.get()->battleGetStackByID(gs->curB->getActiveStackID());
if(!stack || !stack->getOwner().isValidPlayer())
return;

View File

@ -27,6 +27,7 @@ class TurnTimerHandler
const int turnTimePropagateFrequencyCrit = 1000;
const int turnTimePropagateThreshold = 3000;
std::map<PlayerColor, TurnTimerInfo> timers;
std::recursive_mutex mx;
public:
TurnTimerHandler(CGameHandler &);