mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-21 05:09:34 -06:00
Merge pull request #13602 from jordan-woyak/move-only-function
Common: Add MoveOnlyFunction.
This commit is contained in:
@ -70,6 +70,7 @@ add_library(common
|
|||||||
Flag.h
|
Flag.h
|
||||||
FloatUtils.cpp
|
FloatUtils.cpp
|
||||||
FloatUtils.h
|
FloatUtils.h
|
||||||
|
Functional.h
|
||||||
FormatUtil.h
|
FormatUtil.h
|
||||||
FPURoundMode.h
|
FPURoundMode.h
|
||||||
GekkoDisassembler.cpp
|
GekkoDisassembler.cpp
|
||||||
|
51
Source/Core/Common/Functional.h
Normal file
51
Source/Core/Common/Functional.h
Normal 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
|
@ -809,7 +809,8 @@ static bool PauseAndLock(Core::System& system, bool do_lock, bool unpause_on_unl
|
|||||||
return was_unpaused;
|
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 the CPU thread is not running, assume there is no active CPU thread we can race against.
|
||||||
if (!IsRunning(system) || IsCPUThread())
|
if (!IsRunning(system) || IsCPUThread())
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
#include "Common/CommonTypes.h"
|
#include "Common/CommonTypes.h"
|
||||||
|
#include "Common/Functional.h"
|
||||||
|
|
||||||
struct BootParameters;
|
struct BootParameters;
|
||||||
struct WindowSystemInfo;
|
struct WindowSystemInfo;
|
||||||
@ -159,7 +160,8 @@ void OnFrameEnd(Core::System& system);
|
|||||||
|
|
||||||
// Run a function on the CPU thread, asynchronously.
|
// Run a function on the CPU thread, asynchronously.
|
||||||
// This is only valid to call from the host thread, since it uses PauseAndLock() internally.
|
// 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
|
// for calling back into UI code without introducing a dependency on it in core
|
||||||
using StateChangedCallbackFunc = std::function<void(Core::State)>;
|
using StateChangedCallbackFunc = std::function<void(Core::State)>;
|
||||||
|
@ -60,7 +60,7 @@ void CPUManager::ExecutePendingJobs(std::unique_lock<std::mutex>& state_lock)
|
|||||||
{
|
{
|
||||||
while (!m_pending_jobs.empty())
|
while (!m_pending_jobs.empty())
|
||||||
{
|
{
|
||||||
auto callback = m_pending_jobs.front();
|
auto callback = std::move(m_pending_jobs.front());
|
||||||
m_pending_jobs.pop();
|
m_pending_jobs.pop();
|
||||||
state_lock.unlock();
|
state_lock.unlock();
|
||||||
callback();
|
callback();
|
||||||
@ -414,7 +414,7 @@ bool CPUManager::PauseAndLock(bool do_lock, bool unpause_on_unlock, bool control
|
|||||||
return was_unpaused;
|
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);
|
std::unique_lock state_lock(m_state_change_lock);
|
||||||
m_pending_jobs.push(std::move(function));
|
m_pending_jobs.push(std::move(function));
|
||||||
|
@ -4,11 +4,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <functional>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
#include "Common/Event.h"
|
#include "Common/Event.h"
|
||||||
|
#include "Common/Functional.h"
|
||||||
|
|
||||||
namespace Common
|
namespace Common
|
||||||
{
|
{
|
||||||
@ -99,7 +99,7 @@ public:
|
|||||||
|
|
||||||
// Adds a job to be executed during on the CPU thread. This should be combined with
|
// 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.
|
// 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:
|
private:
|
||||||
void FlushStepSyncEventLocked();
|
void FlushStepSyncEventLocked();
|
||||||
@ -135,7 +135,7 @@ private:
|
|||||||
bool m_state_system_request_stepping = false;
|
bool m_state_system_request_stepping = false;
|
||||||
bool m_state_cpu_step_instruction = false;
|
bool m_state_cpu_step_instruction = false;
|
||||||
Common::Event* m_state_cpu_step_instruction_sync = nullptr;
|
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;
|
Common::Event m_time_played_finish_sync;
|
||||||
|
|
||||||
Core::System& m_system;
|
Core::System& m_system;
|
||||||
|
@ -62,6 +62,7 @@
|
|||||||
<ClInclude Include="Common\FloatUtils.h" />
|
<ClInclude Include="Common\FloatUtils.h" />
|
||||||
<ClInclude Include="Common\FormatUtil.h" />
|
<ClInclude Include="Common\FormatUtil.h" />
|
||||||
<ClInclude Include="Common\FPURoundMode.h" />
|
<ClInclude Include="Common\FPURoundMode.h" />
|
||||||
|
<ClInclude Include="Common\Functional.h" />
|
||||||
<ClInclude Include="Common\GekkoDisassembler.h" />
|
<ClInclude Include="Common\GekkoDisassembler.h" />
|
||||||
<ClInclude Include="Common\GL\GLContext.h" />
|
<ClInclude Include="Common\GL\GLContext.h" />
|
||||||
<ClInclude Include="Common\GL\GLExtensions\AMD_pinned_memory.h" />
|
<ClInclude Include="Common\GL\GLExtensions\AMD_pinned_memory.h" />
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
#include "Common/Flag.h"
|
#include "Common/Flag.h"
|
||||||
|
#include "Common/Functional.h"
|
||||||
|
|
||||||
struct EfbPokeData;
|
struct EfbPokeData;
|
||||||
class PointerWrap;
|
class PointerWrap;
|
||||||
@ -60,7 +61,7 @@ public:
|
|||||||
static AsyncRequests* GetInstance() { return &s_singleton; }
|
static AsyncRequests* GetInstance() { return &s_singleton; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using Event = std::function<void()>;
|
using Event = Common::MoveOnlyFunction<void()>;
|
||||||
|
|
||||||
void QueueEvent(Event&& event);
|
void QueueEvent(Event&& event);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user