Merge pull request #12117 from JosJuice/config-callback-cpu

Don't call RunAsCPUThread in config callbacks
This commit is contained in:
Admiral H. Curtiss
2023-08-26 16:34:46 +02:00
committed by GitHub
33 changed files with 234 additions and 49 deletions

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)
{
@ -138,7 +138,6 @@ void Shutdown()
WriteLock lock(s_layers_rw_lock);
s_layers.clear();
s_callbacks.clear();
}
void ClearCurrentRunLayer()

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
@ -22,9 +30,10 @@ void AddLayer(std::unique_ptr<ConfigLayerLoader> loader);
std::shared_ptr<Layer> GetLayer(LayerType layer);
void RemoveLayer(LayerType layer);
// returns an ID that can be passed to RemoveConfigChangedCallback()
size_t AddConfigChangedCallback(ConfigChangedCallback func);
void RemoveConfigChangedCallback(size_t callback_id);
// Returns an ID that can be passed to RemoveConfigChangedCallback().
// The callback may be called from any thread.
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