thread.cpp 3.1 KB

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