TASControlState.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2023 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DolphinQt/TAS/TASControlState.h"
  4. #include <atomic>
  5. #include "Common/CommonTypes.h"
  6. int TASControlState::GetValue() const
  7. {
  8. const State ui_thread_state = m_ui_thread_state.load(std::memory_order_relaxed);
  9. const State cpu_thread_state = m_cpu_thread_state.load(std::memory_order_relaxed);
  10. return (ui_thread_state.version != cpu_thread_state.version ? cpu_thread_state : ui_thread_state)
  11. .value;
  12. }
  13. bool TASControlState::OnControllerValueChanged(int new_value)
  14. {
  15. const State cpu_thread_state = m_cpu_thread_state.load(std::memory_order_relaxed);
  16. if (cpu_thread_state.value == new_value)
  17. {
  18. // The CPU thread state is already up to date with the controller. No need to do anything
  19. return false;
  20. }
  21. const State new_state{static_cast<int>(cpu_thread_state.version + 1), new_value};
  22. m_cpu_thread_state.store(new_state, std::memory_order_relaxed);
  23. return true;
  24. }
  25. void TASControlState::OnUIValueChanged(int new_value)
  26. {
  27. const State ui_thread_state = m_ui_thread_state.load(std::memory_order_relaxed);
  28. const State new_state{ui_thread_state.version, new_value};
  29. m_ui_thread_state.store(new_state, std::memory_order_relaxed);
  30. }
  31. int TASControlState::ApplyControllerValueChange()
  32. {
  33. const State ui_thread_state = m_ui_thread_state.load(std::memory_order_relaxed);
  34. const State cpu_thread_state = m_cpu_thread_state.load(std::memory_order_relaxed);
  35. if (ui_thread_state.version == cpu_thread_state.version)
  36. {
  37. // The UI thread state is already up to date with the CPU thread. No need to do anything
  38. return ui_thread_state.value;
  39. }
  40. else
  41. {
  42. m_ui_thread_state.store(cpu_thread_state, std::memory_order_relaxed);
  43. return cpu_thread_state.value;
  44. }
  45. }