thread.cpp 3.1 KB

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