2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2014-02-10 11:54:46 -07:00
|
|
|
#pragma once
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2014-08-13 23:14:35 -06:00
|
|
|
#include <thread>
|
2008-12-07 21:46:09 -07: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 12:18:03 -07:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <tuple>
|
|
|
|
#endif
|
|
|
|
|
2014-08-13 23:14:35 -06:00
|
|
|
// Don't include Common.h here as it will break LogManager
|
2014-02-18 17:54:11 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2009-02-23 21:54:00 -07:00
|
|
|
// This may not be defined outside _WIN32
|
|
|
|
#ifndef _WIN32
|
2010-04-29 07:46:20 -06:00
|
|
|
#ifndef INFINITE
|
|
|
|
#define INFINITE 0xffffffff
|
|
|
|
#endif
|
2010-11-19 20:24:51 -07:00
|
|
|
#endif
|
2009-02-23 21:54:00 -07:00
|
|
|
|
2008-12-07 21:46:09 -07:00
|
|
|
namespace Common
|
|
|
|
{
|
2011-01-27 13:47:58 -07:00
|
|
|
int CurrentThreadId();
|
|
|
|
|
|
|
|
void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask);
|
|
|
|
void SetCurrentThreadAffinity(u32 mask);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2011-03-04 23:11:26 -07:00
|
|
|
void SleepCurrentThread(int ms);
|
2016-06-24 02:43:46 -06:00
|
|
|
void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms
|
2011-03-04 23:11:26 -07:00
|
|
|
|
|
|
|
// Use this function during a spin-wait to make the current thread
|
|
|
|
// relax while another thread is working. This may be more efficient
|
|
|
|
// than using events because event functions use kernel calls.
|
|
|
|
inline void YieldCPU()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
std::this_thread::yield();
|
2011-03-04 23:11:26 -07:00
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-01-21 12:46:25 -07:00
|
|
|
void SetCurrentThreadName(const char* name);
|
2013-10-28 23:23:17 -06: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 12:18:03 -07:00
|
|
|
#ifndef _WIN32
|
|
|
|
// Returns the lowest address of the stack and the size of the stack
|
|
|
|
std::tuple<void*, size_t> GetCurrentThreadStack();
|
|
|
|
#endif
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
} // namespace Common
|