From bd5253047b18570b44bdaeb2102bf9af4909ae42 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Tue, 10 Nov 2020 01:11:30 +0300 Subject: [PATCH] Improve DYNAMIC_IMPORT Don't call get_proc_address every time if if failed. Also rename Utilities/dynamic_library.h to util/dyn_lib.hpp --- Utilities/sync.h | 2 +- rpcs3/Emu/CMakeLists.txt | 2 +- rpcs3/emucore.vcxproj | 6 ++-- rpcs3/emucore.vcxproj.filters | 4 +-- rpcs3/main.cpp | 2 +- .../util/dyn_lib.cpp | 2 +- .../util/dyn_lib.hpp | 32 ++++++++++--------- 7 files changed, 27 insertions(+), 23 deletions(-) rename Utilities/dynamic_library.cpp => rpcs3/util/dyn_lib.cpp (97%) rename Utilities/dynamic_library.h => rpcs3/util/dyn_lib.hpp (73%) diff --git a/Utilities/sync.h b/Utilities/sync.h index 71b52c5e93..7309499ad5 100644 --- a/Utilities/sync.h +++ b/Utilities/sync.h @@ -4,7 +4,7 @@ #include "types.h" #include "util/atomic.hpp" -#include "dynamic_library.h" +#include "util/dyn_lib.hpp" #ifdef _WIN32 #define NOMINMAX diff --git a/rpcs3/Emu/CMakeLists.txt b/rpcs3/Emu/CMakeLists.txt index f51f625bb7..fb702f83fe 100644 --- a/rpcs3/Emu/CMakeLists.txt +++ b/rpcs3/Emu/CMakeLists.txt @@ -40,11 +40,11 @@ target_sources(rpcs3_emu PRIVATE ../util/yaml.cpp ../util/cereal.cpp ../util/vm_native.cpp + ../util/dyn_lib.cpp ../../Utilities/bin_patch.cpp ../../Utilities/cheat_info.cpp ../../Utilities/cond.cpp ../../Utilities/Config.cpp - ../../Utilities/dynamic_library.cpp ../../Utilities/File.cpp ../../Utilities/JIT.cpp ../../Utilities/LUrlParser.cpp diff --git a/rpcs3/emucore.vcxproj b/rpcs3/emucore.vcxproj index f6c75108aa..384c1c31f4 100644 --- a/rpcs3/emucore.vcxproj +++ b/rpcs3/emucore.vcxproj @@ -141,7 +141,9 @@ NotUsing - + + NotUsing + NotUsing @@ -527,7 +529,6 @@ - @@ -540,6 +541,7 @@ + diff --git a/rpcs3/emucore.vcxproj.filters b/rpcs3/emucore.vcxproj.filters index 3b80f08550..2595b67a86 100644 --- a/rpcs3/emucore.vcxproj.filters +++ b/rpcs3/emucore.vcxproj.filters @@ -734,7 +734,7 @@ Emu - + Utilities @@ -1561,7 +1561,7 @@ Emu\GPU\RSX - + Utilities diff --git a/rpcs3/main.cpp b/rpcs3/main.cpp index 45cd8bc159..daf63e3c69 100644 --- a/rpcs3/main.cpp +++ b/rpcs3/main.cpp @@ -17,7 +17,7 @@ #include "Utilities/sema.h" #ifdef _WIN32 #include -#include "Utilities/dynamic_library.h" +#include "util/dyn_lib.hpp" DYNAMIC_IMPORT("ntdll.dll", NtQueryTimerResolution, NTSTATUS(PULONG MinimumResolution, PULONG MaximumResolution, PULONG CurrentResolution)); DYNAMIC_IMPORT("ntdll.dll", NtSetTimerResolution, NTSTATUS(ULONG DesiredResolution, BOOLEAN SetResolution, PULONG CurrentResolution)); #else diff --git a/Utilities/dynamic_library.cpp b/rpcs3/util/dyn_lib.cpp similarity index 97% rename from Utilities/dynamic_library.cpp rename to rpcs3/util/dyn_lib.cpp index fb2a1f128b..41ff2787ee 100644 --- a/Utilities/dynamic_library.cpp +++ b/rpcs3/util/dyn_lib.cpp @@ -1,5 +1,5 @@ #include "stdafx.h" -#include "dynamic_library.h" +#include "dyn_lib.hpp" #ifdef _WIN32 #include diff --git a/Utilities/dynamic_library.h b/rpcs3/util/dyn_lib.hpp similarity index 73% rename from Utilities/dynamic_library.h rename to rpcs3/util/dyn_lib.hpp index 976912572d..fc013c8f7f 100644 --- a/Utilities/dynamic_library.h +++ b/rpcs3/util/dyn_lib.hpp @@ -53,40 +53,42 @@ namespace utils template struct dynamic_import { - R (*ptr)(Args...); + atomic_t ptr; const char* const lib; const char* const name; // Constant initialization - constexpr dynamic_import(const char* lib, const char* name) - : ptr(nullptr) + constexpr dynamic_import(const char* lib, const char* name) noexcept + : ptr(-1) , lib(lib) , name(name) { } - void init() + void init() noexcept { - if (!ptr) - { - // TODO: atomic - ptr = reinterpret_cast(get_proc_address(lib, name)); - } + ptr.release(reinterpret_cast(get_proc_address(lib, name))); } - operator bool() + operator bool() noexcept { - init(); + if (ptr == umax) [[unlikely]] + { + init(); + } - return ptr; + return ptr != 0; } // Caller - R operator()(Args... args) + R operator()(Args... args) noexcept { - init(); + if (ptr == umax) [[unlikely]] + { + init(); + } - return ptr(args...); + return reinterpret_cast(ptr.load())(args...); } }; }