Browse Source

Replaced boost::thread_specific_ptr with thread_local

Ivan Savenko 2 years ago
parent
commit
87957e74c1
67 changed files with 41 additions and 255 deletions
  1. 10 8
      AI/Nullkiller/AIGateway.cpp
  2. 0 4
      AI/Nullkiller/AIUtility.cpp
  3. 4 2
      AI/Nullkiller/AIUtility.h
  4. 0 3
      AI/Nullkiller/Behaviors/BuildingBehavior.cpp
  5. 0 3
      AI/Nullkiller/Behaviors/BuyArmyBehavior.cpp
  6. 0 3
      AI/Nullkiller/Behaviors/CaptureObjectsBehavior.cpp
  7. 0 3
      AI/Nullkiller/Behaviors/ClusterBehavior.cpp
  8. 0 3
      AI/Nullkiller/Behaviors/DefenceBehavior.cpp
  9. 0 3
      AI/Nullkiller/Behaviors/GatherArmyBehavior.cpp
  10. 0 3
      AI/Nullkiller/Behaviors/RecruitHeroBehavior.cpp
  11. 0 3
      AI/Nullkiller/Behaviors/StartupBehavior.cpp
  12. 0 3
      AI/Nullkiller/Engine/DeepDecomposer.cpp
  13. 0 2
      AI/Nullkiller/Engine/FuzzyEngines.cpp
  14. 1 4
      AI/Nullkiller/Engine/Nullkiller.cpp
  15. 0 3
      AI/Nullkiller/Goals/AbstractGoal.cpp
  16. 0 3
      AI/Nullkiller/Goals/AdventureSpellCast.cpp
  17. 0 3
      AI/Nullkiller/Goals/BuildBoat.cpp
  18. 0 4
      AI/Nullkiller/Goals/BuildThis.cpp
  19. 0 3
      AI/Nullkiller/Goals/BuyArmy.cpp
  20. 0 2
      AI/Nullkiller/Goals/CaptureObject.cpp
  21. 0 3
      AI/Nullkiller/Goals/CompleteQuest.cpp
  22. 0 3
      AI/Nullkiller/Goals/Composition.cpp
  23. 0 3
      AI/Nullkiller/Goals/DigAtTile.cpp
  24. 0 3
      AI/Nullkiller/Goals/DismissHero.cpp
  25. 0 3
      AI/Nullkiller/Goals/ExchangeSwapTownHeroes.cpp
  26. 0 3
      AI/Nullkiller/Goals/ExecuteHeroChain.cpp
  27. 0 3
      AI/Nullkiller/Goals/RecruitHero.cpp
  28. 0 3
      AI/Nullkiller/Goals/SaveResources.cpp
  29. 0 3
      AI/Nullkiller/Markers/ArmyUpgrade.cpp
  30. 0 3
      AI/Nullkiller/Markers/HeroExchange.cpp
  31. 0 3
      AI/Nullkiller/Markers/UnlockCluster.cpp
  32. 0 3
      AI/Nullkiller/Pathfinding/Actions/BattleAction.cpp
  33. 2 5
      AI/Nullkiller/Pathfinding/Actions/BoatActions.cpp
  34. 0 3
      AI/Nullkiller/Pathfinding/Actions/BuyArmyAction.cpp
  35. 0 3
      AI/Nullkiller/Pathfinding/Actions/QuestAction.cpp
  36. 1 4
      AI/Nullkiller/Pathfinding/Actions/TownPortalAction.cpp
  37. 0 4
      AI/VCAI/AIUtility.cpp
  38. 4 2
      AI/VCAI/AIUtility.h
  39. 0 3
      AI/VCAI/FuzzyEngines.cpp
  40. 1 4
      AI/VCAI/FuzzyHelper.cpp
  41. 2 0
      AI/VCAI/FuzzyHelper.h
  42. 0 4
      AI/VCAI/Goals/AbstractGoal.cpp
  43. 0 4
      AI/VCAI/Goals/AdventureSpellCast.cpp
  44. 0 5
      AI/VCAI/Goals/Build.cpp
  45. 0 4
      AI/VCAI/Goals/BuildBoat.cpp
  46. 0 5
      AI/VCAI/Goals/BuildThis.cpp
  47. 0 5
      AI/VCAI/Goals/BuyArmy.cpp
  48. 0 5
      AI/VCAI/Goals/ClearWayTo.cpp
  49. 0 5
      AI/VCAI/Goals/CollectRes.cpp
  50. 0 4
      AI/VCAI/Goals/CompleteQuest.cpp
  51. 0 5
      AI/VCAI/Goals/Conquer.cpp
  52. 0 5
      AI/VCAI/Goals/DigAtTile.cpp
  53. 2 6
      AI/VCAI/Goals/Explore.cpp
  54. 0 4
      AI/VCAI/Goals/FindObj.cpp
  55. 0 5
      AI/VCAI/Goals/GatherArmy.cpp
  56. 0 5
      AI/VCAI/Goals/GatherTroops.cpp
  57. 1 6
      AI/VCAI/Goals/GetArtOfType.cpp
  58. 0 5
      AI/VCAI/Goals/RecruitHero.cpp
  59. 0 4
      AI/VCAI/Goals/VisitHero.cpp
  60. 0 5
      AI/VCAI/Goals/VisitObj.cpp
  61. 0 5
      AI/VCAI/Goals/VisitTile.cpp
  62. 0 5
      AI/VCAI/Goals/Win.cpp
  63. 10 8
      AI/VCAI/VCAI.cpp
  64. 3 1
      client/mainmenu/CMainMenu.cpp
  65. 0 2
      test/CMakeLists.txt
  66. 0 13
      test/vcai/ResourceManagerTest.h
  67. 0 1
      test/vcai/ResurceManagerTest.cpp

