From 675695cde136139554fe35072bf2b2010006268f Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sat, 5 Jun 2021 02:58:53 -0600 Subject: [PATCH] prevent write starvation --- GUI/extenwindow.cpp | 7 +++---- extensions/regexfilter.cpp | 4 ++-- extensions/replacer.cpp | 4 ++-- extensions/threadlinker.cpp | 4 ++-- include/common.h | 2 +- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/GUI/extenwindow.cpp b/GUI/extenwindow.cpp index 76c5e80..07ab793 100644 --- a/GUI/extenwindow.cpp +++ b/GUI/extenwindow.cpp @@ -1,6 +1,5 @@ #include "extenwindow.h" #include "ui_extenwindow.h" -#include #include #include #include @@ -44,7 +43,7 @@ namespace { if (auto callback = (decltype(Extension::callback))GetProcAddress(module, "OnNewSentence")) { - std::scoped_lock writeLock(extenMutex); + std::scoped_lock lock(extenMutex); extensions.push_back({ S(extenName), callback }); return true; } @@ -63,7 +62,7 @@ namespace void Reorder(QStringList extenNames) { - std::scoped_lock writeLock(extenMutex); + std::scoped_lock lock(extenMutex); std::vector extensions; for (auto extenName : extenNames) extensions.push_back(*std::find_if(::extensions.begin(), ::extensions.end(), [&](Extension extension) { return extension.name == S(extenName); })); @@ -128,7 +127,7 @@ bool DispatchSentenceToExtensions(std::wstring& sentence, const InfoForExtension void CleanupExtensions() { - std::scoped_lock writeLock(extenMutex); + std::scoped_lock lock(extenMutex); for (auto extension : extensions) FreeLibrary(GetModuleHandleW((extension.name + L".xdll").c_str())); extensions.clear(); } diff --git a/extensions/regexfilter.cpp b/extensions/regexfilter.cpp index 69560f9..2ba237b 100644 --- a/extensions/regexfilter.cpp +++ b/extensions/regexfilter.cpp @@ -13,7 +13,7 @@ const char* REGEX_SAVE_FILE = "SavedRegexFilters.txt"; std::optional regex; std::wstring replace = L"$1"; -std::shared_mutex m; +concurrency::reader_writer_lock m; DWORD (*GetSelectedProcessId)() = nullptr; class Window : public QDialog, Localizer @@ -66,7 +66,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) while (auto read = savedFilters.Next()) if (read->at(0) == processName) regexes.push_back(std::move(read->at(1))); if (!regexes.empty()) QMetaObject::invokeMethod(&window, std::bind(&Window::SetRegex, &window, S(regexes.back())), Qt::BlockingQueuedConnection); } - std::shared_lock lock(m); + concurrency::reader_writer_lock::scoped_lock_read readLock(m); if (regex) sentence = std::regex_replace(sentence, regex.value(), replace); return true; } diff --git a/extensions/replacer.cpp b/extensions/replacer.cpp index 03ae8dc..6d5fa33 100644 --- a/extensions/replacer.cpp +++ b/extensions/replacer.cpp @@ -10,7 +10,7 @@ extern const wchar_t* REPLACER_INSTRUCTIONS; constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt"; std::atomic replaceFileLastWrite = {}; -std::shared_mutex m; +concurrency::reader_writer_lock m; class Trie { @@ -121,7 +121,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo) { UpdateReplacements(); - std::shared_lock lock(m); + concurrency::reader_writer_lock::scoped_lock_read readLock(m); sentence = trie.Replace(sentence); return true; } diff --git a/extensions/threadlinker.cpp b/extensions/threadlinker.cpp index fb4ff00..0fc5230 100644 --- a/extensions/threadlinker.cpp +++ b/extensions/threadlinker.cpp @@ -10,7 +10,7 @@ extern const char* THREAD_LINK_TO; extern const char* HEXADECIMAL; std::unordered_map> linkedTextHandles; -std::shared_mutex m; +concurrency::reader_writer_lock m; class Window : public QDialog, Localizer { @@ -68,7 +68,7 @@ private: bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) { - std::shared_lock lock(m); + concurrency::reader_writer_lock::scoped_lock_read readLock(m); auto links = linkedTextHandles.find(sentenceInfo["text number"]); if (links != linkedTextHandles.end()) for (auto link : links->second) ((void(*)(int64_t, const wchar_t*))sentenceInfo["void (*AddText)(int64_t number, const wchar_t* text)"])(link, sentence.c_str()); diff --git a/include/common.h b/include/common.h index 32e6d0a..ec37f27 100644 --- a/include/common.h +++ b/include/common.h @@ -2,6 +2,7 @@ #define WIN32_LEAN_AND_MEAN #include +#include #include #include #include @@ -15,7 +16,6 @@ #include #include #include -#include #include #include #include