CThreadHelper.cpp 2.3 KB

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