+ 10 - 8
AI/Nullkiller/AIGateway.cpp

@@ -34,26 +34,26 @@ const float RETREAT_THRESHOLD = 0.3f;
 const double RETREAT_ABSOLUTE_THRESHOLD = 10000.;
 
 //one thread may be turn of AI and another will be handling a side effect for AI2
-boost::thread_specific_ptr<CCallback> cb;
-boost::thread_specific_ptr<AIGateway> ai;
+thread_local CCallback * cb = nullptr;
+thread_local AIGateway * ai = nullptr;
 
 //helper RAII to manage global ai/cb ptrs
 struct SetGlobalState
 {
 	SetGlobalState(AIGateway * AI)
 	{
-		assert(!ai.get());
-		assert(!cb.get());
+		assert(!ai);
+		assert(!cb);
 
-		ai.reset(AI);
-		cb.reset(AI->myCb.get());
+		ai = AI;
+		cb = AI->myCb.get();
 	}
 	~SetGlobalState()
 	{
 		//TODO: how to handle rm? shouldn't be called after ai is destroyed, hopefully
 		//TODO: to ensure that, make rm unique_ptr
-		ai.release();
-		cb.release();
+		ai = nullptr;
+		cb = nullptr;
 	}
 };
 
@@ -1472,6 +1472,8 @@ void AIGateway::requestActionASAP(std::function<void()> whatToDo)
 		boost::shared_lock<boost::shared_mutex> gsLock(CGameState::mutex);
 		whatToDo();
 	});
+
+	newThread.detach();
 }
 
 void AIGateway::lostHero(HeroPtr h)

+ 0 - 4
AI/Nullkiller/AIUtility.cpp

