2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/Thread.h"
|
2009-07-10 20:22:25 +00:00
|
|
|
|
2017-01-23 07:08:45 +00:00
|
|
|
#ifdef _WIN32
|
2020-08-22 08:51:51 +00:00
|
|
|
#include <Windows.h>
|
|
|
|
#include <processthreadsapi.h>
|
2017-01-23 07:08:45 +00:00
|
|
|
#else
|
Jit: Don't use a second stack
This second stack leads to JNI problems on Android, because ART fetches
the address and size of the original stack using pthread functions
(see GetThreadStack in art/runtime/thread.cc), and (presumably) treats
stack addresses outside of the original stack as invalid. (What I don't
understand is why some JNI operations on the CPU thread work fine
despite this but others don't.)
Instead of creating a second stack, let's borrow the approach ART uses:
Use pthread functions to find out the stack's address and size, then
install guard pages at an appropriate location. This lets us get rid
of a workaround we had in the MsgAlert function.
Because we're no longer choosing the stack size ourselves, I've made some
tweaks to where the put the guard pages. Previously we had a stack of
2 MiB and a safe zone of 512 KiB. We now accept stacks as small as 512 KiB
(used on macOS) and use a safe zone of 256 KiB. I feel like this should
be fine, but haven't done much testing beyond "it seems to work".
By the way, on Windows it was already the case that we didn't create
a second stack... But there was a bug in the implementation!
The code for protecting the stack has to run on the CPU thread, since
it's the CPU thread's stack we want to protect, but it was actually
running on EmuThread. This commit fixes that, since now this bug
matters on other operating systems too.
2023-01-01 19:18:03 +00:00
|
|
|
#include <pthread.h>
|
2015-09-04 15:15:16 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2011-02-02 21:52:43 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <mach/mach.h>
|
2016-07-17 10:09:46 +00:00
|
|
|
#elif defined BSD4_4 || defined __FreeBSD__ || defined __OpenBSD__
|
2011-02-02 21:52:43 +00:00
|
|
|
#include <pthread_np.h>
|
2020-12-04 16:09:42 +00:00
|
|
|
#elif defined __NetBSD__
|
|
|
|
#include <sched.h>
|
2017-02-22 17:21:10 +00:00
|
|
|
#elif defined __HAIKU__
|
|
|
|
#include <OS.h>
|
2011-02-02 21:52:43 +00:00
|
|
|
#endif
|
|
|
|
|
2015-02-12 00:36:29 +00:00
|
|
|
#ifdef USE_VTUNE
|
|
|
|
#include <ittnotify.h>
|
|
|
|
#pragma comment(lib, "libittnotify.lib")
|
|
|
|
#endif
|
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "Common/CommonFuncs.h"
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
2008-07-12 17:40:22 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2011-01-27 20:47:58 +00:00
|
|
|
int CurrentThreadId()
|
|
|
|
{
|
2010-04-29 13:46:20 +00:00
|
|
|
#ifdef _WIN32
|
2011-01-27 20:47:58 +00:00
|
|
|
return GetCurrentThreadId();
|
2011-02-02 21:52:43 +00:00
|
|
|
#elif defined __APPLE__
|
|
|
|
return mach_thread_self();
|
2010-04-29 13:46:20 +00:00
|
|
|
#else
|
2011-01-27 20:47:58 +00:00
|
|
|
return 0;
|
2010-04-29 13:46:20 +00:00
|
|
|
#endif
|
2011-01-27 20:47:58 +00:00
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2008-07-12 17:40:22 +00:00
|
|
|
#ifdef _WIN32
|
2011-01-27 20:47:58 +00:00
|
|
|
|
|
|
|
void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask)
|
|
|
|
{
|
|
|
|
SetThreadAffinityMask(thread, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetCurrentThreadAffinity(u32 mask)
|
|
|
|
{
|
|
|
|
SetThreadAffinityMask(GetCurrentThread(), mask);
|
|
|
|
}
|
|
|
|
|
2011-03-05 06:11:26 +00:00
|
|
|
// Supporting functions
|
|
|
|
void SleepCurrentThread(int ms)
|
|
|
|
{
|
|
|
|
Sleep(ms);
|
|
|
|
}
|
2013-08-29 20:14:08 +00:00
|
|
|
|
2011-03-05 06:11:26 +00:00
|
|
|
void SwitchCurrentThread()
|
|
|
|
{
|
|
|
|
SwitchToThread();
|
|
|
|
}
|
2010-06-16 14:22:17 +00:00
|
|
|
|
2011-03-05 06:11:26 +00:00
|
|
|
// Sets the debugger-visible name of the current thread.
|
2017-06-07 11:16:02 +00:00
|
|
|
// Uses trick documented in:
|
|
|
|
// https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
|
2020-08-22 08:51:51 +00:00
|
|
|
static void SetCurrentThreadNameViaException(const char* name)
|
2011-03-05 06:11:26 +00:00
|
|
|
{
|
|
|
|
static const DWORD MS_VC_EXCEPTION = 0x406D1388;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2011-03-05 06:11:26 +00:00
|
|
|
#pragma pack(push, 8)
|
|
|
|
struct THREADNAME_INFO
|
2010-04-29 13:46:20 +00:00
|
|
|
{
|
|
|
|
DWORD dwType; // must be 0x1000
|
|
|
|
LPCSTR szName; // pointer to name (in user addr space)
|
|
|
|
DWORD dwThreadID; // thread ID (-1=caller thread)
|
|
|
|
DWORD dwFlags; // reserved for future use, must be zero
|
2011-03-05 06:11:26 +00:00
|
|
|
} info;
|
|
|
|
#pragma pack(pop)
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2011-03-05 06:11:26 +00:00
|
|
|
info.dwType = 0x1000;
|
2020-08-22 08:51:51 +00:00
|
|
|
info.szName = name;
|
2017-06-07 11:16:02 +00:00
|
|
|
info.dwThreadID = static_cast<DWORD>(-1);
|
2011-03-05 06:11:26 +00:00
|
|
|
info.dwFlags = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2011-03-05 06:11:26 +00:00
|
|
|
__try
|
2010-04-29 13:46:20 +00:00
|
|
|
{
|
2011-03-05 06:11:26 +00:00
|
|
|
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2011-03-05 06:11:26 +00:00
|
|
|
__except (EXCEPTION_CONTINUE_EXECUTION)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2020-08-22 08:51:51 +00:00
|
|
|
static void SetCurrentThreadNameViaApi(const char* name)
|
|
|
|
{
|
|
|
|
// If possible, also set via the newer API. On some versions of Server it needs to be manually
|
|
|
|
// resolved. This API allows being able to observe the thread name even if a debugger wasn't
|
|
|
|
// attached when the name was set (see above link for more info).
|
|
|
|
static auto pSetThreadDescription = (decltype(&SetThreadDescription))GetProcAddress(
|
|
|
|
GetModuleHandleA("kernel32"), "SetThreadDescription");
|
|
|
|
if (pSetThreadDescription)
|
|
|
|
{
|
|
|
|
pSetThreadDescription(GetCurrentThread(), UTF8ToWString(name).c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetCurrentThreadName(const char* name)
|
|
|
|
{
|
|
|
|
SetCurrentThreadNameViaException(name);
|
|
|
|
SetCurrentThreadNameViaApi(name);
|
|
|
|
}
|
|
|
|
|
2009-01-09 12:10:02 +00:00
|
|
|
#else // !WIN32, so must be POSIX threads
|
2011-01-27 20:47:58 +00:00
|
|
|
|
2011-02-02 21:52:43 +00:00
|
|
|
void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask)
|
|
|
|
{
|
|
|
|
#ifdef __APPLE__
|
|
|
|
thread_policy_set(pthread_mach_thread_np(thread), THREAD_AFFINITY_POLICY, (integer_t*)&mask, 1);
|
2020-12-04 16:09:42 +00:00
|
|
|
#elif (defined __linux__ || defined BSD4_4 || defined __FreeBSD__ || defined __NetBSD__) && \
|
|
|
|
!(defined ANDROID)
|
|
|
|
#ifndef __NetBSD__
|
2015-06-11 05:15:11 +00:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
cpuset_t cpu_set;
|
|
|
|
#else
|
2011-01-27 20:47:58 +00:00
|
|
|
cpu_set_t cpu_set;
|
2015-06-11 05:15:11 +00:00
|
|
|
#endif
|
2011-01-27 20:47:58 +00:00
|
|
|
CPU_ZERO(&cpu_set);
|
2013-04-15 20:28:55 +00:00
|
|
|
|
2011-01-27 20:47:58 +00:00
|
|
|
for (int i = 0; i != sizeof(mask) * 8; ++i)
|
|
|
|
if ((mask >> i) & 1)
|
|
|
|
CPU_SET(i, &cpu_set);
|
2013-04-15 20:28:55 +00:00
|
|
|
|
2011-01-27 20:47:58 +00:00
|
|
|
pthread_setaffinity_np(thread, sizeof(cpu_set), &cpu_set);
|
2020-12-04 16:09:42 +00:00
|
|
|
#else
|
|
|
|
cpuset_t* cpu_set = cpuset_create();
|
|
|
|
|
|
|
|
for (int i = 0; i != sizeof(mask) * 8; ++i)
|
|
|
|
if ((mask >> i) & 1)
|
|
|
|
cpuset_set(i, cpu_set);
|
|
|
|
|
|
|
|
pthread_setaffinity_np(thread, cpuset_size(cpu_set), cpu_set);
|
|
|
|
cpuset_destroy(cpu_set);
|
|
|
|
#endif
|
2011-01-27 20:47:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetCurrentThreadAffinity(u32 mask)
|
|
|
|
{
|
2011-02-02 21:52:43 +00:00
|
|
|
SetThreadAffinity(pthread_self(), mask);
|
2011-01-27 20:47:58 +00:00
|
|
|
}
|
|
|
|
|
2011-03-05 06:11:26 +00:00
|
|
|
void SleepCurrentThread(int ms)
|
|
|
|
{
|
|
|
|
usleep(1000 * ms);
|
|
|
|
}
|
2013-08-29 20:14:08 +00:00
|
|
|
|
2011-03-05 06:11:26 +00:00
|
|
|
void SwitchCurrentThread()
|
|
|
|
{
|
|
|
|
usleep(1000 * 1);
|
|
|
|
}
|
2010-06-16 14:22:17 +00:00
|
|
|
|
2020-08-22 08:51:51 +00:00
|
|
|
void SetCurrentThreadName(const char* name)
|
2011-03-05 06:11:26 +00:00
|
|
|
{
|
2013-08-29 20:14:08 +00:00
|
|
|
#ifdef __APPLE__
|
2020-08-22 08:51:51 +00:00
|
|
|
pthread_setname_np(name);
|
2016-07-17 10:09:46 +00:00
|
|
|
#elif defined __FreeBSD__ || defined __OpenBSD__
|
2020-08-22 08:51:51 +00:00
|
|
|
pthread_set_name_np(pthread_self(), name);
|
2020-12-04 16:09:42 +00:00
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
pthread_setname_np(pthread_self(), "%s", const_cast<char*>(name));
|
2017-02-22 17:21:10 +00:00
|
|
|
#elif defined __HAIKU__
|
2020-08-22 08:51:51 +00:00
|
|
|
rename_thread(find_thread(nullptr), name);
|
2013-08-29 20:14:08 +00:00
|
|
|
#else
|
2016-01-26 18:35:31 +00:00
|
|
|
// linux doesn't allow to set more than 16 bytes, including \0.
|
2020-08-22 08:51:51 +00:00
|
|
|
pthread_setname_np(pthread_self(), std::string(name).substr(0, 15).c_str());
|
2013-08-29 20:14:08 +00:00
|
|
|
#endif
|
2015-02-12 00:36:29 +00:00
|
|
|
#ifdef USE_VTUNE
|
|
|
|
// VTune uses OS thread names by default but probably supports longer names when set via its own
|
|
|
|
// API.
|
2020-08-22 08:51:51 +00:00
|
|
|
__itt_thread_set_name(name);
|
2015-02-12 00:36:29 +00:00
|
|
|
#endif
|
2011-03-05 06:11:26 +00:00
|
|
|
}
|
2010-07-06 16:16:07 +00:00
|
|
|
|
Jit: Don't use a second stack
This second stack leads to JNI problems on Android, because ART fetches
the address and size of the original stack using pthread functions
(see GetThreadStack in art/runtime/thread.cc), and (presumably) treats
stack addresses outside of the original stack as invalid. (What I don't
understand is why some JNI operations on the CPU thread work fine
despite this but others don't.)
Instead of creating a second stack, let's borrow the approach ART uses:
Use pthread functions to find out the stack's address and size, then
install guard pages at an appropriate location. This lets us get rid
of a workaround we had in the MsgAlert function.
Because we're no longer choosing the stack size ourselves, I've made some
tweaks to where the put the guard pages. Previously we had a stack of
2 MiB and a safe zone of 512 KiB. We now accept stacks as small as 512 KiB
(used on macOS) and use a safe zone of 256 KiB. I feel like this should
be fine, but haven't done much testing beyond "it seems to work".
By the way, on Windows it was already the case that we didn't create
a second stack... But there was a bug in the implementation!
The code for protecting the stack has to run on the CPU thread, since
it's the CPU thread's stack we want to protect, but it was actually
running on EmuThread. This commit fixes that, since now this bug
matters on other operating systems too.
2023-01-01 19:18:03 +00:00
|
|
|
std::tuple<void*, size_t> GetCurrentThreadStack()
|
|
|
|
{
|
|
|
|
void* stack_addr;
|
|
|
|
size_t stack_size;
|
|
|
|
|
|
|
|
pthread_t self = pthread_self();
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
stack_size = pthread_get_stacksize_np(self);
|
|
|
|
stack_addr = reinterpret_cast<u8*>(pthread_get_stackaddr_np(self)) - stack_size;
|
|
|
|
#elif defined __OpenBSD__
|
|
|
|
stack_t stack;
|
|
|
|
pthread_stackseg_np(self, &stack);
|
|
|
|
|
2023-04-29 23:21:48 +00:00
|
|
|
stack_addr = reinterpret_cast<u8*>(stack.ss_sp) - stack.ss_size;
|
|
|
|
stack_size = stack.ss_size;
|
Jit: Don't use a second stack
This second stack leads to JNI problems on Android, because ART fetches
the address and size of the original stack using pthread functions
(see GetThreadStack in art/runtime/thread.cc), and (presumably) treats
stack addresses outside of the original stack as invalid. (What I don't
understand is why some JNI operations on the CPU thread work fine
despite this but others don't.)
Instead of creating a second stack, let's borrow the approach ART uses:
Use pthread functions to find out the stack's address and size, then
install guard pages at an appropriate location. This lets us get rid
of a workaround we had in the MsgAlert function.
Because we're no longer choosing the stack size ourselves, I've made some
tweaks to where the put the guard pages. Previously we had a stack of
2 MiB and a safe zone of 512 KiB. We now accept stacks as small as 512 KiB
(used on macOS) and use a safe zone of 256 KiB. I feel like this should
be fine, but haven't done much testing beyond "it seems to work".
By the way, on Windows it was already the case that we didn't create
a second stack... But there was a bug in the implementation!
The code for protecting the stack has to run on the CPU thread, since
it's the CPU thread's stack we want to protect, but it was actually
running on EmuThread. This commit fixes that, since now this bug
matters on other operating systems too.
2023-01-01 19:18:03 +00:00
|
|
|
#else
|
|
|
|
pthread_attr_t attr;
|
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_get_np(self, &attr);
|
|
|
|
#else
|
|
|
|
// Linux and NetBSD
|
|
|
|
pthread_getattr_np(self, &attr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pthread_attr_getstack(&attr, &stack_addr, &stack_size);
|
|
|
|
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return std::make_tuple(stack_addr, stack_size);
|
|
|
|
}
|
|
|
|
|
2008-07-12 17:40:22 +00:00
|
|
|
#endif
|
2013-08-29 20:14:08 +00:00
|
|
|
|
2009-03-28 08:57:34 +00:00
|
|
|
} // namespace Common
|