2011-12-14 00:35:28 +03:00
|
|
|
#pragma once
|
|
|
|
|
2009-04-16 14:14:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CThreadHelper.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
|
|
|
|
*
|
2009-04-16 14:14:13 +03:00
|
|
|
*/
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
typedef std::function<void()> Task;
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2011-02-22 13:52:36 +02:00
|
|
|
/// Can assign CPU work to other threads/cores
|
2011-12-14 00:35:28 +03:00
|
|
|
class DLL_LINKAGE CThreadHelper
|
2009-04-16 14:14:13 +03:00
|
|
|
{
|
|
|
|
boost::mutex rtinm;
|
|
|
|
int currentTask, amount, threads;
|
|
|
|
std::vector<Task> *tasks;
|
|
|
|
|
|
|
|
|
|
|
|
void processTasks();
|
|
|
|
public:
|
2013-06-26 14:18:27 +03:00
|
|
|
CThreadHelper(std::vector<std::function<void()> > *Tasks, int Threads);
|
2009-04-16 14:14:13 +03:00
|
|
|
void run();
|
|
|
|
};
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
template <typename T> inline void setData(T * data, std::function<T()> func)
|
2009-04-16 14:14:13 +03:00
|
|
|
{
|
|
|
|
*data = func();
|
|
|
|
}
|
|
|
|
|
2012-06-27 23:44:01 +03:00
|
|
|
void DLL_LINKAGE setThreadName(const std::string &name);
|
2009-04-16 14:14:13 +03:00
|
|
|
|
|
|
|
#define GET_DATA(TYPE,DESTINATION,FUNCTION_TO_GET) \
|
2014-08-04 21:33:59 +03:00
|
|
|
(std::bind(&setData<TYPE>,&DESTINATION,FUNCTION_TO_GET))
|
2009-04-16 14:14:13 +03:00
|
|
|
#define GET_SURFACE(SUR_DESTINATION, SUR_NAME) \
|
|
|
|
(GET_DATA \
|
|
|
|
(SDL_Surface*,SUR_DESTINATION,\
|
2014-08-04 21:33:59 +03:00
|
|
|
std::function<SDL_Surface*()>(std::bind(&BitmapHandler::loadBitmap,SUR_NAME,true))))
|
2009-04-16 14:14:13 +03:00
|
|
|
#define GET_DEF(DESTINATION, DEF_NAME) \
|
|
|
|
(GET_DATA \
|
|
|
|
(CDefHandler*,DESTINATION,\
|
2014-08-04 21:33:59 +03:00
|
|
|
std::function<CDefHandler*()>(std::bind(CDefHandler::giveDef,DEF_NAME))))
|
2009-04-16 14:14:13 +03:00
|
|
|
#define GET_DEF_ESS(DESTINATION, DEF_NAME) \
|
|
|
|
(GET_DATA \
|
|
|
|
(CDefEssential*,DESTINATION,\
|
2014-08-04 21:33:59 +03:00
|
|
|
std::function<CDefEssential*()>(std::bind(CDefHandler::giveDefEss,DEF_NAME))))
|