From 7e8dfc03325a5bc71de98906522a6cc91724aa52 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Thu, 28 Feb 2019 01:40:40 -0500 Subject: [PATCH] fix race condition --- extensions/replacer.cpp | 12 ++++++------ include/common.h | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/extensions/replacer.cpp b/extensions/replacer.cpp index b519b67..c2e960e 100644 --- a/extensions/replacer.cpp +++ b/extensions/replacer.cpp @@ -9,6 +9,7 @@ extern const wchar_t* REPLACER_INSTRUCTIONS; constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt"; std::atomic replaceFileLastWrite; +std::shared_mutex m; struct { @@ -34,8 +35,9 @@ public: { length += 1; } - else if (auto& next = current->next[ch]) + else if (current->next.count(ch) != 0) { + auto& next = current->next.at(ch); length += 1; current = next.get(); if (current->value) result = { length, current->value }; @@ -60,6 +62,7 @@ private: int Parse(const std::wstring& file) { + std::lock_guard l(m); replacementTrie = {}; int count = 0; size_t end = 0; @@ -76,6 +79,7 @@ int Parse(const std::wstring& file) bool Replace(std::wstring& sentence) { + std::shared_lock l(m); for (int i = 0; i < sentence.size(); ++i) { auto[length, replacement] = replacementTrie.Lookup(sentence.substr(i)); @@ -116,21 +120,17 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved bool ProcessSentence(std::wstring& sentence, SentenceInfo) { - static std::shared_mutex m; - static_assert(std::has_unique_object_representations_v); if (!replaceFileLastWrite.compare_exchange_strong(std::filesystem::last_write_time(REPLACE_SAVE_FILE), std::filesystem::last_write_time(REPLACE_SAVE_FILE))) { - std::lock_guard l(m); std::vector file(std::istreambuf_iterator(std::ifstream(REPLACE_SAVE_FILE, std::ios::in | std::ios::binary)), {}); Parse(std::wstring((wchar_t*)file.data(), file.size() / sizeof(wchar_t))); } - std::shared_lock l(m); return Replace(sentence); } -TEST( +TEST_SYNC( { assert(Parse(LR"(|ORIG| さよなら|BECOMES|goodbye|END| |ORIG|バカ|BECOMES|idiot|END| diff --git a/include/common.h b/include/common.h index d530682..efcd004 100644 --- a/include/common.h +++ b/include/common.h @@ -69,3 +69,9 @@ private: #else #define TEST(...) #endif + +#ifdef _DEBUG +#define TEST_SYNC(...) inline auto TEST__RUNNER__DUMMY = std::invoke([] { __VA_ARGS__; return 0UL; }); +#else +#define TEST_SYNC(...) +#endif