summaryrefslogtreecommitdiff
path: root/src/common/thread.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/thread.cpp')
-rw-r--r--src/common/thread.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/common/thread.cpp b/src/common/thread.cpp
index 0cd2d10bf..d2c1ac60d 100644
--- a/src/common/thread.cpp
+++ b/src/common/thread.cpp
@@ -2,6 +2,8 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "common/common_funcs.h"
6#include "common/logging/log.h"
5#include "common/thread.h" 7#include "common/thread.h"
6#ifdef __APPLE__ 8#ifdef __APPLE__
7#include <mach/mach.h> 9#include <mach/mach.h>
@@ -19,12 +21,60 @@
19#include <unistd.h> 21#include <unistd.h>
20#endif 22#endif
21 23
24#include <string>
25
22#ifdef __FreeBSD__ 26#ifdef __FreeBSD__
23#define cpu_set_t cpuset_t 27#define cpu_set_t cpuset_t
24#endif 28#endif
25 29
26namespace Common { 30namespace Common {
27 31
32#ifdef _WIN32
33
34void SetCurrentThreadPriority(ThreadPriority new_priority) {
35 auto handle = GetCurrentThread();
36 int windows_priority = 0;
37 switch (new_priority) {
38 case ThreadPriority::Low:
39 windows_priority = THREAD_PRIORITY_BELOW_NORMAL;
40 break;
41 case ThreadPriority::Normal:
42 windows_priority = THREAD_PRIORITY_NORMAL;
43 break;
44 case ThreadPriority::High:
45 windows_priority = THREAD_PRIORITY_ABOVE_NORMAL;
46 break;
47 case ThreadPriority::VeryHigh:
48 windows_priority = THREAD_PRIORITY_HIGHEST;
49 break;
50 default:
51 windows_priority = THREAD_PRIORITY_NORMAL;
52 break;
53 }
54 SetThreadPriority(handle, windows_priority);
55}
56
57#else
58
59void SetCurrentThreadPriority(ThreadPriority new_priority) {
60 pthread_t this_thread = pthread_self();
61
62 s32 max_prio = sched_get_priority_max(SCHED_OTHER);
63 s32 min_prio = sched_get_priority_min(SCHED_OTHER);
64 u32 level = static_cast<u32>(new_priority) + 1;
65
66 struct sched_param params;
67 if (max_prio > min_prio) {
68 params.sched_priority = min_prio + ((max_prio - min_prio) * level) / 4;
69 } else {
70 params.sched_priority = min_prio - ((min_prio - max_prio) * level) / 4;
71 }
72
73 pthread_setschedparam(this_thread, SCHED_OTHER, &params);
74}
75
76#endif
77
28#ifdef _MSC_VER 78#ifdef _MSC_VER
29 79
30// Sets the debugger-visible name of the current thread. 80// Sets the debugger-visible name of the current thread.
@@ -64,12 +114,26 @@ void SetCurrentThreadName(const char* name) {
64 pthread_set_name_np(pthread_self(), name); 114 pthread_set_name_np(pthread_self(), name);
65#elif defined(__NetBSD__) 115#elif defined(__NetBSD__)
66 pthread_setname_np(pthread_self(), "%s", (void*)name); 116 pthread_setname_np(pthread_self(), "%s", (void*)name);
117#elif defined(__linux__)
118 // Linux limits thread names to 15 characters and will outright reject any
119 // attempt to set a longer name with ERANGE.
120 std::string truncated(name, std::min(strlen(name), static_cast<size_t>(15)));
121 if (int e = pthread_setname_np(pthread_self(), truncated.c_str())) {
122 errno = e;
123 LOG_ERROR(Common, "Failed to set thread name to '{}': {}", truncated, GetLastErrorMsg());
124 }
67#else 125#else
68 pthread_setname_np(pthread_self(), name); 126 pthread_setname_np(pthread_self(), name);
69#endif 127#endif
70} 128}
71#endif 129#endif
72 130
131#if defined(_WIN32)
132void SetCurrentThreadName(const char* name) {
133 // Do Nothing on MingW
134}
135#endif
136
73#endif 137#endif
74 138
75} // namespace Common 139} // namespace Common