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 "ui_extenwindow.h"
#include <concrt.h>
#include <QMenu>
#include <QFileDialog>
#include <QDragEnterEvent>
@ -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<Extension> 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();
}

View File

@ -13,7 +13,7 @@ const char* REGEX_SAVE_FILE = "SavedRegexFilters.txt";
std::optional<std::wregex> 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;
}

View File

@ -10,7 +10,7 @@ extern const wchar_t* REPLACER_INSTRUCTIONS;
constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt";
std::atomic<std::filesystem::file_time_type> 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;
}

View File

@ -10,7 +10,7 @@ extern const char* THREAD_LINK_TO;
extern const char* HEXADECIMAL;
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
{
@ -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());

View File

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