@@ -25,10 +25,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<AIGateway> ai;
-
-//extern static const int3 dirs[8];
-
 const CGObjectInstance * ObjectIdRef::operator->() const
 {
 	return cb->getObj(id, false);

+ 4 - 2
AI/Nullkiller/AIUtility.h

@@ -57,6 +57,7 @@ using dwellingContent = std::pair<ui32, std::vector<CreatureID>>;
 namespace NKAI
 {
 struct creInfo;
+class AIGateway;
 class Nullkiller;
 
 const int GOLD_MINE_PRODUCTION = 1000, WOOD_ORE_MINE_PRODUCTION = 2, RESOURCE_MINE_PRODUCTION = 1;
@@ -67,7 +68,8 @@ const int ALLOWED_ROAMING_HEROES = 8;
 extern const float SAFE_ATTACK_CONSTANT;
 extern const int GOLD_RESERVE;
 
-extern boost::thread_specific_ptr<CCallback> cb;
+extern thread_local CCallback * cb;
+extern thread_local AIGateway * ai;
 
 enum HeroRole
 {
@@ -201,7 +203,7 @@ void foreach_tile_pos(CCallback * cbp, const Func & foo) // avoid costly retriev
 template<class Func>
 void foreach_neighbour(const int3 & pos, const Func & foo)
 {
-	CCallback * cbp = cb.get(); // avoid costly retrieval of thread-specific pointer
+	CCallback * cbp = cb; // avoid costly retrieval of thread-specific pointer
 	for(const int3 & dir : int3::getDirs())
 	{
 		const int3 n = pos + dir;

+ 0 - 3
AI/Nullkiller/Behaviors/BuildingBehavior.cpp

@@ -20,9 +20,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 std::string BuildingBehavior::toString() const

+ 0 - 3
AI/Nullkiller/Behaviors/BuyArmyBehavior.cpp

@@ -17,9 +17,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 std::string BuyArmyBehavior::toString() const

+ 0 - 3
AI/Nullkiller/Behaviors/CaptureObjectsBehavior.cpp

@@ -19,9 +19,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 template <typename T>

+ 0 - 3
AI/Nullkiller/Behaviors/ClusterBehavior.cpp

@@ -19,9 +19,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 std::string ClusterBehavior::toString() const

+ 0 - 3
AI/Nullkiller/Behaviors/DefenceBehavior.cpp

@@ -25,9 +25,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 const float TREAT_IGNORE_RATIO = 2;
 
 using namespace Goals;

+ 0 - 3
AI/Nullkiller/Behaviors/GatherArmyBehavior.cpp

@@ -23,9 +23,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 std::string GatherArmyBehavior::toString() const

+ 0 - 3
AI/Nullkiller/Behaviors/RecruitHeroBehavior.cpp

@@ -17,9 +17,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 std::string RecruitHeroBehavior::toString() const

+ 0 - 3
AI/Nullkiller/Behaviors/StartupBehavior.cpp

@@ -21,9 +21,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 std::string StartupBehavior::toString() const

+ 0 - 3
AI/Nullkiller/Engine/DeepDecomposer.cpp

@@ -24,9 +24,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 void DeepDecomposer::reset()

+ 0 - 2
AI/Nullkiller/Engine/FuzzyEngines.cpp

@@ -20,8 +20,6 @@ namespace NKAI
 #define MIN_AI_STRENGTH (0.5f) //lower when combat AI gets smarter
 #define UNGUARDED_OBJECT (100.0f) //we consider unguarded objects 100 times weaker than us
 
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 engineBase::engineBase()
 {
 	rules = new fl::RuleBlock();

+ 1 - 4
AI/Nullkiller/Engine/Nullkiller.cpp

@@ -24,9 +24,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 #if NKAI_TRACE_LEVEL >= 1
@@ -341,7 +338,7 @@ void Nullkiller::executeTask(Goals::TTask task)
 
 	try
 	{
-		task->accept(ai.get());
+		task->accept(ai);
 		logAi->trace("Task %s completed in %lld", taskDescr, timeElapsed(start));
 	}
 	catch(goalFulfilledException &)

+ 0 - 3
AI/Nullkiller/Goals/AbstractGoal.cpp

@@ -15,9 +15,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 TSubgoal Goals::sptr(const AbstractGoal & tmp)

+ 0 - 3
AI/Nullkiller/Goals/AdventureSpellCast.cpp

@@ -14,9 +14,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 bool AdventureSpellCast::operator==(const AdventureSpellCast & other) const

+ 0 - 3
AI/Nullkiller/Goals/BuildBoat.cpp

@@ -15,9 +15,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 bool BuildBoat::operator==(const BuildBoat & other) const

+ 0 - 4
AI/Nullkiller/Goals/BuildThis.cpp

@@ -17,12 +17,8 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
-
 BuildThis::BuildThis(BuildingID Bid, const CGTownInstance * tid)
 	: ElementarGoal(Goals::BUILD_STRUCTURE)
 {

+ 0 - 3
AI/Nullkiller/Goals/BuyArmy.cpp

@@ -17,9 +17,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 bool BuyArmy::operator==(const BuyArmy & other) const

+ 0 - 2
AI/Nullkiller/Goals/CaptureObject.cpp

@@ -18,8 +18,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-
 using namespace Goals;
 
 bool CaptureObject::operator==(const CaptureObject & other) const

+ 0 - 3
AI/Nullkiller/Goals/CompleteQuest.cpp

@@ -17,9 +17,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 bool isKeyMaster(const QuestInfo & q)

+ 0 - 3
AI/Nullkiller/Goals/Composition.cpp

@@ -17,9 +17,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 bool Composition::operator==(const Composition & other) const

+ 0 - 3
AI/Nullkiller/Goals/DigAtTile.cpp

@@ -16,9 +16,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 bool DigAtTile::operator==(const DigAtTile & other) const

+ 0 - 3
AI/Nullkiller/Goals/DismissHero.cpp

@@ -14,9 +14,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 bool DismissHero::operator==(const DismissHero & other) const

+ 0 - 3
AI/Nullkiller/Goals/ExchangeSwapTownHeroes.cpp

@@ -16,9 +16,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 ExchangeSwapTownHeroes::ExchangeSwapTownHeroes(

+ 0 - 3
AI/Nullkiller/Goals/ExecuteHeroChain.cpp

@@ -15,9 +15,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 ExecuteHeroChain::ExecuteHeroChain(const AIPath & path, const CGObjectInstance * obj)

+ 0 - 3
AI/Nullkiller/Goals/RecruitHero.cpp

@@ -17,9 +17,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 std::string RecruitHero::toString() const

+ 0 - 3
AI/Nullkiller/Goals/SaveResources.cpp

@@ -15,9 +15,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 bool SaveResources::operator==(const SaveResources & other) const

+ 0 - 3
AI/Nullkiller/Markers/ArmyUpgrade.cpp

@@ -16,9 +16,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 ArmyUpgrade::ArmyUpgrade(const AIPath & upgradePath, const CGObjectInstance * upgrader, const ArmyUpgradeInfo & upgrade)

+ 0 - 3
AI/Nullkiller/Markers/HeroExchange.cpp

@@ -17,9 +17,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 bool HeroExchange::operator==(const HeroExchange & other) const

+ 0 - 3
AI/Nullkiller/Markers/UnlockCluster.cpp

@@ -16,9 +16,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 using namespace Goals;
 
 bool UnlockCluster::operator==(const UnlockCluster & other) const

+ 0 - 3
AI/Nullkiller/Pathfinding/Actions/BattleAction.cpp

@@ -16,9 +16,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 namespace AIPathfinding
 {
 	void BattleAction::execute(const CGHeroInstance * hero) const

+ 2 - 5
AI/Nullkiller/Pathfinding/Actions/BoatActions.cpp

@@ -20,14 +20,11 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 namespace AIPathfinding
 {
 	void BuildBoatAction::execute(const CGHeroInstance * hero) const
 	{
-		return Goals::BuildBoat(shipyard).accept(ai.get());
+		return Goals::BuildBoat(shipyard).accept(ai);
 	}
 
 	Goals::TSubgoal BuildBoatAction::decompose(const CGHeroInstance * hero) const
@@ -80,7 +77,7 @@ namespace AIPathfinding
 
 	void SummonBoatAction::execute(const CGHeroInstance * hero) const
 	{
-		Goals::AdventureSpellCast(hero, SpellID::SUMMON_BOAT).accept(ai.get());
+		Goals::AdventureSpellCast(hero, SpellID::SUMMON_BOAT).accept(ai);
 	}
 
 	const ChainActor * SummonBoatAction::getActor(const ChainActor * sourceActor) const

+ 0 - 3
AI/Nullkiller/Pathfinding/Actions/BuyArmyAction.cpp

@@ -16,9 +16,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 namespace AIPathfinding
 {
 	void BuyArmyAction::execute(const CGHeroInstance * hero) const

+ 0 - 3
AI/Nullkiller/Pathfinding/Actions/QuestAction.cpp

@@ -16,9 +16,6 @@
 namespace NKAI
 {
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 namespace AIPathfinding
 {
 	bool QuestAction::canAct(const AIPathNode * node) const

+ 1 - 4
AI/Nullkiller/Pathfinding/Actions/TownPortalAction.cpp

@@ -18,9 +18,6 @@ namespace NKAI
 
 using namespace AIPathfinding;
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<AIGateway> ai;
-
 void TownPortalAction::execute(const CGHeroInstance * hero) const
 {
 	auto goal = Goals::AdventureSpellCast(hero, SpellID::TOWN_PORTAL);
@@ -28,7 +25,7 @@ void TownPortalAction::execute(const CGHeroInstance * hero) const
 	goal.town = target;
 	goal.tile = target->visitablePos();
 
-	goal.accept(ai.get());
+	goal.accept(ai);
 }
 
 std::string TownPortalAction::toString() const

+ 0 - 4
AI/VCAI/AIUtility.cpp

@@ -21,12 +21,8 @@
 #include "../../lib/mapObjects/CQuest.h"
 #include "../../lib/mapping/CMapDefines.h"
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
 extern FuzzyHelper * fh;
 
-//extern static const int3 dirs[8];
-
 const CGObjectInstance * ObjectIdRef::operator->() const
 {
 	return cb->getObj(id, false);

+ 4 - 2
AI/VCAI/AIUtility.h

@@ -18,6 +18,7 @@
 #include "../../lib/mapObjects/CGHeroInstance.h"
 #include "../../CCallback.h"
 
+class VCAI;
 class CCallback;
 struct creInfo;
 
@@ -33,7 +34,8 @@ const int ALLOWED_ROAMING_HEROES = 8;
 extern const double SAFE_ATTACK_CONSTANT;
 extern const int GOLD_RESERVE;
 
-extern boost::thread_specific_ptr<CCallback> cb;
+extern thread_local CCallback * cb;
+extern thread_local VCAI * ai;
 
 //provisional class for AI to store a reference to an owned hero object
 //checks if it's valid on access, should be used in place of const CGHeroInstance*
@@ -192,7 +194,7 @@ void foreach_tile_pos(CCallback * cbp, const Func & foo) // avoid costly retriev
 template<class Func>
 void foreach_neighbour(const int3 & pos, const Func & foo)
 {
-	CCallback * cbp = cb.get(); // avoid costly retrieval of thread-specific pointer
+	CCallback * cbp = cb; // avoid costly retrieval of thread-specific pointer
 	for(const int3 & dir : int3::getDirs())
 	{
 		const int3 n = pos + dir;

+ 0 - 3
AI/VCAI/FuzzyEngines.cpp

@@ -18,9 +18,6 @@
 #define MIN_AI_STRENGTH (0.5f) //lower when combat AI gets smarter
 #define UNGUARDED_OBJECT (100.0f) //we consider unguarded objects 100 times weaker than us
 
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 engineBase::engineBase()
 {
 	rules = new fl::RuleBlock();

+ 1 - 4
AI/VCAI/FuzzyHelper.cpp

@@ -23,9 +23,6 @@
 
 FuzzyHelper * fh;
 
-extern boost::thread_specific_ptr<VCAI> ai;
-extern boost::thread_specific_ptr<CCallback> cb;
-
 Goals::TSubgoal FuzzyHelper::chooseSolution(Goals::TGoalVec vec)
 {
 	if(vec.empty())
@@ -216,7 +213,7 @@ void FuzzyHelper::setPriority(Goals::TSubgoal & g) //calls evaluate - Visitor pa
 
 ui64 FuzzyHelper::evaluateDanger(crint3 tile, const CGHeroInstance * visitor)
 {
-	return evaluateDanger(tile, visitor, ai.get());
+	return evaluateDanger(tile, visitor, ai);
 }
 
 ui64 FuzzyHelper::evaluateDanger(crint3 tile, const CGHeroInstance * visitor, const VCAI * ai)

+ 2 - 0
AI/VCAI/FuzzyHelper.h

@@ -51,3 +51,5 @@ public:
 	ui64 evaluateDanger(crint3 tile, const CGHeroInstance * visitor, const VCAI * ai);
 	ui64 evaluateDanger(crint3 tile, const CGHeroInstance * visitor);
 };
+
+extern FuzzyHelper * fh;

+ 0 - 4
AI/VCAI/Goals/AbstractGoal.cpp

@@ -16,10 +16,6 @@
 #include "../BuildingManager.h"
 #include "../../../lib/StringConstants.h"
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 TSubgoal Goals::sptr(const AbstractGoal & tmp)

+ 0 - 4
AI/VCAI/Goals/AdventureSpellCast.cpp

@@ -14,10 +14,6 @@
 #include "../AIhelper.h"
 #include "../../../lib/mapObjects/CGTownInstance.h"
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool AdventureSpellCast::operator==(const AdventureSpellCast & other) const

+ 0 - 5
AI/VCAI/Goals/Build.cpp

@@ -19,11 +19,6 @@
 #include "../../../lib/mapObjects/CGTownInstance.h"
 #include "../../../lib/StringConstants.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 TGoalVec Build::getAllPossibleSubgoals()

+ 0 - 4
AI/VCAI/Goals/BuildBoat.cpp

@@ -13,10 +13,6 @@
 #include "../FuzzyHelper.h"
 #include "../AIhelper.h"
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool BuildBoat::operator==(const BuildBoat & other) const

+ 0 - 5
AI/VCAI/Goals/BuildThis.cpp

@@ -18,11 +18,6 @@
 #include "../../../lib/mapObjects/CGTownInstance.h"
 #include "../../../lib/StringConstants.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool BuildThis::operator==(const BuildThis & other) const

+ 0 - 5
AI/VCAI/Goals/BuyArmy.cpp

@@ -13,11 +13,6 @@
 #include "../AIhelper.h"
 #include "../../../lib/mapObjects/CGTownInstance.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool BuyArmy::operator==(const BuyArmy & other) const

+ 0 - 5
AI/VCAI/Goals/ClearWayTo.cpp

@@ -16,11 +16,6 @@
 #include "../FuzzyHelper.h"
 #include "../AIhelper.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool ClearWayTo::operator==(const ClearWayTo & other) const

+ 0 - 5
AI/VCAI/Goals/CollectRes.cpp

@@ -18,11 +18,6 @@
 #include "../../../lib/mapObjects/CGMarket.h"
 #include "../../../lib/StringConstants.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool CollectRes::operator==(const CollectRes & other) const

+ 0 - 4
AI/VCAI/Goals/CompleteQuest.cpp

@@ -14,10 +14,6 @@
 #include "../AIhelper.h"
 #include "../../../lib/mapObjects/CQuest.h"
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool CompleteQuest::operator==(const CompleteQuest & other) const

+ 0 - 5
AI/VCAI/Goals/Conquer.cpp

@@ -17,11 +17,6 @@
 #include "../BuildingManager.h"
 #include "../../../lib/StringConstants.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool Conquer::operator==(const Conquer & other) const

+ 0 - 5
AI/VCAI/Goals/DigAtTile.cpp

@@ -13,11 +13,6 @@
 #include "../VCAI.h"
 #include "../AIUtility.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool DigAtTile::operator==(const DigAtTile & other) const

+ 2 - 6
AI/VCAI/Goals/Explore.cpp

@@ -18,10 +18,6 @@
 #include "../../../lib/StringConstants.h"
 #include "../../../lib/CPlayerState.h"
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 namespace Goals
@@ -41,8 +37,8 @@ namespace Goals
 
 		ExplorationHelper(HeroPtr h, bool gatherArmy)
 		{
-			cbp = cb.get();
-			aip = ai.get();
+			cbp = cb;
+			aip = ai;
 			hero = h;
 			ts = cbp->getPlayerTeam(ai->playerID);
 			sightRadius = hero->getSightRadius();

+ 0 - 4
AI/VCAI/Goals/FindObj.cpp

@@ -14,10 +14,6 @@
 #include "../VCAI.h"
 #include "../AIUtility.h"
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool FindObj::operator==(const FindObj & other) const

+ 0 - 5
AI/VCAI/Goals/GatherArmy.cpp

@@ -18,11 +18,6 @@
 #include "../../../lib/mapObjects/CGTownInstance.h"
 #include "../../../lib/StringConstants.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool GatherArmy::operator==(const GatherArmy & other) const

+ 0 - 5
AI/VCAI/Goals/GatherTroops.cpp

@@ -18,11 +18,6 @@
 #include "../../../lib/mapObjects/CGTownInstance.h"
 #include "../../../lib/StringConstants.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool GatherTroops::operator==(const GatherTroops & other) const

+ 1 - 6
AI/VCAI/Goals/GetArtOfType.cpp

@@ -13,11 +13,6 @@
 #include "../VCAI.h"
 #include "../AIUtility.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool GetArtOfType::operator==(const GetArtOfType & other) const
@@ -28,4 +23,4 @@ bool GetArtOfType::operator==(const GetArtOfType & other) const
 TSubgoal GetArtOfType::whatToDoToAchieve()
 {
 	return sptr(FindObj(Obj::ARTIFACT, aid));
-}
+}

+ 0 - 5
AI/VCAI/Goals/RecruitHero.cpp

@@ -17,11 +17,6 @@
 #include "../BuildingManager.h"
 #include "../../../lib/StringConstants.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 TSubgoal RecruitHero::whatToDoToAchieve()

+ 0 - 4
AI/VCAI/Goals/VisitHero.cpp

@@ -18,10 +18,6 @@
 #include "../ResourceManager.h"
 #include "../BuildingManager.h"
 
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool VisitHero::operator==(const VisitHero & other) const

+ 0 - 5
AI/VCAI/Goals/VisitObj.cpp

@@ -17,11 +17,6 @@
 #include "../BuildingManager.h"
 #include "../../../lib/StringConstants.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool VisitObj::operator==(const VisitObj & other) const

+ 0 - 5
AI/VCAI/Goals/VisitTile.cpp

@@ -17,11 +17,6 @@
 #include "../BuildingManager.h"
 #include "../../../lib/StringConstants.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 bool VisitTile::operator==(const VisitTile & other) const

+ 0 - 5
AI/VCAI/Goals/Win.cpp

@@ -19,11 +19,6 @@
 #include "../../../lib/mapObjects/CGTownInstance.h"
 #include "../../../lib/StringConstants.h"
 
-
-extern boost::thread_specific_ptr<CCallback> cb;
-extern boost::thread_specific_ptr<VCAI> ai;
-extern FuzzyHelper * fh;
-
 using namespace Goals;
 
 TSubgoal Win::whatToDoToAchieve()

+ 10 - 8
AI/VCAI/VCAI.cpp

@@ -38,8 +38,8 @@ extern FuzzyHelper * fh;
 const double SAFE_ATTACK_CONSTANT = 1.5;
 
 //one thread may be turn of AI and another will be handling a side effect for AI2
-boost::thread_specific_ptr<CCallback> cb;
-boost::thread_specific_ptr<VCAI> ai;
+thread_local CCallback * cb = nullptr;
+thread_local VCAI * ai = nullptr;
 
 //std::map<int, std::map<int, int> > HeroView::infosCount;
 
@@ -48,18 +48,18 @@ struct SetGlobalState
 {
 	SetGlobalState(VCAI * AI)
 	{
-		assert(!ai.get());
-		assert(!cb.get());
+		assert(!ai);
+		assert(!cb);
 
-		ai.reset(AI);
-		cb.reset(AI->myCb.get());
+		ai = AI;
+		cb = AI->myCb.get();
 	}
 	~SetGlobalState()
 	{
 		//TODO: how to handle rm? shouldn't be called after ai is destroyed, hopefully
 		//TODO: to ensure that, make rm unique_ptr
-		ai.release();
-		cb.release();
+		ai = nullptr;
+		cb = nullptr;
 	}
 };
 
@@ -2497,6 +2497,8 @@ void VCAI::requestActionASAP(std::function<void()> whatToDo)
 		boost::shared_lock<boost::shared_mutex> gsLock(CGameState::mutex);
 		whatToDo();
 	});
+
+	newThread.detach();
 }
 
 void VCAI::lostHero(HeroPtr h)

+ 3 - 1
client/mainmenu/CMainMenu.cpp

@@ -562,7 +562,9 @@ void CSimpleJoinScreen::startConnectThread(const std::string & addr, ui16 port)
 	// https://github.com/libsdl-org/SDL/blob/main/docs/README-android.md#threads-and-the-java-vm
 	CVCMIServer::reuseClientJNIEnv(SDL_AndroidGetJNIEnv());
 #endif
-	boost::thread(&CSimpleJoinScreen::connectThread, this, addr, port);
+	boost::thread connector(&CSimpleJoinScreen::connectThread, this, addr, port);
+
+	connector.detach();
 }
 
 void CSimpleJoinScreen::connectThread(const std::string & addr, ui16 port)

+ 0 - 2
test/CMakeLists.txt

@@ -92,8 +92,6 @@ set(test_HEADERS
 
 		spells/targetConditions/TargetConditionItemFixture.h
 
-		vcai/ResourceManagerTest.h
-
 		mock/BattleFake.h
 		mock/mock_BonusBearer.h
  		mock/mock_IGameCallback.h

+ 0 - 13
test/vcai/ResourceManagerTest.h

@@ -1,13 +0,0 @@
-/*
-* ResourceManagerTest.h, part of VCMI engine
-*
-* Authors: listed in file AUTHORS in main folder
-*
-* License: GNU General Public License v2.0 or later
-* Full text of license available in license.txt file, in main folder
-*
-*/
-
-#pragma once
-
-extern boost::thread_specific_ptr<CCallback> cb;

+ 0 - 1
test/vcai/ResurceManagerTest.cpp

@@ -12,7 +12,6 @@
 #include "gtest/gtest.h"
 
 #include "../AI/VCAI/VCAI.h"
-#include "ResourceManagerTest.h"
 #include "../AI/VCAI/Goals/Goals.h"
 #include "mock_VCAI_CGoal.h"
 #include "mock_VCAI.h"