thread.cpp 3.5 KB

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