Use structs for config callback IDs

This way you can't mix up regular config callback IDs and CPU thread
config callback IDs. (It would be rather bad if you did!)
This commit is contained in:
JosJuice 2023-08-16 21:37:12 +02:00
parent 1104b93ee4
commit 7197e3abd0
17 changed files with 56 additions and 27 deletions

View File

@ -10,6 +10,7 @@
#include "AudioCommon/SurroundDecoder.h" #include "AudioCommon/SurroundDecoder.h"
#include "AudioCommon/WaveFile.h" #include "AudioCommon/WaveFile.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/Config/Config.h"
class PointerWrap; class PointerWrap;
@ -120,5 +121,5 @@ private:
int m_config_timing_variance; int m_config_timing_variance;
bool m_config_audio_stretch; bool m_config_audio_stretch;
size_t m_config_changed_callback_id; Config::ConfigChangedCallbackID m_config_changed_callback_id;
}; };

View File

@ -16,7 +16,7 @@ namespace Config
using Layers = std::map<LayerType, std::shared_ptr<Layer>>; using Layers = std::map<LayerType, std::shared_ptr<Layer>>;
static Layers s_layers; static Layers s_layers;
static std::vector<std::pair<size_t, ConfigChangedCallback>> s_callbacks; static std::vector<std::pair<ConfigChangedCallbackID, ConfigChangedCallback>> s_callbacks;
static size_t s_next_callback_id = 0; static size_t s_next_callback_id = 0;
static u32 s_callback_guards = 0; static u32 s_callback_guards = 0;
static std::atomic<u64> s_config_version = 0; static std::atomic<u64> s_config_version = 0;
@ -65,15 +65,15 @@ void RemoveLayer(LayerType layer)
OnConfigChanged(); OnConfigChanged();
} }
size_t AddConfigChangedCallback(ConfigChangedCallback func) ConfigChangedCallbackID AddConfigChangedCallback(ConfigChangedCallback func)
{ {
const size_t callback_id = s_next_callback_id; const ConfigChangedCallbackID callback_id{s_next_callback_id};
++s_next_callback_id; ++s_next_callback_id;
s_callbacks.emplace_back(std::make_pair(callback_id, std::move(func))); s_callbacks.emplace_back(std::make_pair(callback_id, std::move(func)));
return callback_id; return callback_id;
} }
void RemoveConfigChangedCallback(size_t callback_id) void RemoveConfigChangedCallback(ConfigChangedCallbackID callback_id)
{ {
for (auto it = s_callbacks.begin(); it != s_callbacks.end(); ++it) for (auto it = s_callbacks.begin(); it != s_callbacks.end(); ++it)
{ {

View File

@ -15,6 +15,14 @@
namespace Config namespace Config
{ {
struct ConfigChangedCallbackID
{
size_t id = -1;
bool operator==(const ConfigChangedCallbackID&) const = default;
bool operator!=(const ConfigChangedCallbackID&) const = default;
};
using ConfigChangedCallback = std::function<void()>; using ConfigChangedCallback = std::function<void()>;
// Layer management // Layer management
@ -24,8 +32,8 @@ void RemoveLayer(LayerType layer);
// Returns an ID that can be passed to RemoveConfigChangedCallback(). // Returns an ID that can be passed to RemoveConfigChangedCallback().
// The callback may be called from any thread. // The callback may be called from any thread.
size_t AddConfigChangedCallback(ConfigChangedCallback func); ConfigChangedCallbackID AddConfigChangedCallback(ConfigChangedCallback func);
void RemoveConfigChangedCallback(size_t callback_id); void RemoveConfigChangedCallback(ConfigChangedCallbackID callback_id);
void OnConfigChanged(); void OnConfigChanged();
// Returns the number of times the config has changed in the current execution of the program // Returns the number of times the config has changed in the current execution of the program

View File

@ -13,7 +13,10 @@ namespace
{ {
std::atomic<bool> s_should_run_callbacks = false; std::atomic<bool> s_should_run_callbacks = false;
static std::vector<std::pair<size_t, Config::ConfigChangedCallback>> s_callbacks; static std::vector<
std::pair<CPUThreadConfigCallback::ConfigChangedCallbackID, Config::ConfigChangedCallback>>
s_callbacks;
static size_t s_next_callback_id = 0; static size_t s_next_callback_id = 0;
void RunCallbacks() void RunCallbacks()
@ -39,19 +42,19 @@ void OnConfigChanged()
namespace CPUThreadConfigCallback namespace CPUThreadConfigCallback
{ {
size_t AddConfigChangedCallback(Config::ConfigChangedCallback func) ConfigChangedCallbackID AddConfigChangedCallback(Config::ConfigChangedCallback func)
{ {
DEBUG_ASSERT(Core::IsCPUThread()); DEBUG_ASSERT(Core::IsCPUThread());
static size_t s_config_changed_callback_id = Config::AddConfigChangedCallback(&OnConfigChanged); static auto s_config_changed_callback_id = Config::AddConfigChangedCallback(&OnConfigChanged);
const size_t callback_id = s_next_callback_id; const ConfigChangedCallbackID callback_id{s_next_callback_id};
++s_next_callback_id; ++s_next_callback_id;
s_callbacks.emplace_back(std::make_pair(callback_id, std::move(func))); s_callbacks.emplace_back(std::make_pair(callback_id, std::move(func)));
return callback_id; return callback_id;
} }
void RemoveConfigChangedCallback(size_t callback_id) void RemoveConfigChangedCallback(ConfigChangedCallbackID callback_id)
{ {
DEBUG_ASSERT(Core::IsCPUThread()); DEBUG_ASSERT(Core::IsCPUThread());

View File

@ -11,10 +11,18 @@
namespace CPUThreadConfigCallback namespace CPUThreadConfigCallback
{ {
// returns an ID that can be passed to RemoveConfigChangedCallback() struct ConfigChangedCallbackID
size_t AddConfigChangedCallback(Config::ConfigChangedCallback func); {
size_t id = -1;
void RemoveConfigChangedCallback(size_t callback_id); bool operator==(const ConfigChangedCallbackID&) const = default;
bool operator!=(const ConfigChangedCallbackID&) const = default;
};
// returns an ID that can be passed to RemoveConfigChangedCallback()
ConfigChangedCallbackID AddConfigChangedCallback(Config::ConfigChangedCallback func);
void RemoveConfigChangedCallback(ConfigChangedCallbackID callback_id);
// Should be called regularly from the CPU thread // Should be called regularly from the CPU thread
void CheckForConfigChanges(); void CheckForConfigChanges();

View File

@ -23,6 +23,7 @@
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/SPSCQueue.h" #include "Common/SPSCQueue.h"
#include "Core/CPUThreadConfigCallback.h"
class PointerWrap; class PointerWrap;
@ -182,7 +183,7 @@ private:
EventType* m_ev_lost = nullptr; EventType* m_ev_lost = nullptr;
size_t m_registered_config_callback_id = 0; CPUThreadConfigCallback::ConfigChangedCallbackID m_registered_config_callback_id;
float m_config_oc_factor = 0.0f; float m_config_oc_factor = 0.0f;
float m_config_oc_inv_factor = 0.0f; float m_config_oc_inv_factor = 0.0f;
bool m_config_sync_on_skip_idle = false; bool m_config_sync_on_skip_idle = false;

View File

@ -10,6 +10,7 @@
#include <vector> #include <vector>
#include "Common/Assert.h" #include "Common/Assert.h"
#include "Common/Config/Config.h"
#include "Core/FifoPlayer/FifoDataFile.h" #include "Core/FifoPlayer/FifoDataFile.h"
#include "Core/PowerPC/CPUCoreBase.h" #include "Core/PowerPC/CPUCoreBase.h"
#include "VideoCommon/CPMemory.h" #include "VideoCommon/CPMemory.h"
@ -189,7 +190,7 @@ private:
CallbackFunc m_FileLoadedCb = nullptr; CallbackFunc m_FileLoadedCb = nullptr;
CallbackFunc m_FrameWrittenCb = nullptr; CallbackFunc m_FrameWrittenCb = nullptr;
size_t m_config_changed_callback_id; Config::ConfigChangedCallbackID m_config_changed_callback_id;
std::unique_ptr<FifoDataFile> m_File; std::unique_ptr<FifoDataFile> m_File;

View File

@ -30,7 +30,7 @@ static std::array<u8, MAX_BBMOTES> s_last_connect_request_counter;
namespace namespace
{ {
static std::array<std::atomic<WiimoteSource>, MAX_BBMOTES> s_wiimote_sources; static std::array<std::atomic<WiimoteSource>, MAX_BBMOTES> s_wiimote_sources;
static std::optional<size_t> s_config_callback_id = std::nullopt; static std::optional<Config::ConfigChangedCallbackID> s_config_callback_id = std::nullopt;
WiimoteSource GetSource(unsigned int index) WiimoteSource GetSource(unsigned int index)
{ {

View File

@ -9,6 +9,7 @@
#include <string> #include <string>
#include "Common/Common.h" #include "Common/Common.h"
#include "Common/Config/Config.h"
#include "Core/HW/WiimoteCommon/WiimoteReport.h" #include "Core/HW/WiimoteCommon/WiimoteReport.h"
@ -343,6 +344,6 @@ private:
IMUCursorState m_imu_cursor_state; IMUCursorState m_imu_cursor_state;
size_t m_config_changed_callback_id; Config::ConfigChangedCallbackID m_config_changed_callback_id;
}; };
} // namespace WiimoteEmu } // namespace WiimoteEmu

View File

@ -11,6 +11,7 @@
#include <vector> #include <vector>
#include "Common/Common.h" #include "Common/Common.h"
#include "Common/Config/Config.h"
#include "Common/Event.h" #include "Common/Event.h"
#include "Common/Flag.h" #include "Common/Flag.h"
#include "Common/SPSCQueue.h" #include "Common/SPSCQueue.h"
@ -157,7 +158,7 @@ private:
bool m_speaker_enabled_in_dolphin_config = false; bool m_speaker_enabled_in_dolphin_config = false;
int m_balance_board_dump_port = 0; int m_balance_board_dump_port = 0;
size_t m_config_changed_callback_id; Config::ConfigChangedCallbackID m_config_changed_callback_id;
}; };
class WiimoteScannerBackend class WiimoteScannerBackend

View File

@ -10,6 +10,7 @@
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/IOFile.h" #include "Common/IOFile.h"
#include "Core/CPUThreadConfigCallback.h"
#include "Core/IOS/Device.h" #include "Core/IOS/Device.h"
#include "Core/IOS/IOS.h" #include "Core/IOS/IOS.h"
@ -166,7 +167,7 @@ private:
File::IOFile m_card; File::IOFile m_card;
size_t m_config_callback_id; CPUThreadConfigCallback::ConfigChangedCallbackID m_config_callback_id;
bool m_sd_card_inserted = false; bool m_sd_card_inserted = false;
}; };
} // namespace IOS::HLE } // namespace IOS::HLE

View File

@ -10,6 +10,7 @@
#include "Common/BitSet.h" #include "Common/BitSet.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/x64Emitter.h" #include "Common/x64Emitter.h"
#include "Core/CPUThreadConfigCallback.h"
#include "Core/ConfigManager.h" #include "Core/ConfigManager.h"
#include "Core/MachineContext.h" #include "Core/MachineContext.h"
#include "Core/PowerPC/CPUCoreBase.h" #include "Core/PowerPC/CPUCoreBase.h"
@ -129,7 +130,7 @@ protected:
PPCAnalyst::CodeBuffer m_code_buffer; PPCAnalyst::CodeBuffer m_code_buffer;
PPCAnalyst::PPCAnalyzer analyzer; PPCAnalyst::PPCAnalyzer analyzer;
size_t m_registered_config_callback_id; CPUThreadConfigCallback::ConfigChangedCallbackID m_registered_config_callback_id;
bool bJITOff = false; bool bJITOff = false;
bool bJITLoadStoreOff = false; bool bJITLoadStoreOff = false;
bool bJITLoadStorelXzOff = false; bool bJITLoadStorelXzOff = false;

View File

@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/Config/Config.h"
class PointerWrap; class PointerWrap;
@ -61,7 +62,7 @@ struct Cache
struct InstructionCache : public Cache struct InstructionCache : public Cache
{ {
std::optional<size_t> m_config_callback_id = std::nullopt; std::optional<Config::ConfigChangedCallbackID> m_config_callback_id = std::nullopt;
bool m_disable_icache = false; bool m_disable_icache = false;

View File

@ -219,7 +219,7 @@ private:
SteadyClock::time_point m_next_listports_time; SteadyClock::time_point m_next_listports_time;
std::thread m_hotplug_thread; std::thread m_hotplug_thread;
Common::Flag m_hotplug_thread_running; Common::Flag m_hotplug_thread_running;
std::size_t m_config_change_callback_id; Config::ConfigChangedCallbackID m_config_change_callback_id;
}; };
std::unique_ptr<ciface::InputBackend> CreateInputBackend(ControllerInterface* controller_interface) std::unique_ptr<ciface::InputBackend> CreateInputBackend(ControllerInterface* controller_interface)

View File

@ -23,6 +23,7 @@
#endif #endif
#include "Common/BitUtils.h" #include "Common/BitUtils.h"
#include "Common/Config/Config.h"
#include "Common/Event.h" #include "Common/Event.h"
#include "Common/Flag.h" #include "Common/Flag.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
@ -158,7 +159,7 @@ static u8 s_endpoint_out = 0;
static u64 s_last_init = 0; static u64 s_last_init = 0;
static std::optional<size_t> s_config_callback_id = std::nullopt; static std::optional<Config::ConfigChangedCallbackID> s_config_callback_id = std::nullopt;
static bool s_is_adapter_wanted = false; static bool s_is_adapter_wanted = false;
static std::array<bool, SerialInterface::MAX_SI_CHANNELS> s_config_rumble_enabled{}; static std::array<bool, SerialInterface::MAX_SI_CHANNELS> s_config_rumble_enabled{};

View File

@ -62,7 +62,7 @@
namespace UICommon namespace UICommon
{ {
static size_t s_config_changed_callback_id; static Config::ConfigChangedCallbackID s_config_changed_callback_id;
static void CreateDumpPath(std::string path) static void CreateDumpPath(std::string path)
{ {

View File

@ -9,6 +9,7 @@
#include "Common/BlockingLoop.h" #include "Common/BlockingLoop.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/Config/Config.h"
#include "Common/Event.h" #include "Common/Event.h"
#include "Common/Flag.h" #include "Common/Flag.h"
@ -121,7 +122,7 @@ private:
bool m_syncing_suspended = false; bool m_syncing_suspended = false;
Common::Event m_sync_wakeup_event; Common::Event m_sync_wakeup_event;
std::optional<size_t> m_config_callback_id = std::nullopt; std::optional<Config::ConfigChangedCallbackID> m_config_callback_id = std::nullopt;
bool m_config_sync_gpu = false; bool m_config_sync_gpu = false;
int m_config_sync_gpu_max_distance = 0; int m_config_sync_gpu_max_distance = 0;
int m_config_sync_gpu_min_distance = 0; int m_config_sync_gpu_min_distance = 0;