1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-12-21 13:53:06 +00:00

Ensure thread safety

This commit is contained in:
Evil Eye 2025-11-25 22:20:27 +01:00
parent f7ad7a8263
commit 0c95e5c7b8
2 changed files with 16 additions and 5 deletions

View file

@ -2,6 +2,7 @@
#include <charconv> #include <charconv>
#include <cstring> #include <cstring>
#include <mutex>
#include <optional> #include <optional>
#include <span> #include <span>
@ -256,6 +257,8 @@ namespace L10n
} }
const icu::MessageFormat* MessageBundles::findMessage(std::string_view key, std::string_view localeName) const const icu::MessageFormat* MessageBundles::findMessage(std::string_view key, std::string_view localeName) const
{
std::shared_lock sharedLock(mMutex);
{ {
auto iter = mBundles.find(localeName); auto iter = mBundles.find(localeName);
if (iter != mBundles.end()) if (iter != mBundles.end())
@ -266,8 +269,13 @@ namespace L10n
return &(message->second); return &(message->second);
} }
} }
}
if (localeName == "gmst" && mGmstLoader) if (localeName == "gmst" && mGmstLoader)
{ {
if (!mGmsts.contains(key))
return nullptr;
sharedLock.unlock();
std::unique_lock lock(mMutex);
auto found = mGmsts.find(key); auto found = mGmsts.find(key);
if (found != mGmsts.end()) if (found != mGmsts.end())
{ {
@ -275,6 +283,7 @@ namespace L10n
mGmsts.erase(found); mGmsts.erase(found);
if (message) if (message)
{ {
auto iter = mBundles.find(localeName);
if (iter == mBundles.end()) if (iter == mBundles.end())
iter = mBundles.emplace(localeName, StringMap<icu::MessageFormat>()).first; iter = mBundles.emplace(localeName, StringMap<icu::MessageFormat>()).first;
return &iter->second.emplace(key, *message).first->second; return &iter->second.emplace(key, *message).first->second;

View file

@ -3,6 +3,7 @@
#include <functional> #include <functional>
#include <map> #include <map>
#include <shared_mutex>
#include <string_view> #include <string_view>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -65,6 +66,7 @@ namespace L10n
// icu::Locale isn't hashable (or comparable), so we use the string form instead, which is canonicalized // icu::Locale isn't hashable (or comparable), so we use the string form instead, which is canonicalized
mutable StringMap<StringMap<icu::MessageFormat>> mBundles; mutable StringMap<StringMap<icu::MessageFormat>> mBundles;
mutable StringMap<GmstMessageFormat> mGmsts; mutable StringMap<GmstMessageFormat> mGmsts;
mutable std::shared_mutex mMutex;
const icu::Locale mFallbackLocale; const icu::Locale mFallbackLocale;
std::vector<std::string> mPreferredLocaleStrings; std::vector<std::string> mPreferredLocaleStrings;
std::vector<icu::Locale> mPreferredLocales; std::vector<icu::Locale> mPreferredLocales;