mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-25 22:42:04 +02:00
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there. * Declared existing Entity APIs. * Added basic script context caching * Started Lua script module * Started Lua spell effect API * Started script state persistence * Started battle info callback binding * CommitPackage removed * Extracted spells::Caster to own header; Expanded Spell API. * implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C * !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented * Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key) * Re-enabled VERM macros. * !?GM0 added * !?TM implemented * Added !!MF:N * Started !?OB, !!BM, !!HE, !!OW, !!UN * Added basic support of w-variables * Added support for ERM indirect variables * Made !?FU regular trigger * !!re (ERA loop receiver) implemented * Fixed ERM receivers with zero args.
This commit is contained in:
@@ -10,25 +10,74 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
typedef std::function<void()> Task;
|
||||
|
||||
///DEPRECATED
|
||||
/// Can assign CPU work to other threads/cores
|
||||
class DLL_LINKAGE CThreadHelper
|
||||
{
|
||||
public:
|
||||
typedef std::function<void()> Task;
|
||||
CThreadHelper(std::vector<std::function<void()> > *Tasks, int Threads);
|
||||
void run();
|
||||
private:
|
||||
boost::mutex rtinm;
|
||||
int currentTask, amount, threads;
|
||||
std::vector<Task> *tasks;
|
||||
|
||||
|
||||
void processTasks();
|
||||
public:
|
||||
CThreadHelper(std::vector<std::function<void()> > *Tasks, int Threads);
|
||||
void run();
|
||||
};
|
||||
|
||||
template <typename T> inline void setData(T * data, std::function<T()> func)
|
||||
template<typename Payload>
|
||||
class ThreadPool
|
||||
{
|
||||
*data = func();
|
||||
}
|
||||
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()
|
||||
{
|
||||
boost::thread_group grupa;
|
||||
for(size_t i=0; i<threads; i++)
|
||||
{
|
||||
std::shared_ptr<Payload> payload = context.at(i);
|
||||
|
||||
grupa.create_thread(std::bind(&ThreadPool::processTasks, this, payload));
|
||||
}
|
||||
|
||||
grupa.join_all();
|
||||
|
||||
//thread group deletes threads, do not free manually
|
||||
}
|
||||
private:
|
||||
boost::mutex rtinm;
|
||||
size_t currentTask, amount, threads;
|
||||
Tasks * tasks;
|
||||
std::vector<std::shared_ptr<Payload>> context;
|
||||
|
||||
void processTasks(std::shared_ptr<Payload> payload)
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
size_t pom;
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(rtinm);
|
||||
if((pom = currentTask) >= amount)
|
||||
break;
|
||||
else
|
||||
++currentTask;
|
||||
}
|
||||
(*tasks)[pom](payload);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void DLL_LINKAGE setThreadName(const std::string &name);
|
||||
|
||||
Reference in New Issue
Block a user