CThreadHelper.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include <tbb/task_arena.h>
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. static thread_local std::string threadNameForLogging;
  23. std::string getThreadName()
  24. {
  25. if (!threadNameForLogging.empty())
  26. return threadNameForLogging;
  27. int tbbIndex = tbb::this_task_arena::current_thread_index();
  28. if (tbbIndex < 0)
  29. return boost::lexical_cast<std::string>(std::this_thread::get_id());
  30. else
  31. return "TBB worker " + boost::lexical_cast<std::string>(tbbIndex);
  32. }
  33. void setThreadNameLoggingOnly(const std::string &name)
  34. {
  35. threadNameForLogging = name;
  36. }
  37. void setThreadName(const std::string &name)
  38. {
  39. threadNameForLogging = name;
  40. #ifdef VCMI_WINDOWS
  41. #ifndef __GNUC__
  42. //follows http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
  43. const DWORD MS_VC_EXCEPTION=0x406D1388;
  44. #pragma pack(push,8)
  45. typedef struct tagTHREADNAME_INFO
  46. {
  47. DWORD dwType; // Must be 0x1000.
  48. LPCSTR szName; // Pointer to name (in user addr space).
  49. DWORD dwThreadID; // Thread ID (-1=caller thread).
  50. DWORD dwFlags; // Reserved for future use, must be zero.
  51. } THREADNAME_INFO;
  52. #pragma pack(pop)
  53. THREADNAME_INFO info;
  54. info.dwType = 0x1000;
  55. info.szName = name.c_str();
  56. info.dwThreadID = -1;
  57. info.dwFlags = 0;
  58. __try
  59. {
  60. RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
  61. }
  62. __except(EXCEPTION_EXECUTE_HANDLER)
  63. {
  64. }
  65. #else
  66. //not supported
  67. #endif
  68. #elif defined(VCMI_APPLE)
  69. pthread_setname_np(name.c_str());
  70. #elif defined(VCMI_FREEBSD)
  71. pthread_setname_np(pthread_self(), name.c_str());
  72. #elif defined(VCMI_HAIKU)
  73. rename_thread(find_thread(NULL), name.c_str());
  74. #elif defined(VCMI_UNIX)
  75. prctl(PR_SET_NAME, name.c_str(), 0, 0, 0);
  76. #else
  77. #error "Failed to find method to set thread name on this system. Please provide one (or disable this line if you just want code to compile)"
  78. #endif
  79. }
  80. VCMI_LIB_NAMESPACE_END