2020-06-26 18:36:38 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-06-26 18:36:38 +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 "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);
|
2020-06-26 18:36:38 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
for (int i = 0; i < m_thread_count; ++i) {
|
2020-06-26 18:36:38 +00:00
|
|
|
const u64 mask = (static_cast<u64>(1) << i);
|
2021-10-10 07:14:06 +00:00
|
|
|
if ((m_bitmap & mask) == 0) {
|
2020-06-26 18:36:38 +00:00
|
|
|
*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,
|
2020-06-26 18:36:38 +00:00
|
|
|
};
|
2021-10-10 07:14:06 +00:00
|
|
|
m_bitmap |= mask;
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-06-26 18:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(ns::ResultOutOfMaxRunningTask());
|
2020-06-26 18:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadAllocator::Free(const ThreadInfo &info) {
|
2021-10-10 07:14:06 +00:00
|
|
|
std::scoped_lock lk(m_mutex);
|
2020-06-26 18:36:38 +00:00
|
|
|
|
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])) {
|
2020-06-26 18:36:38 +00:00
|
|
|
const u64 mask = (static_cast<u64>(1) << i);
|
2021-10-10 07:14:06 +00:00
|
|
|
m_bitmap &= ~mask;
|
2020-06-26 18:36:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AMS_ABORT("Invalid thread passed to ThreadAllocator::Free");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|