prevent write starvation

This commit is contained in:
Akash Mozumdar 2021-06-05 02:58:53 -06:00
parent 9f8e523ce3
commit 675695cde1
5 changed files with 10 additions and 11 deletions

View File

@ -1,6 +1,5 @@
#include "extenwindow.h" #include "extenwindow.h"
#include "ui_extenwindow.h" #include "ui_extenwindow.h"
#include <concrt.h>
#include <QMenu> #include <QMenu>
#include <QFileDialog> #include <QFileDialog>
#include <QDragEnterEvent> #include <QDragEnterEvent>
@ -44,7 +43,7 @@ namespace
{ {
if (auto callback = (decltype(Extension::callback))GetProcAddress(module, "OnNewSentence")) 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 }); extensions.push_back({ S(extenName), callback });
return true; return true;
} }
@ -63,7 +62,7 @@ namespace
void Reorder(QStringList extenNames) void Reorder(QStringList extenNames)
{ {
std::scoped_lock writeLock(extenMutex); std::scoped_lock lock(extenMutex);
std::vector<Extension> extensions; std::vector<Extension> extensions;
for (auto extenName : extenNames) for (auto extenName : extenNames)
extensions.push_back(*std::find_if(::extensions.begin(), ::extensions.end(), [&](Extension extension) { return extension.name == S(extenName); })); 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() void CleanupExtensions()
{ {
std::scoped_lock writeLock(extenMutex); std::scoped_lock lock(extenMutex);
for (auto extension : extensions) FreeLibrary(GetModuleHandleW((extension.name + L".xdll").c_str())); for (auto extension : extensions) FreeLibrary(GetModuleHandleW((extension.name + L".xdll").c_str()));
extensions.clear(); extensions.clear();
} }

View File

@ -13,7 +13,7 @@ const char* REGEX_SAVE_FILE = "SavedRegexFilters.txt";
std::optional<std::wregex> regex; std::optional<std::wregex> regex;
std::wstring replace = L"$1"; std::wstring replace = L"$1";
std::shared_mutex m; concurrency::reader_writer_lock m;
DWORD (*GetSelectedProcessId)() = nullptr; DWORD (*GetSelectedProcessId)() = nullptr;
class Window : public QDialog, Localizer 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))); 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); 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); if (regex) sentence = std::regex_replace(sentence, regex.value(), replace);
return true; return true;
} }

View File

@ -10,7 +10,7 @@ extern const wchar_t* REPLACER_INSTRUCTIONS;
constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt"; constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt";
std::atomic<std::filesystem::file_time_type> replaceFileLastWrite = {}; std::atomic<std::filesystem::file_time_type> replaceFileLastWrite = {};
std::shared_mutex m; concurrency::reader_writer_lock m;
class Trie class Trie
{ {
@ -121,7 +121,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo)
{ {
UpdateReplacements(); UpdateReplacements();
std::shared_lock lock(m); concurrency::reader_writer_lock::scoped_lock_read readLock(m);
sentence = trie.Replace(sentence); sentence = trie.Replace(sentence);
return true; return true;
} }

View File

@ -10,7 +10,7 @@ extern const char* THREAD_LINK_TO;
extern const char* HEXADECIMAL; extern const char* HEXADECIMAL;
std::unordered_map<int64_t, std::unordered_set<int64_t>> linkedTextHandles; std::unordered_map<int64_t, std::unordered_set<int64_t>> linkedTextHandles;
std::shared_mutex m; concurrency::reader_writer_lock m;
class Window : public QDialog, Localizer class Window : public QDialog, Localizer
{ {
@ -68,7 +68,7 @@ private:
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) 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"]); auto links = linkedTextHandles.find(sentenceInfo["text number"]);
if (links != linkedTextHandles.end()) for (auto link : links->second) 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()); ((void(*)(int64_t, const wchar_t*))sentenceInfo["void (*AddText)(int64_t number, const wchar_t* text)"])(link, sentence.c_str());

View File

@ -2,6 +2,7 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>
#include <concrt.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <deque> #include <deque>
@ -15,7 +16,6 @@
#include <optional> #include <optional>
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <shared_mutex>
#include <atomic> #include <atomic>
#include <filesystem> #include <filesystem>
#include <cstdint> #include <cstdint>