This commit is contained in:
Akash Mozumdar 2019-06-04 23:12:45 -04:00
parent d16db4d319
commit cf90539d09
5 changed files with 20 additions and 18 deletions

View File

@ -64,8 +64,8 @@ namespace
{ {
return std::hash<int64_t>()(tp.processId + tp.addr) + std::hash<int64_t>()(tp.ctx + tp.ctx2); return std::hash<int64_t>()(tp.processId + tp.addr) + std::hash<int64_t>()(tp.ctx + tp.ctx2);
} }
ThreadSafe<std::unordered_map<ThreadParam, TextThread, Functor<HashThreadParam>>, std::recursive_mutex> textThreadsByParams; Synchronized<std::unordered_map<ThreadParam, TextThread, Functor<HashThreadParam>>, std::recursive_mutex> textThreadsByParams;
ThreadSafe<std::unordered_map<DWORD, ProcessRecord>, std::recursive_mutex> processRecordsByIds; Synchronized<std::unordered_map<DWORD, ProcessRecord>, std::recursive_mutex> processRecordsByIds;
Host::ProcessEventHandler OnConnect, OnDisconnect; Host::ProcessEventHandler OnConnect, OnDisconnect;
Host::ThreadEventHandler OnCreate, OnDestroy; Host::ThreadEventHandler OnCreate, OnDestroy;
@ -130,6 +130,7 @@ namespace
default: default:
{ {
auto tp = *(ThreadParam*)buffer; auto tp = *(ThreadParam*)buffer;
auto textThreadsByParams = ::textThreadsByParams.Acquire();
if (textThreadsByParams->count(tp) == 0) if (textThreadsByParams->count(tp) == 0)
{ {
TextThread& created = textThreadsByParams->try_emplace(tp, tp, Host::GetHookParam(tp)).first->second; TextThread& created = textThreadsByParams->try_emplace(tp, tp, Host::GetHookParam(tp)).first->second;

View File

@ -20,7 +20,7 @@ public:
void AddSentence(std::wstring&& sentence); void AddSentence(std::wstring&& sentence);
void Push(BYTE* data, int length); void Push(BYTE* data, int length);
ThreadSafe<std::wstring> storage; Synchronized<std::wstring> storage;
const int64_t handle; const int64_t handle;
const std::wstring name; const std::wstring name;
const ThreadParam tp; const ThreadParam tp;
@ -36,7 +36,7 @@ private:
std::unordered_set<wchar_t> repeatingChars; std::unordered_set<wchar_t> repeatingChars;
std::mutex bufferMutex; std::mutex bufferMutex;
DWORD lastPushTime = 0; DWORD lastPushTime = 0;
ThreadSafe<std::deque<std::wstring>> queuedSentences; Synchronized<std::deque<std::wstring>> queuedSentences;
struct TimerDeleter { void operator()(HANDLE h) { DeleteTimerQueueTimer(NULL, h, INVALID_HANDLE_VALUE); } }; struct TimerDeleter { void operator()(HANDLE h) { DeleteTimerQueueTimer(NULL, h, INVALID_HANDLE_VALUE); } };
AutoHandle<TimerDeleter> timer = NULL; AutoHandle<TimerDeleter> timer = NULL;
}; };

View File

@ -42,7 +42,7 @@ bool luaL_dostring(lua_State* L, const char* str)
} }
bool logErrors = true; bool logErrors = true;
ThreadSafe<std::string> script; Synchronized<std::string> script;
std::atomic<int> revCount = 0; std::atomic<int> revCount = 0;
struct : QMainWindow struct : QMainWindow

View File

@ -2,11 +2,11 @@
void RemoveRepeatedSentences(std::wstring& sentence, uint64_t textNumber) void RemoveRepeatedSentences(std::wstring& sentence, uint64_t textNumber)
{ {
static std::deque<ThreadSafe<std::vector<std::wstring>>> cache; static std::deque<Synchronized<std::vector<std::wstring>>> cache;
static std::mutex m; static std::mutex m;
m.lock(); m.lock();
if (textNumber + 1 > cache.size()) cache.resize(textNumber + 1); if (textNumber + 1 > cache.size()) cache.resize(textNumber + 1);
auto[lock, prevSentences] = cache.at(textNumber).operator->(); auto prevSentences = cache.at(textNumber).Acquire();
m.unlock(); m.unlock();
auto& inserted = prevSentences->emplace_back(sentence); auto& inserted = prevSentences->emplace_back(sentence);
auto firstLocation = std::find(prevSentences->begin(), prevSentences->end(), sentence); auto firstLocation = std::find(prevSentences->begin(), prevSentences->end(), sentence);

View File

@ -27,25 +27,26 @@ constexpr bool x64 = false;
#endif #endif
#define MESSAGE(text) MessageBoxW(NULL, text, L"Textractor", MB_OK) #define MESSAGE(text) MessageBoxW(NULL, text, L"Textractor", MB_OK)
#define CRITIAL_SECTION static std::mutex m; std::scoped_lock l(m)
template <typename T> using Array = T[]; template <typename T> using Array = T[];
template<typename E, typename M = std::mutex> template<typename E, typename M = std::mutex>
class ThreadSafe class Synchronized
{ {
public: public:
template <typename... Args> template <typename... Args>
ThreadSafe(Args&&... args) : contents(std::forward<Args>(args)...) {} Synchronized(Args&&... args) : contents(std::forward<Args>(args)...) {}
auto operator->()
struct Locker
{ {
struct E* operator->() { return ptr; }
{ std::unique_lock<M> lock;
E* operator->() { return ptr; } E* ptr;
std::unique_lock<M> lock; };
E* ptr;
} lockedProxy{ std::unique_lock(mtx), &contents }; Locker Acquire() { return { std::unique_lock(mtx), &contents }; }
return lockedProxy; Locker operator->() { return Acquire(); }
}
private: private:
E contents; E contents;