From 86f9df5f684dafb7ab83ae63ae020bbe61c9267f Mon Sep 17 00:00:00 2001 From: AlexVinS Date: Wed, 14 Jun 2017 07:59:41 +0300 Subject: [PATCH] Made gamestate lock static --- AI/VCAI/VCAI.cpp | 6 +++--- CCallback.cpp | 2 +- client/CPlayerInterface.cpp | 2 +- lib/CBattleCallback.cpp | 5 ----- lib/CBattleCallback.h | 1 - lib/CGameState.cpp | 6 +++--- lib/CGameState.h | 2 +- 7 files changed, 9 insertions(+), 15 deletions(-) diff --git a/AI/VCAI/VCAI.cpp b/AI/VCAI/VCAI.cpp index c6ccf916b..c185e70bd 100644 --- a/AI/VCAI/VCAI.cpp +++ b/AI/VCAI/VCAI.cpp @@ -746,7 +746,7 @@ void VCAI::makeTurn() logGlobal->info("Player %d (%s) starting turn", playerID, playerID.getStr()); MAKING_TURN; - boost::shared_lock gsLock(cb->getGsMutex()); + boost::shared_lock gsLock(CGameState::mutex); setThreadName("VCAI::makeTurn"); switch(cb->getDate(Date::DAY_OF_WEEK)) @@ -1688,7 +1688,7 @@ void VCAI::battleEnd(const BattleResult *br) void VCAI::waitTillFree() { - auto unlock = vstd::makeUnlockSharedGuard(cb->getGsMutex()); + auto unlock = vstd::makeUnlockSharedGuard(CGameState::mutex); status.waitTillFree(); } @@ -2787,7 +2787,7 @@ void VCAI::requestActionASAP(std::function whatToDo) { setThreadName("VCAI::requestActionASAP::whatToDo"); SET_GLOBAL_STATE(this); - boost::shared_lock gsLock(cb->getGsMutex()); + boost::shared_lock gsLock(CGameState::mutex); whatToDo(); }); } diff --git a/CCallback.cpp b/CCallback.cpp index dabe050bc..f79649627 100644 --- a/CCallback.cpp +++ b/CCallback.cpp @@ -180,7 +180,7 @@ int CBattleCallback::sendRequest(const CPack *request) if(waitTillRealize) { logGlobal->traceStream() << boost::format("We'll wait till request %d is answered.\n") % requestID; - auto gsUnlocker = vstd::makeUnlockSharedGuardIf(getGsMutex(), unlockGsWhenWaiting); + auto gsUnlocker = vstd::makeUnlockSharedGuardIf(CGameState::mutex, unlockGsWhenWaiting); cl->waitingRequest.waitWhileContains(requestID); } diff --git a/client/CPlayerInterface.cpp b/client/CPlayerInterface.cpp index f0487d1d2..cb061839b 100644 --- a/client/CPlayerInterface.cpp +++ b/client/CPlayerInterface.cpp @@ -1634,7 +1634,7 @@ void CPlayerInterface::setSelection(const CArmedInstance * obj) void CPlayerInterface::update() { // Make sure that gamestate won't change when GUI objects may obtain its parts on event processing or drawing request - boost::shared_lock gsLock(cb->getGsMutex()); + boost::shared_lock gsLock(CGameState::mutex); // While mutexes were locked away we may be have stopped being the active interface if (LOCPLINT != this) diff --git a/lib/CBattleCallback.cpp b/lib/CBattleCallback.cpp index fc5021e23..11b9f5cfc 100644 --- a/lib/CBattleCallback.cpp +++ b/lib/CBattleCallback.cpp @@ -100,11 +100,6 @@ namespace SiegeStuffThatShouldBeMovedToHandlers // <=== TODO using namespace SiegeStuffThatShouldBeMovedToHandlers; -boost::shared_mutex& CCallbackBase::getGsMutex() -{ - return *gs->mx; -} - bool CCallbackBase::duringBattle() const { return getBattle() != nullptr; diff --git a/lib/CBattleCallback.h b/lib/CBattleCallback.h index b1e45ca59..7ae560afe 100644 --- a/lib/CBattleCallback.h +++ b/lib/CBattleCallback.h @@ -62,7 +62,6 @@ protected: bool duringBattle() const; public: - boost::shared_mutex &getGsMutex(); //just return a reference to mutex, does not lock nor anything boost::optional getPlayerID() const; friend class CBattleInfoEssentials; diff --git a/lib/CGameState.cpp b/lib/CGameState.cpp index 2a9dfcf89..a1b8a6179 100644 --- a/lib/CGameState.cpp +++ b/lib/CGameState.cpp @@ -45,6 +45,8 @@ * */ +boost::shared_mutex CGameState::mutex; + template class CApplyOnGS; class CBaseForGSApply @@ -65,7 +67,7 @@ public: { T *ptr = static_cast(pack); - boost::unique_lock lock(*gs->mx); + boost::unique_lock lock(CGameState::mutex); ptr->applyGs(gs); } }; @@ -675,7 +677,6 @@ int CGameState::getDate(Date::EDateType mode) const CGameState::CGameState() { gs = this; - mx = new boost::shared_mutex(); applierGs = new CApplier; registerTypesClientPacks1(*applierGs); registerTypesClientPacks2(*applierGs); @@ -687,7 +688,6 @@ CGameState::CGameState() CGameState::~CGameState() { - //delete mx;//TODO: crash on Linux (mutex must be unlocked before destruction) map.dellNull(); curB.dellNull(); //delete scenarioOps; //TODO: fix for loading ind delete diff --git a/lib/CGameState.h b/lib/CGameState.h index 61eafd9f5..638c0d847 100644 --- a/lib/CGameState.h +++ b/lib/CGameState.h @@ -213,7 +213,7 @@ public: CBonusSystemNode globalEffects; RumorState rumor; - boost::shared_mutex *mx; + static boost::shared_mutex mutex; void giveHeroArtifact(CGHeroInstance *h, ArtifactID aid);