CThreadHelper.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * CThreadHelper.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CThreadHelper.h"
  12. #ifdef VCMI_WINDOWS
  13. #include <windows.h>
  14. #elif defined(VCMI_HAIKU)
  15. #include <OS.h>
  16. #elif !defined(VCMI_APPLE) && !defined(VCMI_FREEBSD) && \
  17. !defined(VCMI_HURD) && !defined(VCMI_OPENBSD)
  18. #include <sys/prctl.h>
  19. #endif
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. CThreadHelper::CThreadHelper(std::vector<std::function<void()>> * Tasks, int Threads):
  22. currentTask(0),
  23. amount(static_cast<int>(Tasks->size())),
  24. tasks(Tasks),
  25. threads(Threads)
  26. {
  27. }
  28. void CThreadHelper::run()
  29. {
  30. std::vector<boost::thread> group;
  31. for(int i=0;i<threads;i++)
  32. group.emplace_back(std::bind(&CThreadHelper::processTasks,this));
  33. for (auto & thread : group)
  34. thread.join();
  35. //thread group deletes threads, do not free manually
  36. }
  37. void CThreadHelper::processTasks()
  38. {
  39. while(true)
  40. {
  41. int pom;
  42. {
  43. boost::unique_lock<boost::mutex> lock(rtinm);
  44. if((pom = currentTask) >= amount)
  45. break;
  46. else
  47. ++currentTask;
  48. }
  49. (*tasks)[pom]();
  50. }
  51. }
  52. static thread_local std::string threadNameForLogging;
  53. std::string getThreadName()
  54. {
  55. if (!threadNameForLogging.empty())
  56. return threadNameForLogging;
  57. return boost::lexical_cast<std::string>(boost::this_thread::get_id());
  58. }
  59. void setThreadNameLoggingOnly(const std::string &name)
  60. {
  61. threadNameForLogging = name;
  62. }
  63. void setThreadName(const std::string &name)
  64. {
  65. threadNameForLogging = name;
  66. #ifdef VCMI_WINDOWS
  67. #ifndef __GNUC__
  68. //follows http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
  69. const DWORD MS_VC_EXCEPTION=0x406D1388;
  70. #pragma pack(push,8)
  71. typedef struct tagTHREADNAME_INFO
  72. {
  73. DWORD dwType; // Must be 0x1000.
  74. LPCSTR szName; // Pointer to name (in user addr space).
  75. DWORD dwThreadID; // Thread ID (-1=caller thread).
  76. DWORD dwFlags; // Reserved for future use, must be zero.
  77. } THREADNAME_INFO;
  78. #pragma pack(pop)
  79. THREADNAME_INFO info;
  80. info.dwType = 0x1000;
  81. info.szName = name.c_str();
  82. info.dwThreadID = -1;
  83. info.dwFlags = 0;
  84. __try
  85. {
  86. RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
  87. }
  88. __except(EXCEPTION_EXECUTE_HANDLER)
  89. {
  90. }
  91. #else
  92. //not supported
  93. #endif
  94. #elif defined(VCMI_UNIX)
  95. prctl(PR_SET_NAME, name.c_str(), 0, 0, 0);
  96. #elif defined(VCMI_APPLE)
  97. pthread_setname_np(name.c_str());
  98. #elif defined(VCMI_HAIKU)
  99. rename_thread(find_thread(NULL), name.c_str());
  100. #endif
  101. }
  102. VCMI_LIB_NAMESPACE_END