thread.cpp 3.3 KB

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