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

81 lines
2.7 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_async_impl.hpp"
#include "sysupdater_async_thread_allocator.hpp"
namespace ams::mitm::sysupdater {
Result AsyncBase::ToAsyncResult(Result result) {
R_TRY_CATCH(result) {
R_CONVERT(nim::ResultHttpConnectionCanceled, ns::ResultCanceled());
R_CONVERT(ncm::ResultInstallTaskCancelled, ns::ResultCanceled());
} R_END_TRY_CATCH;
return ResultSuccess();
}
AsyncPrepareSdCardUpdateImpl::~AsyncPrepareSdCardUpdateImpl() {
2021-10-10 09:14:06 +02:00
if (m_thread_info) {
os::WaitThread(m_thread_info->thread);
os::DestroyThread(m_thread_info->thread);
GetAsyncThreadAllocator()->Free(*m_thread_info);
}
}
Result AsyncPrepareSdCardUpdateImpl::Run() {
/* Get a thread info. */
ThreadInfo info;
R_TRY(GetAsyncThreadAllocator()->Allocate(std::addressof(info)));
/* Set the thread info's priority. */
info.priority = AMS_GET_SYSTEM_THREAD_PRIORITY(mitm_sysupdater, AsyncPrepareSdCardUpdateTask);
/* Ensure that we clean up appropriately. */
ON_SCOPE_EXIT {
2021-10-10 09:14:06 +02:00
if (!m_thread_info) {
GetAsyncThreadAllocator()->Free(info);
}
};
/* Create a thread for the task. */
R_TRY(os::CreateThread(info.thread, [](void *arg) {
2021-10-10 09:14:06 +02:00
auto *async = reinterpret_cast<AsyncPrepareSdCardUpdateImpl *>(arg);
async->m_result = async->Execute();
async->m_event.Signal();
}, this, info.stack, info.stack_size, info.priority));
/* Set the thread name. */
os::SetThreadNamePointer(info.thread, AMS_GET_SYSTEM_THREAD_NAME(mitm_sysupdater, AsyncPrepareSdCardUpdateTask));
/* Start the thread. */
os::StartThread(info.thread);
/* Set our thread info. */
2021-10-10 09:14:06 +02:00
m_thread_info = info;
return ResultSuccess();
}
Result AsyncPrepareSdCardUpdateImpl::Execute() {
2021-10-10 09:14:06 +02:00
return m_task->PrepareAndExecute();
}
void AsyncPrepareSdCardUpdateImpl::CancelImpl() {
2021-10-10 09:14:06 +02:00
m_task->Cancel();
}
}