mirror of
https://github.com/skyline-emu/skyline.git
synced 2024-12-28 09:55:29 +03:00
Make accesses to setting values thread-safe
This commit is contained in:
parent
30667a0899
commit
d183d14e2a
@ -15,11 +15,15 @@ namespace skyline {
|
|||||||
using Callback = std::function<void(const T &)>;
|
using Callback = std::function<void(const T &)>;
|
||||||
std::vector<Callback> callbacks; //!< Callbacks to be called when this setting changes
|
std::vector<Callback> callbacks; //!< Callbacks to be called when this setting changes
|
||||||
T value;
|
T value;
|
||||||
|
std::mutex valueMutex;
|
||||||
|
std::mutex callbackMutex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Calls all callbacks registered for this setting
|
* @brief Calls all callbacks registered for this setting
|
||||||
|
* @note Locking of the setting value must be handled by the caller
|
||||||
*/
|
*/
|
||||||
void OnSettingChanged() {
|
void OnSettingChanged() {
|
||||||
|
std::scoped_lock lock{callbackMutex};
|
||||||
for (const auto &callback : callbacks)
|
for (const auto &callback : callbacks)
|
||||||
callback(value);
|
callback(value);
|
||||||
}
|
}
|
||||||
@ -28,7 +32,8 @@ namespace skyline {
|
|||||||
/**
|
/**
|
||||||
* @return The underlying setting value
|
* @return The underlying setting value
|
||||||
*/
|
*/
|
||||||
const T &operator*() const {
|
const T &operator*() {
|
||||||
|
std::scoped_lock lock{valueMutex};
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,6 +41,7 @@ namespace skyline {
|
|||||||
* @brief Sets the underlying setting value, signalling any callbacks if necessary
|
* @brief Sets the underlying setting value, signalling any callbacks if necessary
|
||||||
*/
|
*/
|
||||||
void operator=(T newValue) {
|
void operator=(T newValue) {
|
||||||
|
std::scoped_lock lock{valueMutex};
|
||||||
if (value != newValue) {
|
if (value != newValue) {
|
||||||
value = std::move(newValue);
|
value = std::move(newValue);
|
||||||
OnSettingChanged();
|
OnSettingChanged();
|
||||||
@ -46,6 +52,7 @@ namespace skyline {
|
|||||||
* @brief Register a callback to be run when this setting changes
|
* @brief Register a callback to be run when this setting changes
|
||||||
*/
|
*/
|
||||||
void AddCallback(Callback callback) {
|
void AddCallback(Callback callback) {
|
||||||
|
std::scoped_lock lock{callbackMutex};
|
||||||
callbacks.push_back(std::move(callback));
|
callbacks.push_back(std::move(callback));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user