Thread.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2008 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include "Common/CommonFuncs.h"
  5. #include "Common/CommonTypes.h"
  6. #include "Common/Thread.h"
  7. #ifdef __APPLE__
  8. #include <mach/mach.h>
  9. #elif defined BSD4_4 || defined __FreeBSD__
  10. #include <pthread_np.h>
  11. #endif
  12. #ifdef USE_VTUNE
  13. #include <ittnotify.h>
  14. #pragma comment(lib, "libittnotify.lib")
  15. #endif
  16. namespace Common
  17. {
  18. int CurrentThreadId()
  19. {
  20. #ifdef _WIN32
  21. return GetCurrentThreadId();
  22. #elif defined __APPLE__
  23. return mach_thread_self();
  24. #else
  25. return 0;
  26. #endif
  27. }
  28. #ifdef _WIN32
  29. void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask)
  30. {
  31. SetThreadAffinityMask(thread, mask);
  32. }
  33. void SetCurrentThreadAffinity(u32 mask)
  34. {
  35. SetThreadAffinityMask(GetCurrentThread(), mask);
  36. }
  37. // Supporting functions
  38. void SleepCurrentThread(int ms)
  39. {
  40. Sleep(ms);
  41. }
  42. void SwitchCurrentThread()
  43. {
  44. SwitchToThread();
  45. }
  46. // Sets the debugger-visible name of the current thread.
  47. // Uses undocumented (actually, it is now documented) trick.
  48. // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxtsksettingthreadname.asp
  49. // This is implemented much nicer in upcoming msvc++, see:
  50. // http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.100).aspx
  51. void SetCurrentThreadName(const char* szThreadName)
  52. {
  53. static const DWORD MS_VC_EXCEPTION = 0x406D1388;
  54. #pragma pack(push,8)
  55. struct THREADNAME_INFO
  56. {
  57. DWORD dwType; // must be 0x1000
  58. LPCSTR szName; // pointer to name (in user addr space)
  59. DWORD dwThreadID; // thread ID (-1=caller thread)
  60. DWORD dwFlags; // reserved for future use, must be zero
  61. } info;
  62. #pragma pack(pop)
  63. info.dwType = 0x1000;
  64. info.szName = szThreadName;
  65. info.dwThreadID = -1; //dwThreadID;
  66. info.dwFlags = 0;
  67. __try
  68. {
  69. RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
  70. }
  71. __except(EXCEPTION_CONTINUE_EXECUTION)
  72. {}
  73. }
  74. #else // !WIN32, so must be POSIX threads
  75. void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask)
  76. {
  77. #ifdef __APPLE__
  78. thread_policy_set(pthread_mach_thread_np(thread),
  79. THREAD_AFFINITY_POLICY, (integer_t *)&mask, 1);
  80. #elif (defined __linux__ || defined BSD4_4 || defined __FreeBSD__) && !(defined ANDROID)
  81. #ifdef __FreeBSD__
  82. cpuset_t cpu_set;
  83. #else
  84. cpu_set_t cpu_set;
  85. #endif
  86. CPU_ZERO(&cpu_set);
  87. for (int i = 0; i != sizeof(mask) * 8; ++i)
  88. if ((mask >> i) & 1)
  89. CPU_SET(i, &cpu_set);
  90. pthread_setaffinity_np(thread, sizeof(cpu_set), &cpu_set);
  91. #endif
  92. }
  93. void SetCurrentThreadAffinity(u32 mask)
  94. {
  95. SetThreadAffinity(pthread_self(), mask);
  96. }
  97. void SleepCurrentThread(int ms)
  98. {
  99. usleep(1000 * ms);
  100. }
  101. void SwitchCurrentThread()
  102. {
  103. usleep(1000 * 1);
  104. }
  105. void SetCurrentThreadName(const char* szThreadName)
  106. {
  107. #ifdef __APPLE__
  108. pthread_setname_np(szThreadName);
  109. #elif defined __FreeBSD__
  110. pthread_set_name_np(pthread_self(), szThreadName);
  111. #else
  112. pthread_setname_np(pthread_self(), szThreadName);
  113. #endif
  114. #ifdef USE_VTUNE
  115. // VTune uses OS thread names by default but probably supports longer names when set via its own API.
  116. __itt_thread_set_name(szThreadName);
  117. #endif
  118. }
  119. #endif
  120. } // namespace Common