| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/Thread.h" |
| 5 | |
| 6 | #ifdef _WIN32 |
| 7 | #include <Windows.h> |
| 8 | #include <processthreadsapi.h> |
| 9 | #else |
| 10 | #include <pthread.h> |
| 11 | #include <unistd.h> |
| 12 | #endif |
| 13 | |
| 14 | #ifdef __APPLE__ |
| 15 | #include <mach/mach.h> |
| 16 | #elif defined BSD4_4 || defined __FreeBSD__ || defined __OpenBSD__ |
| 17 | #include <pthread_np.h> |
| 18 | #elif defined __NetBSD__ |
| 19 | #include <sched.h> |
| 20 | #elif defined __HAIKU__ |
| 21 | #include <OS.h> |
| 22 | #endif |
| 23 | |
| 24 | #ifdef USE_VTUNE |
| 25 | #include <ittnotify.h> |
| 26 | #pragma comment(lib, "libittnotify.lib") |
| 27 | #endif |
| 28 | |
| 29 | #include "Common/CommonTypes.h" |
| 30 | #ifdef _WIN32 |
| 31 | #include "Common/StringUtil.h" |
| 32 | #endif |
| 33 | |
| 34 | namespace Common |
| 35 | { |
| 36 | int CurrentThreadId() |
| 37 | { |
| 38 | #ifdef _WIN32 |
| 39 | return GetCurrentThreadId(); |
| 40 | #elif defined __APPLE__ |
| 41 | return mach_thread_self(); |
| 42 | #else |
| 43 | return 0; |
| 44 | #endif |
| 45 | } |
| 46 | |
| 47 | #ifdef _WIN32 |
| 48 | |
| 49 | void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) |
| 50 | { |
| 51 | SetThreadAffinityMask(thread, mask); |
| 52 | } |
| 53 | |
| 54 | void SetCurrentThreadAffinity(u32 mask) |
| 55 | { |
| 56 | SetThreadAffinityMask(GetCurrentThread(), mask); |
| 57 | } |
| 58 | |
| 59 | // Supporting functions |
| 60 | void SleepCurrentThread(int ms) |
| 61 | { |
| 62 | Sleep(ms); |
| 63 | } |
| 64 | |
| 65 | void SwitchCurrentThread() |
| 66 | { |
| 67 | SwitchToThread(); |
| 68 | } |
| 69 | |
| 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 | static void SetCurrentThreadNameViaException(const char* name) |
| 74 | { |
| 75 | static const DWORD MS_VC_EXCEPTION = 0x406D1388; |
| 76 | |
| 77 | #pragma pack(push, 8) |
| 78 | struct THREADNAME_INFO |
| 79 | { |
| 80 | DWORD dwType; // must be 0x1000 |
| 81 | LPCSTR szName; // pointer to name (in user addr space) |
| 82 | DWORD dwThreadID; // thread ID (-1=caller thread) |
| 83 | DWORD dwFlags; // reserved for future use, must be zero |
| 84 | } info; |
| 85 | #pragma pack(pop) |
| 86 | |
| 87 | info.dwType = 0x1000; |
| 88 | info.szName = name; |
| 89 | info.dwThreadID = static_cast<DWORD>(-1); |
| 90 | info.dwFlags = 0; |
| 91 | |
| 92 | __try |
| 93 | { |
| 94 | RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info); |
| 95 | } |
| 96 | __except (EXCEPTION_CONTINUE_EXECUTION) |
| 97 | { |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static void SetCurrentThreadNameViaApi(const char* name) |
| 102 | { |
| 103 | // If possible, also set via the newer API. On some versions of Server it needs to be manually |
| 104 | // resolved. This API allows being able to observe the thread name even if a debugger wasn't |
| 105 | // attached when the name was set (see above link for more info). |
| 106 | static auto pSetThreadDescription = (decltype(&SetThreadDescription))GetProcAddress( |
| 107 | GetModuleHandleA("kernel32"), "SetThreadDescription"); |
| 108 | if (pSetThreadDescription) |
| 109 | { |
| 110 | pSetThreadDescription(GetCurrentThread(), UTF8ToWString(name).c_str()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void SetCurrentThreadName(const char* name) |
| 115 | { |
| 116 | SetCurrentThreadNameViaException(name); |
| 117 | SetCurrentThreadNameViaApi(name); |
| 118 | } |
| 119 | |
| 120 | #else // !WIN32, so must be POSIX threads |
| 121 | |
| 122 | void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) |
| 123 | { |
| 124 | #ifdef __APPLE__ |
| 125 | thread_policy_set(pthread_mach_thread_np(thread), THREAD_AFFINITY_POLICY, (integer_t*)&mask, 1); |
| 126 | #elif (defined __linux__ || defined BSD4_4 || defined __FreeBSD__ || defined __NetBSD__) && \ |
| 127 | !(defined ANDROID) |
| 128 | #ifndef __NetBSD__ |
| 129 | #ifdef __FreeBSD__ |
| 130 | cpuset_t cpu_set; |
| 131 | #else |
| 132 | cpu_set_t cpu_set; |
| 133 | #endif |
| 134 | CPU_ZERO(&cpu_set); |
| 135 | |
| 136 | for (int i = 0; i != sizeof(mask) * 8; ++i) |
| 137 | if ((mask >> i) & 1) |
| 138 | CPU_SET(i, &cpu_set); |
| 139 | |
| 140 | pthread_setaffinity_np(thread, sizeof(cpu_set), &cpu_set); |
| 141 | #else |
| 142 | cpuset_t* cpu_set = cpuset_create(); |
| 143 | |
| 144 | for (int i = 0; i != sizeof(mask) * 8; ++i) |
| 145 | if ((mask >> i) & 1) |
| 146 | cpuset_set(i, cpu_set); |
| 147 | |
| 148 | pthread_setaffinity_np(thread, cpuset_size(cpu_set), cpu_set); |
| 149 | cpuset_destroy(cpu_set); |
| 150 | #endif |
| 151 | #endif |
| 152 | } |
| 153 | |
| 154 | void SetCurrentThreadAffinity(u32 mask) |
| 155 | { |
| 156 | SetThreadAffinity(pthread_self(), mask); |
| 157 | } |
| 158 | |
| 159 | void SleepCurrentThread(int ms) |
| 160 | { |
| 161 | usleep(1000 * ms); |
| 162 | } |
| 163 | |
| 164 | void SwitchCurrentThread() |
| 165 | { |
| 166 | usleep(1000 * 1); |
| 167 | } |
| 168 | |
| 169 | void SetCurrentThreadName(const char* name) |
| 170 | { |
| 171 | #ifdef __APPLE__ |
| 172 | pthread_setname_np(name); |
| 173 | #elif defined __FreeBSD__ || defined __OpenBSD__ |
| 174 | pthread_set_name_np(pthread_self(), name); |
| 175 | #elif defined(__NetBSD__) |
| 176 | pthread_setname_np(pthread_self(), "%s", const_cast<char*>(name)); |
| 177 | #elif defined __HAIKU__ |
| 178 | rename_thread(find_thread(nullptr), name); |
| 179 | #else |
| 180 | // linux doesn't allow to set more than 16 bytes, including \0. |
| 181 | pthread_setname_np(pthread_self(), std::string(name).substr(0, 15).c_str()); |
| 182 | #endif |
| 183 | #ifdef USE_VTUNE |
| 184 | // VTune uses OS thread names by default but probably supports longer names when set via its own |
| 185 | // API. |
| 186 | __itt_thread_set_name(name); |
| 187 | #endif |
| 188 | } |
| 189 | |
| 190 | std::tuple<void*, size_t> GetCurrentThreadStack() |
| 191 | { |
| 192 | void* stack_addr; |
| 193 | size_t stack_size; |
| 194 | |
| 195 | pthread_t self = pthread_self(); |
| 196 | |
| 197 | #ifdef __APPLE__ |
| 198 | stack_size = pthread_get_stacksize_np(self); |
| 199 | stack_addr = reinterpret_cast<u8*>(pthread_get_stackaddr_np(self)) - stack_size; |
| 200 | #elif defined __OpenBSD__ |
| 201 | stack_t stack; |
| 202 | pthread_stackseg_np(self, &stack); |
| 203 | |
| 204 | stack_addr = reinterpret_cast<u8*>(stack.ss_sp) - stack.ss_size; |
| 205 | stack_size = stack.ss_size; |
| 206 | #else |
| 207 | pthread_attr_t attr; |
| 208 | |
| 209 | #ifdef __FreeBSD__ |
| 210 | pthread_attr_init(&attr); |
| 211 | pthread_attr_get_np(self, &attr); |
| 212 | #else |
| 213 | // Linux and NetBSD |
| 214 | pthread_getattr_np(self, &attr); |
| 215 | #endif |
| 216 | |
| 217 | pthread_attr_getstack(&attr, &stack_addr, &stack_size); |
| 218 | |
| 219 | pthread_attr_destroy(&attr); |
| 220 | #endif |
| 221 | |
| 222 | return std::make_tuple(stack_addr, stack_size); |
| 223 | } |
| 224 | |
| 225 | #endif |
| 226 | |
| 227 | } // namespace Common |