-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPatchThreading.cpp
More file actions
41 lines (36 loc) · 1.78 KB
/
PatchThreading.cpp
File metadata and controls
41 lines (36 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "pch.h"
#include "PatchThreading.h"
void DoBlocking(const std::string& moduleName, const PTR_SIZE moduleAddress, const PatchFunction function, bool* result, std::mutex* resultLock, AllocateMemory* allocator) {
LogBufferImpl logBuffer;
const auto newResult = function(moduleName, moduleAddress, allocator, &logBuffer);
resultLock->lock();
logBuffer.CloseAndCopyToStream(out);
*result = *result && newResult;
resultLock->unlock();
}
bool DoPatchFunctionsBlocking(const std::string& moduleName, const PTR_SIZE moduleAddress, AllocateMemory* allocator, const std::vector<PatchFunction>& injectorFunctions) {
bool result = true;
std::mutex resultLock;
for (const auto& func : injectorFunctions) {
DoBlocking(moduleName, moduleAddress, func, &result, &resultLock, allocator);
}
return result;
}
std::thread DoInThread(const std::string& moduleName, const PTR_SIZE moduleAddress, const PatchFunction function, bool* result, std::mutex* resultLock, AllocateMemory* allocator) {
return std::thread([moduleName, moduleAddress, function, result, resultLock, allocator] {
DoBlocking(moduleName, moduleAddress, function, result, resultLock, allocator);
});
}
bool DoPatchFunctionsAsync(const std::string& moduleName, const PTR_SIZE moduleAddress, AllocateMemory* allocator, const std::vector<PatchFunction>& injectorFunctions) {
bool result = true;
std::mutex resultLock;
std::vector<std::thread> threads;
threads.reserve(injectorFunctions.size());
for (const auto& func : injectorFunctions) {
threads.push_back(DoInThread(moduleName, moduleAddress, func, &result, &resultLock, allocator));
}
for (auto& thread : threads) {
thread.join();
}
return result;
}