thread.cpp 3.9 KB

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