From f376b2799941638fc3ac8f0ea4cb57da7d38ae12 Mon Sep 17 00:00:00 2001 From: ArseniyShestakov Date: Sun, 8 Nov 2015 19:19:48 +0300 Subject: [PATCH] Pathfinding: do path calculation in separate thread --- lib/CGameState.cpp | 5 ++++- lib/CGameState.h | 2 ++ lib/CPathfinder.cpp | 21 +++++++++++++++++++++ lib/CPathfinder.h | 1 + 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/CGameState.cpp b/lib/CGameState.cpp index 3b4217ac5..575601fce 100644 --- a/lib/CGameState.cpp +++ b/lib/CGameState.cpp @@ -2164,8 +2164,11 @@ void CGameState::apply(CPack *pack) void CGameState::calculatePaths(const CGHeroInstance *hero, CPathsInfo &out) { + if(pathfinderWorking) + pathfinderWorking->interrupt(); + CPathfinder pathfinder(out, this, hero); - pathfinder.calculatePaths(); + pathfinderWorking = make_unique(&CPathfinder::startPathfinder, pathfinder); } /** diff --git a/lib/CGameState.h b/lib/CGameState.h index 6702616bf..d0cd983ab 100644 --- a/lib/CGameState.h +++ b/lib/CGameState.h @@ -314,6 +314,8 @@ public: boost::shared_mutex *mx; + unique_ptr pathfinderWorking; + void giveHeroArtifact(CGHeroInstance *h, ArtifactID aid); void apply(CPack *pack); diff --git a/lib/CPathfinder.cpp b/lib/CPathfinder.cpp index 732261152..6fa558eaf 100644 --- a/lib/CPathfinder.cpp +++ b/lib/CPathfinder.cpp @@ -7,6 +7,7 @@ #include "mapObjects/CGHeroInstance.h" #include "GameConstants.h" #include "CStopWatch.h" +#include "CThreadHelper.h" /* * CPathfinder.cpp, part of VCMI engine @@ -59,6 +60,24 @@ CPathfinder::CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance neighbours.reserve(16); } +void CPathfinder::startPathfinder() +{ + try + { + setThreadName("CPathfinder::startPathfinder"); + + calculatePaths(); + } + catch(boost::thread_interrupted &e) + { + gs->pathfinderWorking.reset(); + return; + } + + gs->pathfinderWorking.reset(); +} + + void CPathfinder::calculatePaths() { int maxMovePointsLand = hero->maxMovePoints(true); @@ -192,6 +211,8 @@ void CPathfinder::calculatePaths() } } } + + boost::this_thread::interruption_point(); } //queue loop } diff --git a/lib/CPathfinder.h b/lib/CPathfinder.h index e6c346dee..0238110cb 100644 --- a/lib/CPathfinder.h +++ b/lib/CPathfinder.h @@ -91,6 +91,7 @@ class CPathfinder : private CGameInfoCallback { public: CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero); + void startPathfinder(); void calculatePaths(); //calculates possible paths for hero, uses current hero position and movement left; returns pointer to newly allocated CPath or nullptr if path does not exists private: