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

Replaced boost::thread_specific_ptr with thread_local

This commit is contained in:
Ivan Savenko 2023-07-31 17:00:37 +03:00
parent 142889e3a5
commit 87957e74c1
67 changed files with 41 additions and 255 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,14 +20,11 @@
namespace NKAI namespace NKAI
{ {
extern boost::thread_specific_ptr<CCallback> cb;
extern boost::thread_specific_ptr<AIGateway> ai;
namespace AIPathfinding namespace AIPathfinding
{ {
void BuildBoatAction::execute(const CGHeroInstance * hero) const 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 Goals::TSubgoal BuildBoatAction::decompose(const CGHeroInstance * hero) const
@ -80,7 +77,7 @@ namespace AIPathfinding
void SummonBoatAction::execute(const CGHeroInstance * hero) const 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 const ChainActor * SummonBoatAction::getActor(const ChainActor * sourceActor) const

View File

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

View File

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

View File

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

View File

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

View File

@ -18,6 +18,7 @@
#include "../../lib/mapObjects/CGHeroInstance.h" #include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../CCallback.h" #include "../../CCallback.h"
class VCAI;
class CCallback; class CCallback;
struct creInfo; struct creInfo;
@ -33,7 +34,8 @@ const int ALLOWED_ROAMING_HEROES = 8;
extern const double SAFE_ATTACK_CONSTANT; extern const double SAFE_ATTACK_CONSTANT;
extern const int GOLD_RESERVE; 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 //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* //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> template<class Func>
void foreach_neighbour(const int3 & pos, const Func & foo) 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()) for(const int3 & dir : int3::getDirs())
{ {
const int3 n = pos + dir; const int3 n = pos + dir;

View File

@ -18,9 +18,6 @@
#define MIN_AI_STRENGTH (0.5f) //lower when combat AI gets smarter #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 #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() engineBase::engineBase()
{ {
rules = new fl::RuleBlock(); rules = new fl::RuleBlock();

View File

@ -23,9 +23,6 @@
FuzzyHelper * fh; FuzzyHelper * fh;
extern boost::thread_specific_ptr<VCAI> ai;
extern boost::thread_specific_ptr<CCallback> cb;
Goals::TSubgoal FuzzyHelper::chooseSolution(Goals::TGoalVec vec) Goals::TSubgoal FuzzyHelper::chooseSolution(Goals::TGoalVec vec)
{ {
if(vec.empty()) 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) 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) ui64 FuzzyHelper::evaluateDanger(crint3 tile, const CGHeroInstance * visitor, const VCAI * ai)

View File

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

View File

@ -16,10 +16,6 @@
#include "../BuildingManager.h" #include "../BuildingManager.h"
#include "../../../lib/StringConstants.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; using namespace Goals;
TSubgoal Goals::sptr(const AbstractGoal & tmp) TSubgoal Goals::sptr(const AbstractGoal & tmp)

View File

@ -14,10 +14,6 @@
#include "../AIhelper.h" #include "../AIhelper.h"
#include "../../../lib/mapObjects/CGTownInstance.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; using namespace Goals;
bool AdventureSpellCast::operator==(const AdventureSpellCast & other) const bool AdventureSpellCast::operator==(const AdventureSpellCast & other) const

View File

@ -19,11 +19,6 @@
#include "../../../lib/mapObjects/CGTownInstance.h" #include "../../../lib/mapObjects/CGTownInstance.h"
#include "../../../lib/StringConstants.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; using namespace Goals;
TGoalVec Build::getAllPossibleSubgoals() TGoalVec Build::getAllPossibleSubgoals()

View File

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

View File

@ -18,11 +18,6 @@
#include "../../../lib/mapObjects/CGTownInstance.h" #include "../../../lib/mapObjects/CGTownInstance.h"
#include "../../../lib/StringConstants.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; using namespace Goals;
bool BuildThis::operator==(const BuildThis & other) const bool BuildThis::operator==(const BuildThis & other) const

View File

@ -13,11 +13,6 @@
#include "../AIhelper.h" #include "../AIhelper.h"
#include "../../../lib/mapObjects/CGTownInstance.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; using namespace Goals;
bool BuyArmy::operator==(const BuyArmy & other) const bool BuyArmy::operator==(const BuyArmy & other) const

View File

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

View File

@ -18,11 +18,6 @@
#include "../../../lib/mapObjects/CGMarket.h" #include "../../../lib/mapObjects/CGMarket.h"
#include "../../../lib/StringConstants.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; using namespace Goals;
bool CollectRes::operator==(const CollectRes & other) const bool CollectRes::operator==(const CollectRes & other) const

View File

@ -14,10 +14,6 @@
#include "../AIhelper.h" #include "../AIhelper.h"
#include "../../../lib/mapObjects/CQuest.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; using namespace Goals;
bool CompleteQuest::operator==(const CompleteQuest & other) const bool CompleteQuest::operator==(const CompleteQuest & other) const

View File

@ -17,11 +17,6 @@
#include "../BuildingManager.h" #include "../BuildingManager.h"
#include "../../../lib/StringConstants.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; using namespace Goals;
bool Conquer::operator==(const Conquer & other) const bool Conquer::operator==(const Conquer & other) const

View File

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

View File

@ -18,10 +18,6 @@
#include "../../../lib/StringConstants.h" #include "../../../lib/StringConstants.h"
#include "../../../lib/CPlayerState.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; using namespace Goals;
namespace Goals namespace Goals
@ -41,8 +37,8 @@ namespace Goals
ExplorationHelper(HeroPtr h, bool gatherArmy) ExplorationHelper(HeroPtr h, bool gatherArmy)
{ {
cbp = cb.get(); cbp = cb;
aip = ai.get(); aip = ai;
hero = h; hero = h;
ts = cbp->getPlayerTeam(ai->playerID); ts = cbp->getPlayerTeam(ai->playerID);
sightRadius = hero->getSightRadius(); sightRadius = hero->getSightRadius();

View File

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

View File

@ -18,11 +18,6 @@
#include "../../../lib/mapObjects/CGTownInstance.h" #include "../../../lib/mapObjects/CGTownInstance.h"
#include "../../../lib/StringConstants.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; using namespace Goals;
bool GatherArmy::operator==(const GatherArmy & other) const bool GatherArmy::operator==(const GatherArmy & other) const

View File

@ -18,11 +18,6 @@
#include "../../../lib/mapObjects/CGTownInstance.h" #include "../../../lib/mapObjects/CGTownInstance.h"
#include "../../../lib/StringConstants.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; using namespace Goals;
bool GatherTroops::operator==(const GatherTroops & other) const bool GatherTroops::operator==(const GatherTroops & other) const

View File

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

View File

@ -17,11 +17,6 @@
#include "../BuildingManager.h" #include "../BuildingManager.h"
#include "../../../lib/StringConstants.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; using namespace Goals;
TSubgoal RecruitHero::whatToDoToAchieve() TSubgoal RecruitHero::whatToDoToAchieve()

View File

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

View File

@ -17,11 +17,6 @@
#include "../BuildingManager.h" #include "../BuildingManager.h"
#include "../../../lib/StringConstants.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; using namespace Goals;
bool VisitObj::operator==(const VisitObj & other) const bool VisitObj::operator==(const VisitObj & other) const

View File

@ -17,11 +17,6 @@
#include "../BuildingManager.h" #include "../BuildingManager.h"
#include "../../../lib/StringConstants.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; using namespace Goals;
bool VisitTile::operator==(const VisitTile & other) const bool VisitTile::operator==(const VisitTile & other) const

View File

@ -19,11 +19,6 @@
#include "../../../lib/mapObjects/CGTownInstance.h" #include "../../../lib/mapObjects/CGTownInstance.h"
#include "../../../lib/StringConstants.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; using namespace Goals;
TSubgoal Win::whatToDoToAchieve() TSubgoal Win::whatToDoToAchieve()

View File

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

View File

@ -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 // https://github.com/libsdl-org/SDL/blob/main/docs/README-android.md#threads-and-the-java-vm
CVCMIServer::reuseClientJNIEnv(SDL_AndroidGetJNIEnv()); CVCMIServer::reuseClientJNIEnv(SDL_AndroidGetJNIEnv());
#endif #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) void CSimpleJoinScreen::connectThread(const std::string & addr, ui16 port)

View File

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

View File

@ -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;

View File

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