Atmosphere/stratosphere/ams_mitm/source/sysupdater/sysupdater_thread_allocator.cpp

56 lines
1.8 KiB
C++
Raw Normal View History

/*
* Copyright (c) Atmosphère-NX
*
* 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 "sysupdater_thread_allocator.hpp"
namespace ams::mitm::sysupdater {
Result ThreadAllocator::Allocate(ThreadInfo *out) {
2021-10-10 07:14:06 +00:00
std::scoped_lock lk(m_mutex);
2021-10-10 07:14:06 +00:00
for (int i = 0; i < m_thread_count; ++i) {
const u64 mask = (static_cast<u64>(1) << i);
2021-10-10 07:14:06 +00:00
if ((m_bitmap & mask) == 0) {
*out = {
2021-10-10 07:14:06 +00:00
.thread = m_thread_list + i,
.priority = m_thread_priority,
.stack = m_stack_heap + (m_stack_size * i),
.stack_size = m_stack_size,
};
2021-10-10 07:14:06 +00:00
m_bitmap |= mask;
2022-03-26 07:14:36 +00:00
R_SUCCEED();
}
}
2022-03-26 07:14:36 +00:00
R_THROW(ns::ResultOutOfMaxRunningTask());
}
void ThreadAllocator::Free(const ThreadInfo &info) {
2021-10-10 07:14:06 +00:00
std::scoped_lock lk(m_mutex);
2021-10-10 07:14:06 +00:00
for (int i = 0; i < m_thread_count; ++i) {
if (info.thread == std::addressof(m_thread_list[i])) {
const u64 mask = (static_cast<u64>(1) << i);
2021-10-10 07:14:06 +00:00
m_bitmap &= ~mask;
return;
}
}
AMS_ABORT("Invalid thread passed to ThreadAllocator::Free");
}
}