This commit is contained in:
Jordan Woyak 2025-04-27 20:09:15 -05:00 committed by GitHub
commit 08f1644ccb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 65 additions and 8 deletions

View file

@ -70,6 +70,7 @@ add_library(common
Flag.h
FloatUtils.cpp
FloatUtils.h
Functional.h
FormatUtil.h
FPURoundMode.h
GekkoDisassembler.cpp

View file

@ -0,0 +1,51 @@
// Copyright 2025 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <memory>
// TODO C++23: Replace with std::move_only_function.
namespace Common
{
template <typename T>
class MoveOnlyFunction;
template <typename R, typename... Args>
class MoveOnlyFunction<R(Args...)>
{
public:
using result_type = R;
MoveOnlyFunction() = default;
template <typename F>
MoveOnlyFunction(F&& f) : m_ptr{std::make_unique<Func<F>>(std::forward<F>(f))}
{
}
result_type operator()(Args... args) const { return m_ptr->Invoke(std::forward<Args>(args)...); }
explicit operator bool() const { return m_ptr != nullptr; };
void swap(MoveOnlyFunction& other) { m_ptr.swap(other.m_ptr); }
private:
struct FuncBase
{
virtual ~FuncBase() = default;
virtual result_type Invoke(Args...) = 0;
};
template <typename F>
struct Func : FuncBase
{
Func(F&& f) : func{std::forward<F>(f)} {}
result_type Invoke(Args... args) override { return func(std::forward<Args>(args)...); }
F func;
};
std::unique_ptr<FuncBase> m_ptr;
};
} // namespace Common

View file

@ -809,7 +809,8 @@ static bool PauseAndLock(Core::System& system, bool do_lock, bool unpause_on_unl
return was_unpaused;
}
void RunOnCPUThread(Core::System& system, std::function<void()> function, bool wait_for_completion)
void RunOnCPUThread(Core::System& system, Common::MoveOnlyFunction<void()> function,
bool wait_for_completion)
{
// If the CPU thread is not running, assume there is no active CPU thread we can race against.
if (!IsRunning(system) || IsCPUThread())

View file

@ -15,6 +15,7 @@
#include <string_view>
#include "Common/CommonTypes.h"
#include "Common/Functional.h"
struct BootParameters;
struct WindowSystemInfo;
@ -159,7 +160,8 @@ void OnFrameEnd(Core::System& system);
// Run a function on the CPU thread, asynchronously.
// This is only valid to call from the host thread, since it uses PauseAndLock() internally.
void RunOnCPUThread(Core::System& system, std::function<void()> function, bool wait_for_completion);
void RunOnCPUThread(Core::System& system, Common::MoveOnlyFunction<void()> function,
bool wait_for_completion);
// for calling back into UI code without introducing a dependency on it in core
using StateChangedCallbackFunc = std::function<void(Core::State)>;

View file

@ -60,7 +60,7 @@ void CPUManager::ExecutePendingJobs(std::unique_lock<std::mutex>& state_lock)
{
while (!m_pending_jobs.empty())
{
auto callback = m_pending_jobs.front();
auto callback = std::move(m_pending_jobs.front());
m_pending_jobs.pop();
state_lock.unlock();
callback();
@ -414,7 +414,7 @@ bool CPUManager::PauseAndLock(bool do_lock, bool unpause_on_unlock, bool control
return was_unpaused;
}
void CPUManager::AddCPUThreadJob(std::function<void()> function)
void CPUManager::AddCPUThreadJob(Common::MoveOnlyFunction<void()> function)
{
std::unique_lock state_lock(m_state_change_lock);
m_pending_jobs.push(std::move(function));

View file

@ -4,11 +4,11 @@
#pragma once
#include <condition_variable>
#include <functional>
#include <mutex>
#include <queue>
#include "Common/Event.h"
#include "Common/Functional.h"
namespace Common
{
@ -99,7 +99,7 @@ public:
// Adds a job to be executed during on the CPU thread. This should be combined with
// PauseAndLock(), as while the CPU is in the run loop, it won't execute the function.
void AddCPUThreadJob(std::function<void()> function);
void AddCPUThreadJob(Common::MoveOnlyFunction<void()> function);
private:
void FlushStepSyncEventLocked();
@ -135,7 +135,7 @@ private:
bool m_state_system_request_stepping = false;
bool m_state_cpu_step_instruction = false;
Common::Event* m_state_cpu_step_instruction_sync = nullptr;
std::queue<std::function<void()>> m_pending_jobs;
std::queue<Common::MoveOnlyFunction<void()>> m_pending_jobs;
Common::Event m_time_played_finish_sync;
Core::System& m_system;

View file

@ -62,6 +62,7 @@
<ClInclude Include="Common\FloatUtils.h" />
<ClInclude Include="Common\FormatUtil.h" />
<ClInclude Include="Common\FPURoundMode.h" />
<ClInclude Include="Common\Functional.h" />
<ClInclude Include="Common\GekkoDisassembler.h" />
<ClInclude Include="Common\GL\GLContext.h" />
<ClInclude Include="Common\GL\GLExtensions\AMD_pinned_memory.h" />

View file

@ -10,6 +10,7 @@
#include <queue>
#include "Common/Flag.h"
#include "Common/Functional.h"
struct EfbPokeData;
class PointerWrap;
@ -60,7 +61,7 @@ public:
static AsyncRequests* GetInstance() { return &s_singleton; }
private:
using Event = std::function<void()>;
using Event = Common::MoveOnlyFunction<void()>;
void QueueEvent(Event&& event);