From 67a527010db826603239617fea7f85fa2d8fb541 Mon Sep 17 00:00:00 2001 From: Maschell Date: Tue, 20 Jan 2026 18:26:50 +0100 Subject: [PATCH] Use std::recursive vs std::mutex --- source/export.cpp | 9 ++++++--- source/function_patcher.cpp | 4 ++++ source/main.cpp | 10 ++++++---- source/utils/globals.cpp | 2 +- source/utils/globals.h | 3 ++- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/source/export.cpp b/source/export.cpp index a550363..b6d15b6 100644 --- a/source/export.cpp +++ b/source/export.cpp @@ -2,8 +2,11 @@ #include "PatchedFunctionData.h" #include "function_patcher.h" #include "utils/globals.h" + +#include #include #include + #include WUT_CHECK_OFFSET(function_replacement_data_v2_t, 0x00, VERSION); @@ -50,7 +53,7 @@ FunctionPatcherStatus FPAddFunctionPatch(function_replacement_data_t *function_d } { - std::lock_guard lock(gPatchedFunctionsMutex); + std::lock_guard lock(gPatchedFunctionsMutex); gPatchedFunctions.push_back(std::move(functionData)); OSMemoryBarrier(); @@ -64,7 +67,7 @@ bool FunctionPatcherPatchFunction(function_replacement_data_t *function_data, Pa } FunctionPatcherStatus FPRemoveFunctionPatch(PatchedFunctionHandle handle) { - std::lock_guard lock(gPatchedFunctionsMutex); + std::lock_guard lock(gPatchedFunctionsMutex); std::vector> toBeTempRestored; bool found = false; int32_t erasePosition = 0; @@ -132,7 +135,7 @@ FunctionPatcherStatus FPIsFunctionPatched(PatchedFunctionHandle handle, bool *ou if (outIsFunctionPatched == nullptr) { return FUNCTION_PATCHER_RESULT_INVALID_ARGUMENT; } - std::lock_guard lock(gPatchedFunctionsMutex); + std::lock_guard lock(gPatchedFunctionsMutex); for (auto &cur : gPatchedFunctions) { if (cur->getHandle() == handle) { *outIsFunctionPatched = cur->isPatched; diff --git a/source/function_patcher.cpp b/source/function_patcher.cpp index ea102cf..66d61b0 100644 --- a/source/function_patcher.cpp +++ b/source/function_patcher.cpp @@ -4,11 +4,15 @@ #include "utils/CThread.h" #include "utils/logger.h" #include "utils/utils.h" + #include #include #include + #include + #include +#include static void writeDataAndFlushIC(CThread *thread, void *arg) { auto *data = (PatchedFunctionData *) arg; diff --git a/source/main.cpp b/source/main.cpp index 5818992..15063ac 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -4,9 +4,11 @@ #include "utils/globals.h" #include "utils/logger.h" #include "utils/utils.h" + #include #include #include +#include #include #include #include @@ -41,7 +43,7 @@ void UpdateFunctionPointer() { } void CheckIfPatchedFunctionsAreStillInMemory() { - std::lock_guard lock(gPatchedFunctionsMutex); + std::lock_guard lock(gPatchedFunctionsMutex); // Check if rpl has been unloaded by comparing the instruction. std::set physicalAddressesUnchanged; std::set physicalAddressesChanged; @@ -129,12 +131,12 @@ void notify_callback(OSDynLoad_Module module, OSDynLoad_NotifyReason reason, OSDynLoad_NotifyData *infos) { if (reason == OS_DYNLOAD_NOTIFY_LOADED) { - std::lock_guard lock(gPatchedFunctionsMutex); + std::lock_guard lock(gPatchedFunctionsMutex); for (auto &cur : gPatchedFunctions) { PatchFunction(cur); } } else if (reason == OS_DYNLOAD_NOTIFY_UNLOADED) { - std::lock_guard lock(gPatchedFunctionsMutex); + std::lock_guard lock(gPatchedFunctionsMutex); auto library = gFunctionAddressProvider->getTypeForHandle(module); if (library != LIBRARY_OTHER) { for (auto &cur : gPatchedFunctions) { @@ -162,7 +164,7 @@ WUMS_APPLICATION_STARTS() { initLogging(); { - std::lock_guard lock(gPatchedFunctionsMutex); + std::lock_guard lock(gPatchedFunctionsMutex); // reset function patch status if the rpl they were patching has been unloaded from memory. CheckIfPatchedFunctionsAreStillInMemory(); DEBUG_FUNCTION_LINE_VERBOSE("Patch all functions"); diff --git a/source/utils/globals.cpp b/source/utils/globals.cpp index d5a6619..69e5f39 100644 --- a/source/utils/globals.cpp +++ b/source/utils/globals.cpp @@ -4,7 +4,7 @@ char gJumpHeapData[JUMP_HEAP_DATA_SIZE] __attribute__((section(".data"))); MEMHeapHandle gJumpHeapHandle __attribute__((section(".data"))); std::shared_ptr gFunctionAddressProvider; -std::mutex gPatchedFunctionsMutex; +std::recursive_mutex gPatchedFunctionsMutex; std::vector> gPatchedFunctions; void *(*gMEMAllocFromDefaultHeapExForThreads)(uint32_t size, int align); diff --git a/source/utils/globals.h b/source/utils/globals.h index 002e655..94135c9 100644 --- a/source/utils/globals.h +++ b/source/utils/globals.h @@ -3,6 +3,7 @@ #include "version.h" #include #include +#include #include #define MODULE_VERSION "v0.2.3" @@ -13,7 +14,7 @@ extern char gJumpHeapData[]; extern MEMHeapHandle gJumpHeapHandle; extern std::shared_ptr gFunctionAddressProvider; -extern std::mutex gPatchedFunctionsMutex; +extern std::recursive_mutex gPatchedFunctionsMutex; extern std::vector> gPatchedFunctions; extern void *(*gMEMAllocFromDefaultHeapExForThreads)(uint32_t size, int align);