thread.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <string>
  5. #include "common/logging/log.h"
  6. #include "common/thread.h"
  7. #ifdef __APPLE__
  8. #include <mach/mach.h>
  9. #elif defined(_WIN32)
  10. #include <windows.h>
  11. #else
  12. #if defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  13. #include <pthread_np.h>
  14. #else
  15. #include <pthread.h>
  16. #endif
  17. #include <sched.h>
  18. #endif
  19. #ifndef _WIN32
  20. #include <unistd.h>
  21. #endif
  22. #ifdef __FreeBSD__
  23. #define cpu_set_t cpuset_t
  24. #endif
  25. namespace Common {
  26. #ifdef _WIN32
  27. void SetCurrentThreadPriority(ThreadPriority new_priority) {
  28. auto handle = GetCurrentThread();
  29. int windows_priority = 0;
  30. switch (new_priority) {
  31. case ThreadPriority::Low:
  32. windows_priority = THREAD_PRIORITY_BELOW_NORMAL;
  33. break;
  34. case ThreadPriority::Normal:
  35. windows_priority = THREAD_PRIORITY_NORMAL;
  36. break;
  37. case ThreadPriority::High:
  38. windows_priority = THREAD_PRIORITY_ABOVE_NORMAL;
  39. break;
  40. case ThreadPriority::VeryHigh:
  41. windows_priority = THREAD_PRIORITY_HIGHEST;
  42. break;
  43. default:
  44. windows_priority = THREAD_PRIORITY_NORMAL;
  45. break;
  46. }
  47. SetThreadPriority(handle, windows_priority);
  48. }
  49. #else
  50. void SetCurrentThreadPriority(ThreadPriority new_priority) {
  51. pthread_t this_thread = pthread_self();
  52. s32 max_prio = sched_get_priority_max(SCHED_OTHER);
  53. s32 min_prio = sched_get_priority_min(SCHED_OTHER);
  54. u32 level = static_cast<u32>(new_priority) + 1;
  55. struct sched_param params;
  56. if (max_prio > min_prio) {
  57. params.sched_priority = min_prio + ((max_prio - min_prio) * level) / 4;
  58. } else {
  59. params.sched_priority = min_prio - ((min_prio - max_prio) * level) / 4;
  60. }
  61. pthread_setschedparam(this_thread, SCHED_OTHER, &params);
  62. }
  63. #endif
  64. #ifdef _MSC_VER
  65. // Sets the debugger-visible name of the current thread.
  66. // Uses trick documented in:
  67. // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
  68. void SetCurrentThreadName(const char* name) {
  69. static const DWORD MS_VC_EXCEPTION = 0x406D1388;
  70. #pragma pack(push, 8)
  71. struct THREADNAME_INFO {
  72. DWORD dwType; // must be 0x1000
  73. LPCSTR szName; // pointer to name (in user addr space)
  74. DWORD dwThreadID; // thread ID (-1=caller thread)
  75. DWORD dwFlags; // reserved for future use, must be zero
  76. } info;
  77. #pragma pack(pop)
  78. info.dwType = 0x1000;
  79. info.szName = name;
  80. info.dwThreadID = std::numeric_limits<DWORD>::max();
  81. info.dwFlags = 0;
  82. __try {
  83. RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
  84. } __except (EXCEPTION_CONTINUE_EXECUTION) {
  85. }
  86. }
  87. #else // !MSVC_VER, so must be POSIX threads
  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* name) {
  91. #ifdef __APPLE__
  92. pthread_setname_np(name);
  93. #elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  94. pthread_set_name_np(pthread_self(), name);
  95. #elif defined(__NetBSD__)
  96. pthread_setname_np(pthread_self(), "%s", (void*)name);
  97. #elif defined(__linux__)
  98. // Linux limits thread names to 15 characters and will outright reject any
  99. // attempt to set a longer name with ERANGE.
  100. std::string truncated(name, std::min(strlen(name), static_cast<size_t>(15)));
  101. if (int e = pthread_setname_np(pthread_self(), truncated.c_str())) {
  102. errno = e;
  103. LOG_ERROR(Common, "Failed to set thread name to '{}': {}", truncated, GetLastErrorMsg());
  104. }
  105. #else
  106. pthread_setname_np(pthread_self(), name);
  107. #endif
  108. }
  109. #endif
  110. #if defined(_WIN32)
  111. void SetCurrentThreadName(const char* name) {
  112. // Do Nothing on MingW
  113. }
  114. #endif
  115. #endif
  116. } // namespace Common