From d183d14e2ab4da6b7bf79cd0661c7f4a77b0b4d1 Mon Sep 17 00:00:00 2001 From: lynxnb Date: Tue, 19 Jul 2022 14:05:16 +0200 Subject: [PATCH] Make accesses to setting values thread-safe --- app/src/main/cpp/skyline/common/settings.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/src/main/cpp/skyline/common/settings.h b/app/src/main/cpp/skyline/common/settings.h index ed22b4c4..d1962726 100644 --- a/app/src/main/cpp/skyline/common/settings.h +++ b/app/src/main/cpp/skyline/common/settings.h @@ -15,11 +15,15 @@ namespace skyline { using Callback = std::function; std::vector callbacks; //!< Callbacks to be called when this setting changes T value; + std::mutex valueMutex; + std::mutex callbackMutex; /** * @brief Calls all callbacks registered for this setting + * @note Locking of the setting value must be handled by the caller */ void OnSettingChanged() { + std::scoped_lock lock{callbackMutex}; for (const auto &callback : callbacks) callback(value); } @@ -28,7 +32,8 @@ namespace skyline { /** * @return The underlying setting value */ - const T &operator*() const { + const T &operator*() { + std::scoped_lock lock{valueMutex}; return value; } @@ -36,6 +41,7 @@ namespace skyline { * @brief Sets the underlying setting value, signalling any callbacks if necessary */ void operator=(T newValue) { + std::scoped_lock lock{valueMutex}; if (value != newValue) { value = std::move(newValue); OnSettingChanged(); @@ -46,6 +52,7 @@ namespace skyline { * @brief Register a callback to be run when this setting changes */ void AddCallback(Callback callback) { + std::scoped_lock lock{callbackMutex}; callbacks.push_back(std::move(callback)); } };