2020-10-30 23:36:11 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-10-30 23:36:11 +01: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>
|
|
|
|
|
|
|
|
namespace ams::ddsf {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum class LoopControlCommand {
|
|
|
|
None = 0,
|
|
|
|
Register = 1,
|
|
|
|
Unregister = 2,
|
|
|
|
Terminate = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct EventHandlerManager::LoopControlCommandParameters {
|
|
|
|
LoopControlCommand command;
|
|
|
|
IEventHandler *target;
|
|
|
|
|
|
|
|
LoopControlCommandParameters() : command(LoopControlCommand::None), target(nullptr) { /* ... */ }
|
|
|
|
LoopControlCommandParameters(LoopControlCommand c, IEventHandler *t) : command(c), target(t) { /* ... */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
void EventHandlerManager::Initialize() {
|
|
|
|
/* Check that we're not already initialized. */
|
2021-10-10 09:14:06 +02:00
|
|
|
if (m_is_initialized) {
|
2020-10-30 23:36:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-01 04:00:47 +02:00
|
|
|
/* Initialize multi wait/holder. */
|
2021-10-10 09:14:06 +02:00
|
|
|
os::InitializeMultiWait(std::addressof(m_multi_wait));
|
|
|
|
os::InitializeMultiWaitHolder(std::addressof(m_loop_control_event_holder), m_loop_control_event.GetBase());
|
|
|
|
os::LinkMultiWaitHolder(std::addressof(m_multi_wait), std::addressof(m_loop_control_event_holder));
|
2020-10-30 23:36:11 +01:00
|
|
|
|
2021-10-10 09:14:06 +02:00
|
|
|
m_is_initialized = true;
|
2020-10-30 23:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventHandlerManager::Finalize() {
|
|
|
|
/* Check that we're initialized and not looping. */
|
2021-10-10 09:14:06 +02:00
|
|
|
AMS_ASSERT(!m_is_looping);
|
|
|
|
AMS_ASSERT(m_is_initialized);
|
|
|
|
if (!m_is_initialized) {
|
2020-10-30 23:36:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-01 04:00:47 +02:00
|
|
|
/* Finalize multi wait/holder. */
|
2021-10-10 09:14:06 +02:00
|
|
|
os::UnlinkMultiWaitHolder(std::addressof(m_loop_control_event_holder));
|
|
|
|
os::FinalizeMultiWaitHolder(std::addressof(m_loop_control_event_holder));
|
|
|
|
os::FinalizeMultiWait(std::addressof(m_multi_wait));
|
2020-10-30 23:36:11 +01:00
|
|
|
|
2021-10-10 09:14:06 +02:00
|
|
|
m_is_initialized = false;
|
2020-10-30 23:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventHandlerManager::ProcessControlCommand(LoopControlCommandParameters *params) {
|
|
|
|
/* Check pre-conditions. */
|
2021-10-10 09:14:06 +02:00
|
|
|
AMS_ASSERT(m_is_initialized);
|
2020-10-30 23:36:11 +01:00
|
|
|
AMS_ASSERT(params != nullptr);
|
|
|
|
|
|
|
|
/* Acquire exclusive access. */
|
2021-10-10 09:14:06 +02:00
|
|
|
std::scoped_lock lk(m_loop_control_lock);
|
2020-10-30 23:36:11 +01:00
|
|
|
|
|
|
|
/* If we're processing for the loop thread, we can directly handle. */
|
2021-10-10 09:14:06 +02:00
|
|
|
if (!m_is_looping || this->IsRunningOnLoopThread()) {
|
2020-10-30 23:36:11 +01:00
|
|
|
this->ProcessControlCommandImpl(params);
|
|
|
|
} else {
|
|
|
|
/* Otherwise, signal to the loop thread. */
|
2021-10-10 09:14:06 +02:00
|
|
|
m_loop_control_command_params = params;
|
|
|
|
m_loop_control_event.Signal();
|
|
|
|
m_loop_control_command_done_event.Wait();
|
2020-10-30 23:36:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventHandlerManager::ProcessControlCommandImpl(LoopControlCommandParameters *params) {
|
|
|
|
/* Check pre-conditions. */
|
2021-10-10 09:14:06 +02:00
|
|
|
AMS_ASSERT(m_loop_control_lock.IsLockedByCurrentThread() || !m_loop_control_lock.TryLock());
|
2020-10-30 23:36:11 +01:00
|
|
|
AMS_ASSERT(params != nullptr);
|
|
|
|
AMS_ASSERT(params->target != nullptr);
|
|
|
|
|
|
|
|
/* Process the command. */
|
|
|
|
switch (params->command) {
|
|
|
|
case LoopControlCommand::Register:
|
2021-10-10 09:14:06 +02:00
|
|
|
params->target->Link(std::addressof(m_multi_wait));
|
2020-10-30 23:36:11 +01:00
|
|
|
break;
|
|
|
|
case LoopControlCommand::Unregister:
|
|
|
|
params->target->Unlink();
|
|
|
|
break;
|
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventHandlerManager::RegisterHandler(IEventHandler *handler) {
|
|
|
|
/* Check that the handler is valid. */
|
|
|
|
AMS_ASSERT(handler != nullptr);
|
|
|
|
AMS_ASSERT(handler->IsInitialized());
|
|
|
|
|
|
|
|
/* Send registration command. */
|
|
|
|
LoopControlCommandParameters params(LoopControlCommand::Register, handler);
|
|
|
|
return this->ProcessControlCommand(std::addressof(params));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventHandlerManager::UnregisterHandler(IEventHandler *handler) {
|
|
|
|
/* Check that the handler is valid. */
|
|
|
|
AMS_ASSERT(handler != nullptr);
|
|
|
|
AMS_ASSERT(handler->IsInitialized());
|
|
|
|
|
|
|
|
/* Send registration command. */
|
|
|
|
LoopControlCommandParameters params(LoopControlCommand::Unregister, handler);
|
|
|
|
return this->ProcessControlCommand(std::addressof(params));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventHandlerManager::WaitLoopEnter() {
|
|
|
|
/* Acquire exclusive access. */
|
2021-10-10 09:14:06 +02:00
|
|
|
std::scoped_lock lk(m_loop_control_lock);
|
2020-10-30 23:36:11 +01:00
|
|
|
|
|
|
|
/* Wait until we're looping. */
|
2021-10-10 09:14:06 +02:00
|
|
|
while (!m_is_looping) {
|
|
|
|
m_is_looping_cv.Wait(m_loop_control_lock);
|
2020-10-30 23:36:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventHandlerManager::WaitLoopExit() {
|
|
|
|
/* Acquire exclusive access. */
|
2021-10-10 09:14:06 +02:00
|
|
|
std::scoped_lock lk(m_loop_control_lock);
|
2020-10-30 23:36:11 +01:00
|
|
|
|
|
|
|
/* Wait until we're not looping. */
|
2021-10-10 09:14:06 +02:00
|
|
|
while (m_is_looping) {
|
|
|
|
m_is_looping_cv.Wait(m_loop_control_lock);
|
2020-10-30 23:36:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventHandlerManager::RequestStop() {
|
|
|
|
/* Check that we're looping and not the loop thread. */
|
2021-10-10 09:14:06 +02:00
|
|
|
AMS_ASSERT(m_is_looping);
|
2020-10-30 23:36:11 +01:00
|
|
|
AMS_ASSERT(!this->IsRunningOnLoopThread());
|
|
|
|
|
2021-10-10 09:14:06 +02:00
|
|
|
if (m_is_looping) {
|
2020-10-30 23:36:11 +01:00
|
|
|
/* Acquire exclusive access. */
|
2021-10-10 09:14:06 +02:00
|
|
|
std::scoped_lock lk(m_loop_control_lock);
|
2020-10-30 23:36:11 +01:00
|
|
|
|
|
|
|
/* Signal to the loop thread. */
|
|
|
|
LoopControlCommandParameters params(LoopControlCommand::Terminate, nullptr);
|
2021-10-10 09:14:06 +02:00
|
|
|
m_loop_control_command_params = std::addressof(params);
|
|
|
|
m_loop_control_event.Signal();
|
|
|
|
m_loop_control_command_done_event.Wait();
|
2020-10-30 23:36:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventHandlerManager::LoopAuto() {
|
|
|
|
/* Check that we're not already looping. */
|
2021-10-10 09:14:06 +02:00
|
|
|
AMS_ASSERT(!m_is_looping);
|
2020-10-30 23:36:11 +01:00
|
|
|
|
|
|
|
/* Begin looping with the current thread. */
|
2021-10-10 09:14:06 +02:00
|
|
|
m_loop_thread = os::GetCurrentThread();
|
|
|
|
m_is_looping = true;
|
|
|
|
m_is_looping_cv.Broadcast();
|
2020-10-30 23:36:11 +01:00
|
|
|
|
|
|
|
/* Whenever we're done looping, clean up. */
|
|
|
|
ON_SCOPE_EXIT {
|
2021-10-10 09:14:06 +02:00
|
|
|
m_loop_thread = nullptr;
|
|
|
|
m_is_looping = false;
|
|
|
|
m_is_looping_cv.Broadcast();
|
2020-10-30 23:36:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Loop until we're asked to stop. */
|
|
|
|
bool should_terminate = false;
|
|
|
|
while (!should_terminate) {
|
|
|
|
/* Wait for a holder to be signaled. */
|
2021-10-10 09:14:06 +02:00
|
|
|
os::MultiWaitHolderType *event_holder = os::WaitAny(std::addressof(m_multi_wait));
|
2020-10-30 23:36:11 +01:00
|
|
|
AMS_ASSERT(event_holder != nullptr);
|
|
|
|
|
|
|
|
/* Check if we have a request to handle. */
|
2021-10-10 09:14:06 +02:00
|
|
|
if (event_holder == std::addressof(m_loop_control_event_holder)) {
|
2020-10-30 23:36:11 +01:00
|
|
|
/* Check that the request hasn't already been handled. */
|
2021-10-10 09:14:06 +02:00
|
|
|
if (m_loop_control_event.TryWait()) {
|
2020-10-30 23:36:11 +01:00
|
|
|
/* Handle the request. */
|
2021-10-10 09:14:06 +02:00
|
|
|
AMS_ASSERT(m_loop_control_command_params != nullptr);
|
|
|
|
switch (m_loop_control_command_params->command) {
|
2020-10-30 23:36:11 +01:00
|
|
|
case LoopControlCommand::Register:
|
|
|
|
case LoopControlCommand::Unregister:
|
2021-10-10 09:14:06 +02:00
|
|
|
this->ProcessControlCommandImpl(m_loop_control_command_params);
|
2020-10-30 23:36:11 +01:00
|
|
|
break;
|
|
|
|
case LoopControlCommand::Terminate:
|
|
|
|
should_terminate = true;
|
|
|
|
break;
|
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear the request, and signal that it's done. */
|
2021-10-10 09:14:06 +02:00
|
|
|
m_loop_control_command_params = nullptr;
|
|
|
|
m_loop_control_command_done_event.Signal();
|
2020-10-30 23:36:11 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Handle the event. */
|
|
|
|
IEventHandler::ToEventHandler(event_holder).HandleEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|