From 6ec8e7c19e608a7fa70be16f981de6915a367080 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Thu, 1 Nov 2018 14:46:37 -0400 Subject: [PATCH] make extensions thread safe --- GUI/extenwindow.cpp | 40 ++++++++++++++++++++++------------------ GUI/misc.h | 1 - GUI/qtcommon.h | 1 + 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/GUI/extenwindow.cpp b/GUI/extenwindow.cpp index 34cb6b1..449ef62 100644 --- a/GUI/extenwindow.cpp +++ b/GUI/extenwindow.cpp @@ -13,14 +13,9 @@ namespace ~InfoForExtension() { if (next) delete next; }; }; - struct Extension - { - QString name; - wchar_t*(*callback)(const wchar_t*, const InfoForExtension*); - }; - + QHash extensions; + QStringList extenNames; std::shared_mutex extenMutex; - QVector extensions; void Load(QString extenName) { @@ -30,14 +25,23 @@ namespace if (!module) return; FARPROC callback = GetProcAddress(module, "OnNewSentence"); if (!callback) return; - extensions.push_back({ extenName, (wchar_t*(*)(const wchar_t*, const InfoForExtension*))callback }); + LOCK(extenMutex); + extensions[extenName] = (wchar_t*(*)(const wchar_t*, const InfoForExtension*))callback; + extenNames.push_back(extenName); } void Unload(QString extenName) { - extensions.erase(std::remove_if(extensions.begin(), extensions.end(), [&](Extension extension) { return extension.name == extenName; }), extensions.end()); + LOCK(extenMutex); + extenNames.erase(std::remove(extenNames.begin(), extenNames.end(), extenName), extenNames.end()); FreeLibrary(GetModuleHandleW(extenName.toStdWString().c_str())); } + + void Reorder(QStringList extenNames) + { + LOCK(extenMutex); + ::extenNames = extenNames; + } } bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map miscInfo) @@ -51,9 +55,9 @@ bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_mapnext = new InfoForExtension{ i.first.c_str(), i.second, nullptr }; std::shared_lock sharedLock(extenMutex); - for (auto extension : extensions) + for (auto extenName : extenNames) { - wchar_t* nextBuffer = extension.callback(sentenceBuffer, &miscInfoLinkedList); + wchar_t* nextBuffer = extensions[extenName](sentenceBuffer, &miscInfoLinkedList); if (nextBuffer == nullptr) { success = false; break; } if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer); sentenceBuffer = nextBuffer; @@ -108,10 +112,9 @@ bool ExtenWindow::eventFilter(QObject* target, QEvent* event) // See https://stackoverflow.com/questions/1224432/how-do-i-respond-to-an-internal-drag-and-drop-operation-using-a-qlistwidget/1528215 if (event->type() == QEvent::ChildRemoved) { - QVector newExtensions; - for (int i = 0; i < extenList->count(); ++i) - newExtensions.push_back(*std::find_if(extensions.begin(), extensions.end(), [=](Extension extension) { return extension.name == extenList->item(i)->text(); })); - extensions = newExtensions; + QStringList extenNames; + for (int i = 0; i < extenList->count(); ++i) extenNames.push_back(extenList->item(i)->text()); + Reorder(extenNames); Sync(); } return false; @@ -121,10 +124,11 @@ void ExtenWindow::Sync() { extenList->clear(); extenSaveFile.open(QIODevice::WriteOnly | QIODevice::Truncate); - for (auto extension : extensions) + std::shared_lock sharedLock(extenMutex); + for (auto extenName : extenNames) { - extenList->addItem(extension.name); - extenSaveFile.write((extension.name + ">").toUtf8()); + extenList->addItem(extenName); + extenSaveFile.write((extenName + ">").toUtf8()); } extenSaveFile.close(); } diff --git a/GUI/misc.h b/GUI/misc.h index 7cab830..dd93884 100644 --- a/GUI/misc.h +++ b/GUI/misc.h @@ -3,7 +3,6 @@ #include "qtcommon.h" #include "types.h" -#include QString GetFullModuleName(DWORD processId, HMODULE module = NULL); QString GetModuleName(DWORD processId, HMODULE module = NULL); diff --git a/GUI/qtcommon.h b/GUI/qtcommon.h index 46029ba..cdc2efe 100644 --- a/GUI/qtcommon.h +++ b/GUI/qtcommon.h @@ -3,6 +3,7 @@ #include "common.h" #include #include +#include #include #include #include