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:
parent
f7ad7a8263
commit
0c95e5c7b8
2 changed files with 16 additions and 5 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <mutex>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <span>
|
#include <span>
|
||||||
|
|
||||||
|
|
@ -257,17 +258,24 @@ 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
|
||||||
{
|
{
|
||||||
auto iter = mBundles.find(localeName);
|
std::shared_lock sharedLock(mMutex);
|
||||||
if (iter != mBundles.end())
|
|
||||||
{
|
{
|
||||||
auto message = iter->second.find(key);
|
auto iter = mBundles.find(localeName);
|
||||||
if (message != iter->second.end())
|
if (iter != mBundles.end())
|
||||||
{
|
{
|
||||||
return &(message->second);
|
auto message = iter->second.find(key);
|
||||||
|
if (message != iter->second.end())
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue