From 7404592431c59ea69579a27416668a54c8e9f7c3 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 14 Mar 2020 22:48:45 -0700 Subject: [PATCH] Core: Use a lock for i18n categories. Also for the missed key log. --- ext/native/i18n/i18n.cpp | 5 +++++ ext/native/i18n/i18n.h | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ext/native/i18n/i18n.cpp b/ext/native/i18n/i18n.cpp index 958f162ca..eb823dc2b 100644 --- a/ext/native/i18n/i18n.cpp +++ b/ext/native/i18n/i18n.cpp @@ -14,6 +14,7 @@ std::string I18NRepo::LanguageID() { } void I18NRepo::Clear() { + std::lock_guard guard(catsLock_); for (auto iter = cats_.begin(); iter != cats_.end(); ++iter) { iter->second.reset(); } @@ -33,6 +34,7 @@ const char *I18NCategory::T(const char *key, const char *def) { // ILOG("translation key found in %s: %s", name_.c_str(), key); return iter->second.text.c_str(); } else { + std::lock_guard guard(missedKeyLock_); if (def) missedKeyLog_[key] = def; else @@ -53,6 +55,7 @@ void I18NCategory::SetMap(const std::map &m) { } std::shared_ptr I18NRepo::GetCategory(const char *category) { + std::lock_guard guard(catsLock_); auto iter = cats_.find(category); if (iter != cats_.end()) { return iter->second; @@ -94,6 +97,7 @@ bool I18NRepo::LoadIni(const std::string &languageID, const std::string &overrid const std::vector §ions = ini.Sections(); + std::lock_guard guard(catsLock_); for (auto iter = sections.begin(); iter != sections.end(); ++iter) { if (iter->name() != "") { cats_[iter->name()].reset(LoadSection(&(*iter), iter->name().c_str())); @@ -116,6 +120,7 @@ I18NCategory *I18NRepo::LoadSection(const IniFile::Section *section, const char void I18NRepo::SaveIni(const std::string &languageID) { IniFile ini; ini.Load(GetIniPath(languageID)); + std::lock_guard guard(catsLock_); for (auto iter = cats_.begin(); iter != cats_.end(); ++iter) { std::string categoryName = iter->first; IniFile::Section *section = ini.GetOrCreateSection(categoryName.c_str()); diff --git a/ext/native/i18n/i18n.h b/ext/native/i18n/i18n.h index 5a527d664..bc4e3ca4b 100644 --- a/ext/native/i18n/i18n.h +++ b/ext/native/i18n/i18n.h @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -44,20 +45,22 @@ public: } const std::map &Missed() const { + std::lock_guard guard(missedKeyLock_); return missedKeyLog_; } - void SetMap(const std::map &m); const std::map &GetMap() { return map_; } void ClearMissed() { missedKeyLog_.clear(); } const char *GetName() const { return name_.c_str(); } private: I18NCategory(I18NRepo *repo, const char *name) : name_(name) {} + void SetMap(const std::map &m); std::string name_; std::map map_; + mutable std::mutex missedKeyLock_; std::map missedKeyLog_; // Noone else can create these. @@ -79,6 +82,7 @@ public: std::shared_ptr GetCategory(const char *categoryName); bool HasCategory(const char *categoryName) const { + std::lock_guard guard(catsLock_); return cats_.find(categoryName) != cats_.end(); } const char *T(const char *category, const char *key, const char *def = 0); @@ -89,6 +93,7 @@ private: I18NCategory *LoadSection(const IniFile::Section *section, const char *name); void SaveSection(IniFile &ini, IniFile::Section *section, std::shared_ptr cat); + mutable std::mutex catsLock_; std::map> cats_; std::string languageID_;