2
0
Эх сурвалжийг харах

Remove CThreadHelper class, final usage replaced with tbb

Ivan Savenko 7 сар өмнө
parent
commit
3d205e0291

+ 5 - 19
AI/VCAI/Pathfinding/AIPathfinder.cpp

@@ -13,6 +13,8 @@
 #include "../../../CCallback.h"
 #include "../../../lib/mapping/CMapDefines.h"
 
+#include <tbb/task_group.h>
+
 std::vector<std::shared_ptr<AINodeStorage>> AIPathfinder::storagePool;
 std::map<HeroPtr, std::shared_ptr<AINodeStorage>> AIPathfinder::storageMap;
 
@@ -60,7 +62,7 @@ void AIPathfinder::updatePaths(std::vector<HeroPtr> heroes)
 		cb->calculatePaths(config);
 	};
 
-	std::vector<CThreadHelper::Task> calculationTasks;
+	tbb::task_group calculationTasks;
 
 	for(HeroPtr hero : heroes)
 	{
@@ -81,26 +83,10 @@ void AIPathfinder::updatePaths(std::vector<HeroPtr> heroes)
 
 		auto config = std::make_shared<AIPathfinding::AIPathfinderConfig>(cb, ai, nodeStorage);
 
-		calculationTasks.push_back(std::bind(calculatePaths, hero.get(), config));
+		calculationTasks.run(std::bind(calculatePaths, hero.get(), config));
 	}
 
-	int threadsCount = std::min(
-		boost::thread::hardware_concurrency(),
-		(uint32_t)calculationTasks.size());
-
-	if(threadsCount <= 1)
-	{
-		for(auto task : calculationTasks)
-		{
-			task();
-		}
-	}
-	else
-	{
-		CThreadHelper helper(&calculationTasks, threadsCount);
-
-		helper.run();
-	}
+	calculationTasks.wait();
 }
 
 std::shared_ptr<const AINodeStorage> AIPathfinder::getStorage(const HeroPtr & hero) const

+ 0 - 34
lib/CThreadHelper.cpp

@@ -21,40 +21,6 @@
 
 VCMI_LIB_NAMESPACE_BEGIN
 
-CThreadHelper::CThreadHelper(std::vector<std::function<void()>> * Tasks, int Threads):
-	currentTask(0),
-	amount(static_cast<int>(Tasks->size())),
-	tasks(Tasks),
-	threads(Threads)
-{
-}
-void CThreadHelper::run()
-{
-	std::vector<boost::thread> group;
-	for(int i=0;i<threads;i++)
-		group.emplace_back(std::bind(&CThreadHelper::processTasks,this));
-
-	for (auto & thread : group)
-		thread.join();
-
-	//thread group deletes threads, do not free manually
-}
-void CThreadHelper::processTasks()
-{
-	while(true)
-	{
-		int pom;
-		{
-			std::unique_lock<std::mutex> lock(rtinm);
-			if((pom = currentTask) >= amount)
-				break;
-			else
-				++currentTask;
-		}
-		(*tasks)[pom]();
-	}
-}
-
 static thread_local std::string threadNameForLogging;
 
 std::string getThreadName()

+ 0 - 74
lib/CThreadHelper.h

@@ -11,80 +11,6 @@
 
 VCMI_LIB_NAMESPACE_BEGIN
 
-
-///DEPRECATED
-/// Can assign CPU work to other threads/cores
-class DLL_LINKAGE CThreadHelper
-{
-public:
-	using Task = std::function<void()>;
-	CThreadHelper(std::vector<std::function<void()> > *Tasks, int Threads);
-	void run();
-private:
-	std::mutex rtinm;
-	int currentTask;
-	int amount;
-	int threads;
-	std::vector<Task> *tasks;
-
-
-	void processTasks();
-};
-
-template<typename Payload>
-class ThreadPool
-{
-public:
-	using Task = std::function<void(std::shared_ptr<Payload>)>;
-	using Tasks = std::vector<Task>;
-
-	ThreadPool(Tasks * tasks_, std::vector<std::shared_ptr<Payload>> context_)
-		: currentTask(0),
-		amount(tasks_->size()),
-		threads(context_.size()),
-		tasks(tasks_),
-		context(context_)
-	{}
-
-	void run()
-	{
-		std::vector<boost::thread> group;
-		for(size_t i=0; i<threads; i++)
-		{
-			std::shared_ptr<Payload> payload = context.at(i);
-			group.emplace_back(std::bind(&ThreadPool::processTasks, this, payload));
-		}
-
-		for (auto & thread : group)
-			thread.join();
-
-		//thread group deletes threads, do not free manually
-	}
-private:
-	std::mutex rtinm;
-	size_t currentTask;
-	size_t amount;
-	size_t threads;
-	Tasks * tasks;
-	std::vector<std::shared_ptr<Payload>> context;
-
-	void processTasks(std::shared_ptr<Payload> payload)
-	{
-		while(true)
-		{
-			size_t pom;
-			{
-				std::unique_lock<std::mutex> lock(rtinm);
-				if((pom = currentTask) >= amount)
-					break;
-				else
-					++currentTask;
-			}
-			(*tasks)[pom](payload);
-		}
-	}
-};
-
 /// Sets thread name that will be used for both logs and debugger (if supported)
 /// WARNING: on Unix-like systems this method should not be used for main thread since it will also change name of the process
 void DLL_LINKAGE setThreadName(const std::string &name);