thread.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. case ThreadPriority::Critical:
  45. windows_priority = THREAD_PRIORITY_TIME_CRITICAL;
  46. break;
  47. default:
  48. windows_priority = THREAD_PRIORITY_NORMAL;
  49. break;
  50. }
  51. SetThreadPriority(handle, windows_priority);
  52. }
  53. #else
  54. void SetCurrentThreadPriority(ThreadPriority new_priority) {
  55. pthread_t this_thread = pthread_self();
  56. const auto scheduling_type = SCHED_OTHER;
  57. s32 max_prio = sched_get_priority_max(scheduling_type);
  58. s32 min_prio = sched_get_priority_min(scheduling_type);
  59. u32 level = std::max(static_cast<u32>(new_priority) + 1, 4U);
  60. struct sched_param params;
  61. if (max_prio > min_prio) {
  62. params.sched_priority = min_prio + ((max_prio - min_prio) * level) / 4;
  63. } else {
  64. params.sched_priority = min_prio - ((min_prio - max_prio) * level) / 4;
  65. }
  66. pthread_setschedparam(this_thread, scheduling_type, &params);
  67. }
  68. #endif
  69. #ifdef _MSC_VER
  70. // Sets the debugger-visible name of the current thread.
  71. // Uses trick documented in:
  72. // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
  73. void SetCurrentThreadName(const char* name) {
  74. static const DWORD MS_VC_EXCEPTION = 0x406D1388;
  75. #pragma pack(push, 8)
  76. struct THREADNAME_INFO {
  77. DWORD dwType; // must be 0x1000
  78. LPCSTR szName; // pointer to name (in user addr space)
  79. DWORD dwThreadID; // thread ID (-1=caller thread)
  80. DWORD dwFlags; // reserved for future use, must be zero
  81. } info;
  82. #pragma pack(pop)
  83. info.dwType = 0x1000;
  84. info.szName = name;
  85. info.dwThreadID = std::numeric_limits<DWORD>::max();
  86. info.dwFlags = 0;
  87. __try {
  88. RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
  89. } __except (EXCEPTION_CONTINUE_EXECUTION) {
  90. }
  91. }
  92. #else // !MSVC_VER, so must be POSIX threads
  93. // MinGW with the POSIX threading model does not support pthread_setname_np
  94. #if !defined(_WIN32) || defined(_MSC_VER)
  95. void SetCurrentThreadName(const char* name) {
  96. #ifdef __APPLE__
  97. pthread_setname_np(name);
  98. #elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  99. pthread_set_name_np(pthread_self(), name);
  100. #elif defined(__NetBSD__)
  101. pthread_setname_np(pthread_self(), "%s", (void*)name);
  102. #elif defined(__linux__)
  103. // Linux limits thread names to 15 characters and will outright reject any
  104. // attempt to set a longer name with ERANGE.
  105. std::string truncated(name, std::min(strlen(name), static_cast<size_t>(15)));
  106. if (int e = pthread_setname_np(pthread_self(), truncated.c_str())) {
  107. errno = e;
  108. LOG_ERROR(Common, "Failed to set thread name to '{}': {}", truncated, GetLastErrorMsg());
  109. }
  110. #else
  111. pthread_setname_np(pthread_self(), name);
  112. #endif
  113. }
  114. #endif
  115. #if defined(_WIN32)
  116. void SetCurrentThreadName(const char* name) {
  117. // Do Nothing on MingW
  118. }
  119. #endif
  120. #endif
  121. } // namespace Common