thread.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "common/string_util.h"
  13. #else
  14. #if defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  15. #include <pthread_np.h>
  16. #else
  17. #include <pthread.h>
  18. #endif
  19. #include <sched.h>
  20. #endif
  21. #ifndef _WIN32
  22. #include <unistd.h>
  23. #endif
  24. #ifdef __FreeBSD__
  25. #define cpu_set_t cpuset_t
  26. #endif
  27. namespace Common {
  28. #ifdef _WIN32
  29. void SetCurrentThreadPriority(ThreadPriority new_priority) {
  30. auto handle = GetCurrentThread();
  31. int windows_priority = 0;
  32. switch (new_priority) {
  33. case ThreadPriority::Low:
  34. windows_priority = THREAD_PRIORITY_BELOW_NORMAL;
  35. break;
  36. case ThreadPriority::Normal:
  37. windows_priority = THREAD_PRIORITY_NORMAL;
  38. break;
  39. case ThreadPriority::High:
  40. windows_priority = THREAD_PRIORITY_ABOVE_NORMAL;
  41. break;
  42. case ThreadPriority::VeryHigh:
  43. windows_priority = THREAD_PRIORITY_HIGHEST;
  44. break;
  45. case ThreadPriority::Critical:
  46. windows_priority = THREAD_PRIORITY_TIME_CRITICAL;
  47. break;
  48. default:
  49. windows_priority = THREAD_PRIORITY_NORMAL;
  50. break;
  51. }
  52. SetThreadPriority(handle, windows_priority);
  53. }
  54. #else
  55. void SetCurrentThreadPriority(ThreadPriority new_priority) {
  56. pthread_t this_thread = pthread_self();
  57. const auto scheduling_type = SCHED_OTHER;
  58. s32 max_prio = sched_get_priority_max(scheduling_type);
  59. s32 min_prio = sched_get_priority_min(scheduling_type);
  60. u32 level = std::max(static_cast<u32>(new_priority) + 1, 4U);
  61. struct sched_param params;
  62. if (max_prio > min_prio) {
  63. params.sched_priority = min_prio + ((max_prio - min_prio) * level) / 4;
  64. } else {
  65. params.sched_priority = min_prio - ((min_prio - max_prio) * level) / 4;
  66. }
  67. pthread_setschedparam(this_thread, scheduling_type, &params);
  68. }
  69. #endif
  70. #ifdef _MSC_VER
  71. // Sets the debugger-visible name of the current thread.
  72. void SetCurrentThreadName(const char* name) {
  73. SetThreadDescription(GetCurrentThread(), UTF8ToUTF16W(name).data());
  74. }
  75. #else // !MSVC_VER, so must be POSIX threads
  76. // MinGW with the POSIX threading model does not support pthread_setname_np
  77. #if !defined(_WIN32) || defined(_MSC_VER)
  78. void SetCurrentThreadName(const char* name) {
  79. #ifdef __APPLE__
  80. pthread_setname_np(name);
  81. #elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  82. pthread_set_name_np(pthread_self(), name);
  83. #elif defined(__NetBSD__)
  84. pthread_setname_np(pthread_self(), "%s", (void*)name);
  85. #elif defined(__linux__)
  86. // Linux limits thread names to 15 characters and will outright reject any
  87. // attempt to set a longer name with ERANGE.
  88. std::string truncated(name, std::min(strlen(name), static_cast<size_t>(15)));
  89. if (int e = pthread_setname_np(pthread_self(), truncated.c_str())) {
  90. errno = e;
  91. LOG_ERROR(Common, "Failed to set thread name to '{}': {}", truncated, GetLastErrorMsg());
  92. }
  93. #else
  94. pthread_setname_np(pthread_self(), name);
  95. #endif
  96. }
  97. #endif
  98. #if defined(_WIN32)
  99. void SetCurrentThreadName(const char* name) {
  100. // Do Nothing on MingW
  101. }
  102. #endif
  103. #endif
  104. } // namespace Common