Преглед изворни кода

Made gamestate lock static

AlexVinS пре 8 година
родитељ
комит
86f9df5f68
7 измењених фајлова са 9 додато и 15 уклоњено
  1. 3 3
      AI/VCAI/VCAI.cpp
  2. 1 1
      CCallback.cpp
  3. 1 1
      client/CPlayerInterface.cpp
  4. 0 5
      lib/CBattleCallback.cpp
  5. 0 1
      lib/CBattleCallback.h
  6. 3 3
      lib/CGameState.cpp
  7. 1 1
      lib/CGameState.h

+ 3 - 3
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<boost::shared_mutex> gsLock(cb->getGsMutex());
+	boost::shared_lock<boost::shared_mutex> 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<void()> whatToDo)
 	{
 		setThreadName("VCAI::requestActionASAP::whatToDo");
 		SET_GLOBAL_STATE(this);
-		boost::shared_lock<boost::shared_mutex> gsLock(cb->getGsMutex());
+		boost::shared_lock<boost::shared_mutex> gsLock(CGameState::mutex);
 		whatToDo();
 	});
 }

+ 1 - 1
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);
 	}
 

+ 1 - 1
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<boost::shared_mutex> gsLock(cb->getGsMutex());
+	boost::shared_lock<boost::shared_mutex> gsLock(CGameState::mutex);
 
 	// While mutexes were locked away we may be have stopped being the active interface
 	if (LOCPLINT != this)

+ 0 - 5
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;

+ 0 - 1
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<PlayerColor> getPlayerID() const;
 
 	friend class CBattleInfoEssentials;

+ 3 - 3
lib/CGameState.cpp

@@ -45,6 +45,8 @@
  *
  */
 
+boost::shared_mutex CGameState::mutex;
+
 template <typename T> class CApplyOnGS;
 
 class CBaseForGSApply
@@ -65,7 +67,7 @@ public:
 	{
 		T *ptr = static_cast<T*>(pack);
 
-		boost::unique_lock<boost::shared_mutex> lock(*gs->mx);
+		boost::unique_lock<boost::shared_mutex> 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<CBaseForGSApply>;
 	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

+ 1 - 1
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);