thread.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. void SetCurrentThreadPriority(ThreadPriority new_priority) {
  26. auto handle = GetCurrentThread();
  27. int windows_priority = 0;
  28. switch (new_priority) {
  29. case ThreadPriority::Low:
  30. windows_priority = THREAD_PRIORITY_BELOW_NORMAL;
  31. break;
  32. case ThreadPriority::Normal:
  33. windows_priority = THREAD_PRIORITY_NORMAL;
  34. break;
  35. case ThreadPriority::High:
  36. windows_priority = THREAD_PRIORITY_ABOVE_NORMAL;
  37. break;
  38. case ThreadPriority::VeryHigh:
  39. windows_priority = THREAD_PRIORITY_HIGHEST;
  40. break;
  41. default:
  42. windows_priority = THREAD_PRIORITY_NORMAL;
  43. break;
  44. }
  45. SetThreadPriority(handle, windows_priority);
  46. }
  47. #else
  48. void SetCurrentThreadPriority(ThreadPriority new_priority) {
  49. pthread_t this_thread = pthread_self();
  50. s32 max_prio = sched_get_priority_max(SCHED_OTHER);
  51. s32 min_prio = sched_get_priority_min(SCHED_OTHER);
  52. u32 level = static_cast<u32>(new_priority) + 1;
  53. struct sched_param params;
  54. if (max_prio > min_prio) {
  55. params.sched_priority = min_prio + ((max_prio - min_prio) * level) / 4;
  56. } else {
  57. params.sched_priority = min_prio - ((min_prio - max_prio) * level) / 4;
  58. }
  59. pthread_setschedparam(this_thread, SCHED_OTHER, &params);
  60. }
  61. #endif
  62. #ifdef _MSC_VER
  63. // Sets the debugger-visible name of the current thread.
  64. // Uses trick documented in:
  65. // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
  66. void SetCurrentThreadName(const char* name) {
  67. static const DWORD MS_VC_EXCEPTION = 0x406D1388;
  68. #pragma pack(push, 8)
  69. struct THREADNAME_INFO {
  70. DWORD dwType; // must be 0x1000
  71. LPCSTR szName; // pointer to name (in user addr space)
  72. DWORD dwThreadID; // thread ID (-1=caller thread)
  73. DWORD dwFlags; // reserved for future use, must be zero
  74. } info;
  75. #pragma pack(pop)
  76. info.dwType = 0x1000;
  77. info.szName = name;
  78. info.dwThreadID = std::numeric_limits<DWORD>::max();
  79. info.dwFlags = 0;
  80. __try {
  81. RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
  82. } __except (EXCEPTION_CONTINUE_EXECUTION) {
  83. }
  84. }
  85. #else // !MSVC_VER, so must be POSIX threads
  86. // MinGW with the POSIX threading model does not support pthread_setname_np
  87. #if !defined(_WIN32) || defined(_MSC_VER)
  88. void SetCurrentThreadName(const char* name) {
  89. #ifdef __APPLE__
  90. pthread_setname_np(name);
  91. #elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  92. pthread_set_name_np(pthread_self(), name);
  93. #elif defined(__NetBSD__)
  94. pthread_setname_np(pthread_self(), "%s", (void*)name);
  95. #else
  96. pthread_setname_np(pthread_self(), name);
  97. #endif
  98. }
  99. #endif
  100. #if defined(_WIN32)
  101. void SetCurrentThreadName(const char* name) {
  102. // Do Nothing on MingW
  103. }
  104. #endif
  105. #endif
  106. } // namespace Common