Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11622 from JosJuice/tas-input-nonblocking
DolphinQt: Rework TAS input threading
- Loading branch information
Showing
14 changed files
with
276 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2023 Dolphin Emulator Project | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#pragma once | ||
|
||
#include <atomic> | ||
|
||
#include "Common/CommonTypes.h" | ||
|
||
class TASControlState | ||
{ | ||
public: | ||
// Call this from the CPU thread to get the current value. (This function can also safely be | ||
// called from the UI thread, but you're effectively just getting the value the UI control has.) | ||
u16 GetValue() const; | ||
// Call this from the CPU thread when the controller state changes. | ||
// If the return value is true, queue up a call to ApplyControllerChangeValue on the UI thread. | ||
bool OnControllerValueChanged(u16 new_value); | ||
// Call this from the UI thread when the user changes the value using the UI. | ||
void OnUIValueChanged(u16 new_value); | ||
// Call this from the UI thread after OnControllerValueChanged returns true, | ||
// and set the state of the UI control to the return value. | ||
u16 ApplyControllerValueChange(); | ||
|
||
private: | ||
// A description of how threading is handled: The UI thread can update its copy of the state | ||
// whenever it wants to, and must *not* increment the version when doing so. The CPU thread can | ||
// update its copy of the state whenever it wants to, and *must* increment the version when doing | ||
// so. When the CPU thread updates its copy of the state, the UI thread should then (possibly | ||
// after a delay) mirror the change by copying the CPU thread's state to the UI thread's state. | ||
// This mirroring is the only way for the version number stored in the UI thread's state to | ||
// change. The version numbers of the two copies can be compared to check if the UI thread's view | ||
// of what has happened on the CPU thread is up to date. | ||
|
||
struct State | ||
{ | ||
u16 version = 0; | ||
u16 value = 0; | ||
}; | ||
|
||
std::atomic<State> m_ui_thread_state; | ||
std::atomic<State> m_cpu_thread_state; | ||
}; |
Oops, something went wrong.