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/WaveFile.h"
#include "Common/CommonTypes.h"
#include "Common/Config/Config.h"
class PointerWrap;
@ -120,5 +121,5 @@ private:
int m_config_timing_variance;
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>>;
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 u32 s_callback_guards = 0;
static std::atomic<u64> s_config_version = 0;
@ -65,15 +65,15 @@ void RemoveLayer(LayerType layer)
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_callbacks.emplace_back(std::make_pair(callback_id, std::move(func)));
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)
{

View File

@ -15,6 +15,14 @@
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()>;
// Layer management
@ -24,8 +32,8 @@ void RemoveLayer(LayerType layer);
// Returns an ID that can be passed to RemoveConfigChangedCallback().
// The callback may be called from any thread.
size_t AddConfigChangedCallback(ConfigChangedCallback func);
void RemoveConfigChangedCallback(size_t callback_id);
ConfigChangedCallbackID AddConfigChangedCallback(ConfigChangedCallback func);
void RemoveConfigChangedCallback(ConfigChangedCallbackID callback_id);
void OnConfigChanged();
// 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;
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;
void RunCallbacks()
@ -39,19 +42,19 @@ void OnConfigChanged()
namespace CPUThreadConfigCallback
{
size_t AddConfigChangedCallback(Config::ConfigChangedCallback func)
ConfigChangedCallbackID AddConfigChangedCallback(Config::ConfigChangedCallback func)
{
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_callbacks.emplace_back(std::make_pair(callback_id, std::move(func)));
return callback_id;
}
void RemoveConfigChangedCallback(size_t callback_id)
void RemoveConfigChangedCallback(ConfigChangedCallbackID callback_id)
{
DEBUG_ASSERT(Core::IsCPUThread());

View File

@ -11,10 +11,18 @@
namespace CPUThreadConfigCallback
{
// returns an ID that can be passed to RemoveConfigChangedCallback()
size_t AddConfigChangedCallback(Config::ConfigChangedCallback func);
struct ConfigChangedCallbackID
{
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
void CheckForConfigChanges();

View File

@ -23,6 +23,7 @@
#include "Common/CommonTypes.h"
#include "Common/SPSCQueue.h"
#include "Core/CPUThreadConfigCallback.h"
class PointerWrap;
@ -182,7 +183,7 @@ private:
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_inv_factor = 0.0f;
bool m_config_sync_on_skip_idle = false;

View File

@ -10,6 +10,7 @@
#include <vector>
#include "Common/Assert.h"
#include "Common/Config/Config.h"
#include "Core/FifoPlayer/FifoDataFile.h"
#include "Core/PowerPC/CPUCoreBase.h"
#include "VideoCommon/CPMemory.h"
@ -189,7 +190,7 @@ private:
CallbackFunc m_FileLoadedCb = 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;

View File

@ -30,7 +30,7 @@ static std::array<u8, MAX_BBMOTES> s_last_connect_request_counter;
namespace
{
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)
{

View File

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

View File

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

View File

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

View File

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

View File

@ -8,6 +8,7 @@
#include <vector>
#include "Common/CommonTypes.h"
#include "Common/Config/Config.h"
class PointerWrap;
@ -61,7 +62,7 @@ struct 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;

View File

@ -219,7 +219,7 @@ private:
SteadyClock::time_point m_next_listports_time;
std::thread m_hotplug_thread;
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)

View File

@ -23,6 +23,7 @@
#endif
#include "Common/BitUtils.h"
#include "Common/Config/Config.h"
#include "Common/Event.h"
#include "Common/Flag.h"
#include "Common/Logging/Log.h"
@ -158,7 +159,7 @@ static u8 s_endpoint_out = 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 std::array<bool, SerialInterface::MAX_SI_CHANNELS> s_config_rumble_enabled{};

View File

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

View File

@ -9,6 +9,7 @@
#include "Common/BlockingLoop.h"
#include "Common/CommonTypes.h"
#include "Common/Config/Config.h"
#include "Common/Event.h"
#include "Common/Flag.h"
@ -121,7 +122,7 @@ private:
bool m_syncing_suspended = false;
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;
int m_config_sync_gpu_max_distance = 0;
int m_config_sync_gpu_min_distance = 0;