2020-04-08 09:21:35 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-04-08 09:21:35 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
#include "os_thread_manager.hpp"
|
2021-10-01 02:00:47 +00:00
|
|
|
#include "os_multiple_wait_impl.hpp"
|
|
|
|
#include "os_multiple_wait_holder_base.hpp"
|
|
|
|
#include "os_multiple_wait_holder_impl.hpp"
|
|
|
|
#include "os_multiple_wait_object_list.hpp"
|
2021-09-29 20:42:11 +00:00
|
|
|
#include "os_utility.hpp"
|
2020-04-08 09:21:35 +00:00
|
|
|
|
|
|
|
namespace ams::os::impl {
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
void SetupThreadObjectUnsafe(ThreadType *thread, void *platform, ThreadFunction function, void *arg, void *stack, size_t stack_size, s32 priority) {
|
|
|
|
/* Clear the thread object. */
|
|
|
|
std::memset(thread, 0, sizeof(*thread));
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
/* Setup objects. */
|
2021-03-22 03:30:40 +00:00
|
|
|
util::ConstructAt(thread->cs_thread);
|
|
|
|
util::ConstructAt(thread->cv_thread);
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2021-03-22 03:30:40 +00:00
|
|
|
util::ConstructAt(thread->all_threads_node);
|
|
|
|
util::ConstructAt(thread->waitlist);
|
2020-04-08 09:21:35 +00:00
|
|
|
|
|
|
|
/* Set member variables. */
|
2022-03-06 20:08:20 +00:00
|
|
|
thread->magic = os::ThreadType::Magic;
|
|
|
|
thread->stack_is_aliased = false;
|
|
|
|
thread->auto_registered = false;
|
|
|
|
thread->version = 0;
|
|
|
|
thread->function = function;
|
|
|
|
thread->argument = arg;
|
|
|
|
thread->original_stack = stack;
|
|
|
|
thread->stack = stack;
|
|
|
|
thread->stack_size = stack_size;
|
|
|
|
thread->base_priority = priority;
|
|
|
|
thread->suspend_count = 0;
|
|
|
|
thread->initial_fiber = nullptr;
|
|
|
|
thread->current_fiber = nullptr;
|
|
|
|
thread->name_buffer[0] = '\x00';
|
|
|
|
thread->name_pointer = thread->name_buffer;
|
|
|
|
|
|
|
|
/* Set platform variables. */
|
|
|
|
#if defined(AMS_OS_IMPL_USE_PTHREADS)
|
|
|
|
util::ConstructAt(thread->cs_pthread_exit);
|
|
|
|
util::ConstructAt(thread->cv_pthread_exit);
|
|
|
|
thread->exited_pthread = false;
|
|
|
|
|
|
|
|
std::memset(thread->tls_value_array, 0, sizeof(thread->tls_value_array));
|
|
|
|
AMS_UNUSED(platform);
|
|
|
|
#elif defined(ATMOSPHERE_OS_HORIZON)
|
|
|
|
std::memset(std::addressof(thread->sdk_internal_tls), 0, sizeof(thread->sdk_internal_tls));
|
|
|
|
thread->thread_impl = (platform != nullptr) ? static_cast<ThreadType::ThreadImpl *>(platform) : std::addressof(thread->thread_impl_storage);
|
|
|
|
#else
|
|
|
|
std::memset(thread->tls_value_array, 0, sizeof(thread->tls_value_array));
|
|
|
|
AMS_UNUSED(platform);
|
|
|
|
#endif
|
2020-06-30 06:19:33 +00:00
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
/* Mark initialized. */
|
|
|
|
thread->state = ThreadType::State_Initialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadManager::InvokeThread(ThreadType *thread) {
|
|
|
|
auto &manager = GetThreadManager();
|
|
|
|
|
|
|
|
manager.SetCurrentThread(thread);
|
|
|
|
manager.NotifyThreadNameChanged(thread);
|
|
|
|
|
|
|
|
{
|
2021-01-12 11:54:46 +00:00
|
|
|
GetReference(thread->cs_thread).Lock();
|
2020-04-08 09:21:35 +00:00
|
|
|
while (thread->state == ThreadType::State_Initialized) {
|
|
|
|
GetReference(thread->cv_thread).Wait(GetPointer(thread->cs_thread));
|
|
|
|
}
|
2021-01-12 11:54:46 +00:00
|
|
|
const auto new_state = thread->state;
|
|
|
|
GetReference(thread->cs_thread).Unlock();
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2021-01-12 11:54:46 +00:00
|
|
|
if (new_state == ThreadType::State_Started) {
|
2020-04-08 09:21:35 +00:00
|
|
|
thread->function(thread->argument);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.CleanupThread();
|
|
|
|
}
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
ThreadManager::ThreadManager() : m_impl(std::addressof(m_main_thread)), m_total_thread_stack_size(0), m_num_created_threads(0) {
|
2022-03-06 20:08:20 +00:00
|
|
|
m_total_thread_stack_size = 0;
|
|
|
|
m_num_created_threads = 0;
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
m_main_thread.state = ThreadType::State_Started;
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
this->SetCurrentThread(std::addressof(m_main_thread));
|
|
|
|
this->PlaceThreadObjectUnderThreadManagerSafe(std::addressof(m_main_thread));
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
void ThreadManager::CleanupThread(ThreadType *thread) {
|
|
|
|
/* TODO: TLS Manager->InvokeTlsDestructors(); */
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
std::scoped_lock lk(GetReference(thread->cs_thread));
|
|
|
|
|
|
|
|
thread->state = ThreadType::State_Terminated;
|
|
|
|
|
|
|
|
GetReference(thread->cv_thread).Broadcast();
|
|
|
|
GetReference(thread->waitlist).SignalAllThreads();
|
|
|
|
}
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
void ThreadManager::CleanupThread() {
|
|
|
|
return this->CleanupThread(this->GetCurrentThread());
|
|
|
|
}
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
bool ThreadManager::CreateAliasStackUnsafe(ThreadType *thread) {
|
|
|
|
void *alias_stack;
|
|
|
|
if (m_impl.MapAliasStack(std::addressof(alias_stack), thread->stack, thread->stack_size)) {
|
|
|
|
thread->stack_is_aliased = true;
|
|
|
|
thread->stack = alias_stack;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
void ThreadManager::DeleteAliasStackUnsafe(ThreadType *thread) {
|
|
|
|
AMS_ABORT_UNLESS(m_impl.UnmapAliasStack(thread->stack, thread->original_stack, thread->stack_size));
|
|
|
|
|
|
|
|
thread->stack_is_aliased = false;
|
|
|
|
thread->stack = thread->original_stack;
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
Result ThreadManager::CreateThread(ThreadType *thread, ThreadFunction function, void *argument, void *stack, size_t stack_size, s32 priority, s32 ideal_core) {
|
|
|
|
SetupThreadObjectUnsafe(thread, nullptr, function, argument, stack, stack_size, priority);
|
2022-03-06 20:08:20 +00:00
|
|
|
AMS_ABORT_UNLESS(this->CreateAliasStackUnsafe(thread));
|
|
|
|
ON_RESULT_FAILURE {
|
|
|
|
this->DeleteAliasStackUnsafe(thread);
|
|
|
|
thread->state = ThreadType::State_NotInitialized;
|
|
|
|
};
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
R_TRY(m_impl.CreateThread(thread, ideal_core));
|
2020-04-08 09:21:35 +00:00
|
|
|
|
|
|
|
this->PlaceThreadObjectUnderThreadManagerSafe(thread);
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
R_SUCCEED();
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ThreadManager::CreateThread(ThreadType *thread, ThreadFunction function, void *argument, void *stack, size_t stack_size, s32 priority) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(this->CreateThread(thread, function, argument, stack, stack_size, priority, m_impl.GetDefaultCoreNumber()));
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadManager::DestroyThread(ThreadType *thread) {
|
|
|
|
{
|
|
|
|
std::scoped_lock lk(GetReference(thread->cs_thread));
|
|
|
|
|
|
|
|
if (thread->state == ThreadType::State_Initialized) {
|
|
|
|
thread->state = ThreadType::State_DestroyedBeforeStarted;
|
2021-10-10 07:14:06 +00:00
|
|
|
m_impl.StartThread(thread);
|
2020-04-08 09:21:35 +00:00
|
|
|
GetReference(thread->cv_thread).Signal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
m_impl.WaitForThreadExit(thread);
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
this->DestroyThreadObject(thread);
|
|
|
|
}
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
void ThreadManager::DestroyThreadObject(ThreadType *thread) {
|
2020-04-08 09:21:35 +00:00
|
|
|
{
|
|
|
|
std::scoped_lock lk(GetReference(thread->cs_thread));
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
{
|
|
|
|
std::scoped_lock tlk(m_cs);
|
|
|
|
this->EraseFromAllThreadsListUnsafe(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread->stack_is_aliased) {
|
|
|
|
this->DeleteAliasStackUnsafe(thread);
|
|
|
|
}
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
m_impl.DestroyThreadUnsafe(thread);
|
2020-04-08 09:21:35 +00:00
|
|
|
|
|
|
|
thread->state = ThreadType::State_NotInitialized;
|
|
|
|
|
|
|
|
thread->name_buffer[0] = '\x00';
|
2021-07-20 00:35:00 +00:00
|
|
|
thread->magic = 0xCCCC;
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
util::DestroyAt(thread->waitlist);
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
2022-03-06 20:08:20 +00:00
|
|
|
util::DestroyAt(thread->cs_thread);
|
|
|
|
util::DestroyAt(thread->cv_thread);
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadManager::StartThread(ThreadType *thread) {
|
|
|
|
std::scoped_lock lk(GetReference(thread->cs_thread));
|
|
|
|
|
|
|
|
AMS_ASSERT(thread->state == ThreadType::State_Initialized);
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
m_impl.StartThread(thread);
|
2020-04-08 09:21:35 +00:00
|
|
|
thread->state = ThreadType::State_Started;
|
|
|
|
|
|
|
|
GetReference(thread->cv_thread).Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadManager::WaitThread(ThreadType *thread) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_impl.WaitForThreadExit(thread);
|
2020-04-08 09:21:35 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
std::scoped_lock lk(GetReference(thread->cs_thread));
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
if (thread->stack_is_aliased) {
|
|
|
|
this->DeleteAliasStackUnsafe(thread);
|
|
|
|
}
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ThreadManager::TryWaitThread(ThreadType *thread) {
|
2021-10-10 07:14:06 +00:00
|
|
|
const bool result = m_impl.TryWaitForThreadExit(thread);
|
2020-04-08 09:21:35 +00:00
|
|
|
|
|
|
|
if (result) {
|
|
|
|
std::scoped_lock lk(GetReference(thread->cs_thread));
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
if (thread->stack_is_aliased) {
|
|
|
|
this->DeleteAliasStackUnsafe(thread);
|
|
|
|
}
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 ThreadManager::SuspendThread(ThreadType *thread) {
|
|
|
|
std::scoped_lock lk(GetReference(thread->cs_thread));
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
const auto prev_suspend_count = thread->suspend_count;
|
2020-04-08 09:21:35 +00:00
|
|
|
AMS_ASSERT(prev_suspend_count < ThreadSuspendCountMax);
|
|
|
|
thread->suspend_count = prev_suspend_count + 1;
|
|
|
|
|
|
|
|
if (prev_suspend_count == 0) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_impl.SuspendThreadUnsafe(thread);
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
|
|
|
return prev_suspend_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 ThreadManager::ResumeThread(ThreadType *thread) {
|
|
|
|
std::scoped_lock lk(GetReference(thread->cs_thread));
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
const auto prev_suspend_count = thread->suspend_count;
|
2020-04-08 09:21:35 +00:00
|
|
|
if (prev_suspend_count > 0) {
|
|
|
|
thread->suspend_count = prev_suspend_count - 1;
|
|
|
|
if (prev_suspend_count == 1) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_impl.ResumeThreadUnsafe(thread);
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return prev_suspend_count;
|
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#if !defined(ATMOSPHERE_OS_HORIZON)
|
|
|
|
void ThreadManager::SetZeroToAllThreadsTlsSafe(int slot) {
|
|
|
|
std::scoped_lock lk(m_cs);
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
for (auto it = m_all_threads_list.begin(); it != m_all_threads_list.end(); ++it) {
|
|
|
|
it->tls_value_array[slot] = 0;
|
|
|
|
}
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
2022-03-06 20:08:20 +00:00
|
|
|
#endif
|
2020-04-08 09:21:35 +00:00
|
|
|
|
|
|
|
/* TODO void ThreadManager::GetThreadContext(ThreadContextInfo *out_context, const ThreadType *thread); */
|
|
|
|
|
2021-10-01 07:41:35 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr inline const char MainThreadName[] = "MainThread";
|
|
|
|
constexpr inline const char ThreadNamePrefix[] = "Thread_0x";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
void ThreadManager::SetInitialThreadNameUnsafe(ThreadType *thread) {
|
2021-10-10 07:14:06 +00:00
|
|
|
if (thread == std::addressof(m_main_thread)) {
|
2020-04-08 09:21:35 +00:00
|
|
|
static_assert(sizeof(thread->name_buffer) >= sizeof(MainThreadName));
|
|
|
|
static_assert(MainThreadName[sizeof(MainThreadName) - 1] == '\x00');
|
|
|
|
std::memcpy(thread->name_buffer, MainThreadName, sizeof(MainThreadName));
|
|
|
|
} else {
|
|
|
|
constexpr size_t ThreadNamePrefixSize = sizeof(ThreadNamePrefix) - 1;
|
|
|
|
const u64 func = reinterpret_cast<u64>(thread->function);
|
|
|
|
static_assert(ThreadNamePrefixSize + sizeof(func) * 2 + 1 <= sizeof(thread->name_buffer));
|
2021-09-29 20:42:11 +00:00
|
|
|
std::memcpy(thread->name_buffer, ThreadNamePrefix, ThreadNamePrefixSize);
|
|
|
|
os::impl::ExpandUnsignedValueToAscii(thread->name_buffer + ThreadNamePrefixSize, func);
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
thread->name_pointer = thread->name_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|