make extensions thread safe

This commit is contained in:
Akash Mozumdar 2018-11-01 14:46:37 -04:00
parent ce225fd900
commit 6ec8e7c19e
3 changed files with 23 additions and 19 deletions

View File

@ -13,14 +13,9 @@ namespace
~InfoForExtension() { if (next) delete next; }; ~InfoForExtension() { if (next) delete next; };
}; };
struct Extension QHash<QString, wchar_t*(*)(const wchar_t*, const InfoForExtension*)> extensions;
{ QStringList extenNames;
QString name;
wchar_t*(*callback)(const wchar_t*, const InfoForExtension*);
};
std::shared_mutex extenMutex; std::shared_mutex extenMutex;
QVector<Extension> extensions;
void Load(QString extenName) void Load(QString extenName)
{ {
@ -30,14 +25,23 @@ namespace
if (!module) return; if (!module) return;
FARPROC callback = GetProcAddress(module, "OnNewSentence"); FARPROC callback = GetProcAddress(module, "OnNewSentence");
if (!callback) return; 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) 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())); FreeLibrary(GetModuleHandleW(extenName.toStdWString().c_str()));
} }
void Reorder(QStringList extenNames)
{
LOCK(extenMutex);
::extenNames = extenNames;
}
} }
bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std::string, int64_t> miscInfo) bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std::string, int64_t> miscInfo)
@ -51,9 +55,9 @@ bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std
for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->next = new InfoForExtension{ i.first.c_str(), i.second, nullptr }; for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->next = new InfoForExtension{ i.first.c_str(), i.second, nullptr };
std::shared_lock sharedLock(extenMutex); 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 == nullptr) { success = false; break; }
if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer); if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
sentenceBuffer = nextBuffer; 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 // 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) if (event->type() == QEvent::ChildRemoved)
{ {
QVector<Extension> newExtensions; QStringList extenNames;
for (int i = 0; i < extenList->count(); ++i) for (int i = 0; i < extenList->count(); ++i) extenNames.push_back(extenList->item(i)->text());
newExtensions.push_back(*std::find_if(extensions.begin(), extensions.end(), [=](Extension extension) { return extension.name == extenList->item(i)->text(); })); Reorder(extenNames);
extensions = newExtensions;
Sync(); Sync();
} }
return false; return false;
@ -121,10 +124,11 @@ void ExtenWindow::Sync()
{ {
extenList->clear(); extenList->clear();
extenSaveFile.open(QIODevice::WriteOnly | QIODevice::Truncate); extenSaveFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
for (auto extension : extensions) std::shared_lock sharedLock(extenMutex);
for (auto extenName : extenNames)
{ {
extenList->addItem(extension.name); extenList->addItem(extenName);
extenSaveFile.write((extension.name + ">").toUtf8()); extenSaveFile.write((extenName + ">").toUtf8());
} }
extenSaveFile.close(); extenSaveFile.close();
} }

View File

@ -3,7 +3,6 @@
#include "qtcommon.h" #include "qtcommon.h"
#include "types.h" #include "types.h"
#include <QHash>
QString GetFullModuleName(DWORD processId, HMODULE module = NULL); QString GetFullModuleName(DWORD processId, HMODULE module = NULL);
QString GetModuleName(DWORD processId, HMODULE module = NULL); QString GetModuleName(DWORD processId, HMODULE module = NULL);

View File

@ -3,6 +3,7 @@
#include "common.h" #include "common.h"
#include <QString> #include <QString>
#include <QVector> #include <QVector>
#include <QHash>
#include <QMainWindow> #include <QMainWindow>
#include <QFile> #include <QFile>
#include <QRegularExpression> #include <QRegularExpression>