thread.cpp 3.2 KB

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