summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinQt/TAS/TASControlState.cpp
blob: 0706c2ae18d3e268d8d0ba6016a566169baa3257 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "DolphinQt/TAS/TASControlState.h"

#include <atomic>

int 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(int 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<int>(cpu_thread_state.version + 1), new_value};
  m_cpu_thread_state.store(new_state, std::memory_order_relaxed);

  return true;
}

void TASControlState::OnUIValueChanged(int 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);
}

int 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;
  }
}