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

Properly shutdown battle thread in CGameHandler destructor

This commit is contained in:
Adriankhl 2023-03-25 20:29:33 +01:00
parent 5288e3761e
commit 21a4113fc1
2 changed files with 16 additions and 6 deletions

View File

@ -51,6 +51,8 @@
#include "../lib/serializer/Cast.h"
#include "../lib/serializer/JsonSerializer.h"
#include "../lib/ScriptHandler.h"
#include "vstd/CLoggerBase.h"
#include <memory>
#include <vcmi/events/EventBus.h>
#include <vcmi/events/GenericEvents.h>
#include <vcmi/events/AdventureEvents.h>
@ -1659,6 +1661,12 @@ CGameHandler::CGameHandler(CVCMIServer * lobby)
CGameHandler::~CGameHandler()
{
if (battleThread)
{
//Setting battleMadeAction is needed because battleThread waits for the action to continue the main loop
battleMadeAction.setn(true);
battleThread->join();
}
delete spellEnv;
delete gs;
}
@ -2700,7 +2708,7 @@ void CGameHandler::startBattlePrimary(const CArmedInstance *army1, const CArmedI
auto battleQuery = std::make_shared<CBattleQuery>(this, gs->curB);
queries.addQuery(battleQuery);
boost::thread(&CGameHandler::runBattle, this);
this->battleThread = std::make_unique<boost::thread>(boost::thread(&CGameHandler::runBattle, this));
}
void CGameHandler::startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, bool creatureBank)
@ -6412,7 +6420,7 @@ void CGameHandler::runBattle()
//tactic round
{
while (gs->curB->tacticDistance && !battleResult.get())
while ((lobby->state != EServerState::SHUTDOWN) && gs->curB->tacticDistance && !battleResult.get())
boost::this_thread::sleep(boost::posix_time::milliseconds(50));
}
@ -6490,7 +6498,7 @@ void CGameHandler::runBattle()
bool firstRound = true;//FIXME: why first round is -1?
//main loop
while (!battleResult.get()) //till the end of the battle ;]
while ((lobby->state != EServerState::SHUTDOWN) && !battleResult.get()) //till the end of the battle ;]
{
BattleNextRound bnr;
bnr.round = gs->curB->round + 1;
@ -6555,7 +6563,7 @@ void CGameHandler::runBattle()
};
const CStack * next = nullptr;
while((next = getNextStack()))
while((lobby->state != EServerState::SHUTDOWN) && (next = getNextStack()))
{
BattleUnitsChanged removeGhosts;
for(auto stack : curB.stacks)
@ -6734,7 +6742,7 @@ void CGameHandler::runBattle()
boost::unique_lock<boost::mutex> lock(battleMadeAction.mx);
battleMadeAction.data = false;
while (!actionWasMade())
while ((lobby->state != EServerState::SHUTDOWN) && !actionWasMade())
{
battleMadeAction.cond.wait(lock);
if (battleGetStackByID(nextId, false) != next)
@ -6790,7 +6798,8 @@ void CGameHandler::runBattle()
firstRound = false;
}
endBattle(gs->curB->tile, gs->curB->battleGetFightingHero(0), gs->curB->battleGetFightingHero(1));
if (lobby->state != EServerState::SHUTDOWN)
endBattle(gs->curB->tile, gs->curB->battleGetFightingHero(0), gs->curB->battleGetFightingHero(1));
}
bool CGameHandler::makeAutomaticAction(const CStack *stack, BattleAction &ba)

View File

@ -96,6 +96,7 @@ class CGameHandler : public IGameCallback, public CBattleInfoCallback, public En
{
CVCMIServer * lobby;
std::shared_ptr<CApplier<CBaseForGHApply>> applier;
std::unique_ptr<boost::thread> battleThread;
public:
using FireShieldInfo = std::vector<std::pair<const CStack *, int64_t>>;
//use enums as parameters, because doMove(sth, true, false, true) is not readable