1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Merge pull request #2406 from IvanSavenko/fix_mutex

Fix game hang/crash on locking mutex
This commit is contained in:
Ivan Savenko 2023-07-25 18:15:37 +03:00 committed by GitHub
commit eccd8590f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 5 deletions

View File

@ -59,7 +59,8 @@ class CBattleAI : public CBattleGameInterface
std::shared_ptr<Environment> env;
//Previous setting of cb
bool wasWaitingForRealize, wasUnlockingGs;
bool wasWaitingForRealize;
bool wasUnlockingGs;
int movesSkippedByDefense;
public:

View File

@ -18,14 +18,21 @@ static std::shared_ptr<CBattleCallback> cbc;
CStupidAI::CStupidAI()
: side(-1)
, wasWaitingForRealize(false)
, wasUnlockingGs(false)
{
print("created");
}
CStupidAI::~CStupidAI()
{
print("destroyed");
if(cb)
{
//Restore previous state of CB - it may be shared with the main AI (like VCAI)
cb->waitTillRealize = wasWaitingForRealize;
cb->unlockGsWhenWaiting = wasUnlockingGs;
}
}
void CStupidAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB)
@ -33,6 +40,11 @@ void CStupidAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::share
print("init called, saving ptr to IBattleCallback");
env = ENV;
cbc = cb = CB;
wasWaitingForRealize = CB->waitTillRealize;
wasUnlockingGs = CB->unlockGsWhenWaiting;
CB->waitTillRealize = false;
CB->unlockGsWhenWaiting = false;
}
void CStupidAI::actionFinished(const BattleAction &action)

View File

@ -20,6 +20,9 @@ class CStupidAI : public CBattleGameInterface
std::shared_ptr<CBattleCallback> cb;
std::shared_ptr<Environment> env;
bool wasWaitingForRealize;
bool wasUnlockingGs;
void print(const std::string &text) const;
public:
CStupidAI();

View File

@ -70,13 +70,22 @@ void InputHandler::handleCurrentEvent(const SDL_Event & current)
}
}
void InputHandler::processEvents()
std::vector<SDL_Event> InputHandler::acquireEvents()
{
boost::unique_lock<boost::mutex> lock(eventsMutex);
for(const auto & currentEvent : eventsQueue)
std::vector<SDL_Event> result;
std::swap(result, eventsQueue);
return result;
}
void InputHandler::processEvents()
{
std::vector<SDL_Event> eventsToProcess = acquireEvents();
for(const auto & currentEvent : eventsToProcess)
handleCurrentEvent(currentEvent);
eventsQueue.clear();
fingerHandler->handleUpdate();
}

View File

@ -29,6 +29,8 @@ class InputHandler
Point cursorPosition;
std::vector<SDL_Event> acquireEvents();
void preprocessEvent(const SDL_Event & event);
void handleCurrentEvent(const SDL_Event & current);
void handleUserEvent(const SDL_UserEvent & current);