2023-03-04 11:24:31 +00:00
|
|
|
// Copyright 2023 Dolphin Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#include "DolphinQt/TAS/TASControlState.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
|
2022-10-04 19:24:33 +00:00
|
|
|
int TASControlState::GetValue() const
|
2023-03-04 11:24:31 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-04 19:24:33 +00:00
|
|
|
bool TASControlState::OnControllerValueChanged(int new_value)
|
2023-03-04 11:24:31 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-04 19:24:33 +00:00
|
|
|
const State new_state{static_cast<int>(cpu_thread_state.version + 1), new_value};
|
2023-03-04 11:24:31 +00:00
|
|
|
m_cpu_thread_state.store(new_state, std::memory_order_relaxed);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-04 19:24:33 +00:00
|
|
|
void TASControlState::OnUIValueChanged(int new_value)
|
2023-03-04 11:24:31 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-04 19:24:33 +00:00
|
|
|
int TASControlState::ApplyControllerValueChange()
|
2023-03-04 11:24:31 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|