Merge pull request #13594 from jordan-woyak/state-cleanups

State: Simplify interthread communication and general cleanups.
This commit is contained in:
Jordan Woyak
2026-02-03 16:50:52 -06:00
committed by GitHub
14 changed files with 581 additions and 373 deletions
+1
View File
@@ -151,6 +151,7 @@ add_library(common
Timer.h
TimeUtil.cpp
TimeUtil.h
TransferableSharedMutex.h
TraversalClient.cpp
TraversalClient.h
TraversalProto.h
@@ -0,0 +1,92 @@
// Copyright 2025 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <atomic>
#include <cassert>
#include <cstdint>
namespace Common
{
// Behaves like `std::shared_mutex` but locks and unlocks may come from different threads.
class TransferableSharedMutex
{
public:
void lock()
{
while (true)
{
CounterType old_value{};
if (m_counter.compare_exchange_strong(old_value, EXCLUSIVE_LOCK_VALUE,
std::memory_order_acquire, std::memory_order_relaxed))
{
return;
}
// lock() or lock_shared() is already held.
// Wait for an unlock notification and try again.
m_counter.wait(old_value, std::memory_order_relaxed);
}
}
bool try_lock()
{
CounterType old_value{};
return m_counter.compare_exchange_weak(old_value, EXCLUSIVE_LOCK_VALUE,
std::memory_order_acquire, std::memory_order_relaxed);
}
void unlock()
{
m_counter.store(0, std::memory_order_release);
m_counter.notify_all(); // Notify potentially multiple wait()ers in lock_shared().
}
void lock_shared()
{
while (true)
{
auto old_value = m_counter.load(std::memory_order_relaxed);
while (old_value < LAST_SHARED_LOCK_VALUE)
{
if (m_counter.compare_exchange_strong(old_value, old_value + 1, std::memory_order_acquire,
std::memory_order_relaxed))
{
return;
}
}
// Something has gone very wrong if m_counter is nearly saturated with shared_lock().
assert(old_value != LAST_SHARED_LOCK_VALUE);
// lock() is already held.
// Wait for an unlock notification and try again.
m_counter.wait(old_value, std::memory_order_relaxed);
}
}
bool try_lock_shared()
{
auto old_value = m_counter.load(std::memory_order_relaxed);
return (old_value < LAST_SHARED_LOCK_VALUE) &&
m_counter.compare_exchange_weak(old_value, old_value + 1, std::memory_order_acquire,
std::memory_order_relaxed);
}
void unlock_shared()
{
if (m_counter.fetch_sub(1, std::memory_order_release) == 1)
m_counter.notify_one(); // Notify one of the wait()ers in lock().
}
private:
using CounterType = std::uintptr_t;
static constexpr auto EXCLUSIVE_LOCK_VALUE = CounterType(-1);
static constexpr auto LAST_SHARED_LOCK_VALUE = EXCLUSIVE_LOCK_VALUE - 1;
std::atomic<CounterType> m_counter{};
};
} // namespace Common
+310 -340
View File
File diff suppressed because it is too large Load Diff
+4 -15
View File
@@ -10,7 +10,6 @@
#include <string>
#include <type_traits>
#include "Common/Buffer.h"
#include "Common/CommonTypes.h"
namespace Core
@@ -81,13 +80,8 @@ struct StateExtendedHeader
};
void Init(Core::System& system);
void Shutdown();
void EnableCompression(bool compression);
bool ReadHeader(const std::string& filename, StateHeader& header);
// Returns a string containing information of the savestate in the given slot
// which can be presented to the user for identification purposes
std::string GetInfoStringOfSlot(int slot, bool translate = true);
@@ -97,17 +91,12 @@ u64 GetUnixTimeOfSlot(int slot);
// These don't happen instantly - they get scheduled as events.
// ...But only if we're not in the main CPU thread.
// If we're in the main CPU thread then they run immediately instead
// because some things (like Lua) need them to run immediately.
// Slots from 0-99.
void Save(Core::System& system, int slot, bool wait = false);
// If we're in the main CPU thread then they run immediately instead.
void Save(Core::System& system, int slot);
void Load(Core::System& system, int slot);
void SaveAs(Core::System& system, const std::string& filename, bool wait = false);
void LoadAs(Core::System& system, const std::string& filename);
void SaveToBuffer(Core::System& system, Common::UniqueBuffer<u8>& buffer);
void LoadFromBuffer(Core::System& system, Common::UniqueBuffer<u8>& buffer);
void SaveAs(Core::System& system, std::string filename);
void LoadAs(Core::System& system, std::string filename);
void LoadLastSaved(Core::System& system, int i = 1);
void SaveFirstSaved(Core::System& system);
+1
View File
@@ -171,6 +171,7 @@
<ClInclude Include="Common\Thread.h" />
<ClInclude Include="Common\Timer.h" />
<ClInclude Include="Common\TimeUtil.h" />
<ClInclude Include="Common\TransferableSharedMutex.h" />
<ClInclude Include="Common\TraversalClient.h" />
<ClInclude Include="Common\TraversalProto.h" />
<ClInclude Include="Common\TypeUtils.h" />
+12
View File
@@ -60,6 +60,7 @@
namespace UICommon
{
static Config::ConfigChangedCallbackID s_config_changed_callback_id;
static Common::HookableEvent<> s_flush_unsaved_data_event_hook;
static void CreateDumpPath(std::string path)
{
@@ -157,6 +158,17 @@ void Shutdown()
Config::Shutdown();
}
[[nodiscard]] Common::EventHook AddFlushUnsavedDataCallback(std::function<void()> callback)
{
return s_flush_unsaved_data_event_hook.Register(std::move(callback));
}
void FlushUnsavedData()
{
INFO_LOG_FMT(CORE, "Flushing unsaved data...");
s_flush_unsaved_data_event_hook.Trigger();
}
void InitControllers(const WindowSystemInfo& wsi)
{
if (g_controller_interface.IsInit())
+5
View File
@@ -6,6 +6,7 @@
#include <string>
#include "Common/CommonTypes.h"
#include "Common/HookableEvent.h"
struct WindowSystemInfo;
@@ -14,6 +15,10 @@ namespace UICommon
void Init();
void Shutdown();
// Triggered from the Host-thread on Android before a potential process termination.
[[nodiscard]] Common::EventHook AddFlushUnsavedDataCallback(std::function<void()> callback);
void FlushUnsavedData();
void InitControllers(const WindowSystemInfo& wsi);
void ShutdownControllers();