From cf90539d094505f654f011820a43aeaa9258a508 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Tue, 4 Jun 2019 23:12:45 -0400 Subject: [PATCH] optimize --- GUI/host/host.cpp | 5 +++-- GUI/host/textthread.h | 4 ++-- extensions/lua.cpp | 2 +- extensions/removerepeat.cpp | 4 ++-- include/common.h | 23 ++++++++++++----------- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/GUI/host/host.cpp b/GUI/host/host.cpp index e0e1f9a..d3afe8e 100644 --- a/GUI/host/host.cpp +++ b/GUI/host/host.cpp @@ -64,8 +64,8 @@ namespace { return std::hash()(tp.processId + tp.addr) + std::hash()(tp.ctx + tp.ctx2); } - ThreadSafe>, std::recursive_mutex> textThreadsByParams; - ThreadSafe, std::recursive_mutex> processRecordsByIds; + Synchronized>, std::recursive_mutex> textThreadsByParams; + Synchronized, std::recursive_mutex> processRecordsByIds; Host::ProcessEventHandler OnConnect, OnDisconnect; Host::ThreadEventHandler OnCreate, OnDestroy; @@ -130,6 +130,7 @@ namespace default: { auto tp = *(ThreadParam*)buffer; + auto textThreadsByParams = ::textThreadsByParams.Acquire(); if (textThreadsByParams->count(tp) == 0) { TextThread& created = textThreadsByParams->try_emplace(tp, tp, Host::GetHookParam(tp)).first->second; diff --git a/GUI/host/textthread.h b/GUI/host/textthread.h index 95bb5e0..18c7a61 100644 --- a/GUI/host/textthread.h +++ b/GUI/host/textthread.h @@ -20,7 +20,7 @@ public: void AddSentence(std::wstring&& sentence); void Push(BYTE* data, int length); - ThreadSafe storage; + Synchronized storage; const int64_t handle; const std::wstring name; const ThreadParam tp; @@ -36,7 +36,7 @@ private: std::unordered_set repeatingChars; std::mutex bufferMutex; DWORD lastPushTime = 0; - ThreadSafe> queuedSentences; + Synchronized> queuedSentences; struct TimerDeleter { void operator()(HANDLE h) { DeleteTimerQueueTimer(NULL, h, INVALID_HANDLE_VALUE); } }; AutoHandle timer = NULL; }; diff --git a/extensions/lua.cpp b/extensions/lua.cpp index c9346bd..8449b3f 100644 --- a/extensions/lua.cpp +++ b/extensions/lua.cpp @@ -42,7 +42,7 @@ bool luaL_dostring(lua_State* L, const char* str) } bool logErrors = true; -ThreadSafe script; +Synchronized script; std::atomic revCount = 0; struct : QMainWindow diff --git a/extensions/removerepeat.cpp b/extensions/removerepeat.cpp index ea30680..61d2ca1 100644 --- a/extensions/removerepeat.cpp +++ b/extensions/removerepeat.cpp @@ -2,11 +2,11 @@ void RemoveRepeatedSentences(std::wstring& sentence, uint64_t textNumber) { - static std::deque>> cache; + static std::deque>> cache; static std::mutex m; m.lock(); if (textNumber + 1 > cache.size()) cache.resize(textNumber + 1); - auto[lock, prevSentences] = cache.at(textNumber).operator->(); + auto prevSentences = cache.at(textNumber).Acquire(); m.unlock(); auto& inserted = prevSentences->emplace_back(sentence); auto firstLocation = std::find(prevSentences->begin(), prevSentences->end(), sentence); diff --git a/include/common.h b/include/common.h index 9005b28..4a52cc2 100644 --- a/include/common.h +++ b/include/common.h @@ -27,25 +27,26 @@ constexpr bool x64 = false; #endif #define MESSAGE(text) MessageBoxW(NULL, text, L"Textractor", MB_OK) +#define CRITIAL_SECTION static std::mutex m; std::scoped_lock l(m) template using Array = T[]; template -class ThreadSafe +class Synchronized { public: template - ThreadSafe(Args&&... args) : contents(std::forward(args)...) {} - auto operator->() + Synchronized(Args&&... args) : contents(std::forward(args)...) {} + + struct Locker { - struct - { - E* operator->() { return ptr; } - std::unique_lock lock; - E* ptr; - } lockedProxy{ std::unique_lock(mtx), &contents }; - return lockedProxy; - } + E* operator->() { return ptr; } + std::unique_lock lock; + E* ptr; + }; + + Locker Acquire() { return { std::unique_lock(mtx), &contents }; } + Locker operator->() { return Acquire(); } private: E contents;