2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CThreadHelper.cpp, 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
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CThreadHelper.h"
|
|
|
|
|
|
|
|
#ifdef VCMI_WINDOWS
|
|
|
|
#include <windows.h>
|
2023-03-26 18:42:08 +02:00
|
|
|
#elif defined(VCMI_HAIKU)
|
|
|
|
#include <OS.h>
|
2017-07-13 10:26:03 +02:00
|
|
|
#elif !defined(VCMI_APPLE) && !defined(VCMI_FREEBSD) && !defined(VCMI_HURD)
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
2009-04-15 17:03:31 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
CThreadHelper::CThreadHelper(std::vector<std::function<void()>> * Tasks, int Threads):
|
|
|
|
currentTask(0),
|
|
|
|
amount(static_cast<int>(Tasks->size())),
|
|
|
|
tasks(Tasks),
|
|
|
|
threads(Threads)
|
2008-08-06 16:18:28 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
void CThreadHelper::run()
|
|
|
|
{
|
2023-08-20 22:45:41 +02:00
|
|
|
std::vector<boost::thread> group;
|
2008-08-06 16:18:28 +03:00
|
|
|
for(int i=0;i<threads;i++)
|
2023-08-20 22:45:41 +02:00
|
|
|
group.emplace_back(std::bind(&CThreadHelper::processTasks,this));
|
|
|
|
|
|
|
|
for (auto & thread : group)
|
|
|
|
thread.join();
|
2016-11-25 15:17:38 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
//thread group deletes threads, do not free manually
|
2008-08-06 16:18:28 +03:00
|
|
|
}
|
|
|
|
void CThreadHelper::processTasks()
|
|
|
|
{
|
|
|
|
while(true)
|
|
|
|
{
|
2013-11-07 15:48:41 +03:00
|
|
|
int pom;
|
2008-08-06 16:18:28 +03:00
|
|
|
{
|
2012-09-15 22:16:16 +03:00
|
|
|
boost::unique_lock<boost::mutex> lock(rtinm);
|
2012-02-20 00:03:43 +03:00
|
|
|
if((pom = currentTask) >= amount)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
++currentTask;
|
2008-08-06 16:18:28 +03:00
|
|
|
}
|
2012-02-20 00:03:43 +03:00
|
|
|
(*tasks)[pom]();
|
2008-08-06 16:18:28 +03:00
|
|
|
}
|
2011-02-24 15:57:47 +02:00
|
|
|
}
|
|
|
|
|
2012-06-27 23:44:01 +03:00
|
|
|
// set name for this thread.
|
|
|
|
// NOTE: on *nix string will be trimmed to 16 symbols
|
|
|
|
void setThreadName(const std::string &name)
|
2011-02-24 15:57:47 +02:00
|
|
|
{
|
2014-08-26 13:19:04 +03:00
|
|
|
#ifdef VCMI_WINDOWS
|
2012-09-15 22:16:16 +03:00
|
|
|
#ifndef __GNUC__
|
2011-02-24 15:57:47 +02:00
|
|
|
//follows http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
|
|
|
|
const DWORD MS_VC_EXCEPTION=0x406D1388;
|
|
|
|
#pragma pack(push,8)
|
|
|
|
typedef struct tagTHREADNAME_INFO
|
|
|
|
{
|
|
|
|
DWORD dwType; // Must be 0x1000.
|
|
|
|
LPCSTR szName; // Pointer to name (in user addr space).
|
|
|
|
DWORD dwThreadID; // Thread ID (-1=caller thread).
|
|
|
|
DWORD dwFlags; // Reserved for future use, must be zero.
|
|
|
|
} THREADNAME_INFO;
|
|
|
|
#pragma pack(pop)
|
|
|
|
THREADNAME_INFO info;
|
|
|
|
info.dwType = 0x1000;
|
|
|
|
info.szName = name.c_str();
|
2012-06-27 23:44:01 +03:00
|
|
|
info.dwThreadID = -1;
|
2011-02-24 15:57:47 +02:00
|
|
|
info.dwFlags = 0;
|
|
|
|
|
2012-09-15 22:16:16 +03:00
|
|
|
|
2011-02-24 15:57:47 +02:00
|
|
|
__try
|
|
|
|
{
|
|
|
|
RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
|
|
|
|
}
|
|
|
|
__except(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
}
|
2012-09-15 22:16:16 +03:00
|
|
|
#else
|
|
|
|
//not supported
|
|
|
|
#endif
|
|
|
|
|
2012-12-01 09:30:52 +03:00
|
|
|
#elif defined(__linux__)
|
|
|
|
prctl(PR_SET_NAME, name.c_str(), 0, 0, 0);
|
2021-03-05 18:22:10 +02:00
|
|
|
#elif defined(VCMI_APPLE)
|
2022-09-23 18:22:37 +02:00
|
|
|
pthread_setname_np(name.c_str());
|
2023-03-26 18:42:08 +02:00
|
|
|
#elif defined(VCMI_HAIKU)
|
|
|
|
rename_thread(find_thread(NULL), name.c_str());
|
2011-02-24 15:57:47 +02:00
|
|
|
#endif
|
2012-09-15 22:16:16 +03:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|