summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinQt/TAS/TASControlState.cpp
diff options
context:
space:
mode:
authorAdmiral H. Curtiss <pikachu025@gmail.com>2023-03-09 16:08:30 +0100
committerGitHub <noreply@github.com>2023-03-09 16:08:30 +0100
commit234c5dd90e31cbbe6782b4a6c6ae5f2f578e199b (patch)
treecdc1505c7932d8de0f129b06437d686e27445685 /Source/Core/DolphinQt/TAS/TASControlState.cpp
parenta6b1b2f9a50b41d491115a8fb08f33a4a5dcf586 (diff)
parent0300b44d23b149a0b93dd0aa2e77d20a02882ddf (diff)
Merge pull request #11622 from JosJuice/tas-input-nonblocking
DolphinQt: Rework TAS input threading
Diffstat (limited to 'Source/Core/DolphinQt/TAS/TASControlState.cpp')
-rw-r--r--Source/Core/DolphinQt/TAS/TASControlState.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/Source/Core/DolphinQt/TAS/TASControlState.cpp b/Source/Core/DolphinQt/TAS/TASControlState.cpp
new file mode 100644
index 0000000000..fd0c209aa6
--- /dev/null
+++ b/Source/Core/DolphinQt/TAS/TASControlState.cpp
@@ -0,0 +1,58 @@
+// Copyright 2023 Dolphin Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "DolphinQt/TAS/TASControlState.h"
+
+#include <atomic>
+
+#include "Common/CommonTypes.h"
+
+u16 TASControlState::GetValue() const
+{
+ const State ui_thread_state = m_ui_thread_state.load(std::memory_order_relaxed);
+ const State cpu_thread_state = m_cpu_thread_state.load(std::memory_order_relaxed);
+
+ return (ui_thread_state.version != cpu_thread_state.version ? cpu_thread_state : ui_thread_state)
+ .value;
+}
+
+bool TASControlState::OnControllerValueChanged(u16 new_value)
+{
+ const State cpu_thread_state = m_cpu_thread_state.load(std::memory_order_relaxed);
+
+ if (cpu_thread_state.value == new_value)
+ {
+ // The CPU thread state is already up to date with the controller. No need to do anything
+ return false;
+ }
+
+ const State new_state{static_cast<u16>(cpu_thread_state.version + 1), new_value};
+ m_cpu_thread_state.store(new_state, std::memory_order_relaxed);
+
+ return true;
+}
+
+void TASControlState::OnUIValueChanged(u16 new_value)
+{
+ const State ui_thread_state = m_ui_thread_state.load(std::memory_order_relaxed);
+
+ const State new_state{ui_thread_state.version, new_value};
+ m_ui_thread_state.store(new_state, std::memory_order_relaxed);
+}
+
+u16 TASControlState::ApplyControllerValueChange()
+{
+ const State ui_thread_state = m_ui_thread_state.load(std::memory_order_relaxed);
+ const State cpu_thread_state = m_cpu_thread_state.load(std::memory_order_relaxed);
+
+ if (ui_thread_state.version == cpu_thread_state.version)
+ {
+ // The UI thread state is already up to date with the CPU thread. No need to do anything
+ return ui_thread_state.value;
+ }
+ else
+ {
+ m_ui_thread_state.store(cpu_thread_state, std::memory_order_relaxed);
+ return cpu_thread_state.value;
+ }
+}