From 99ffc3fca9aa67131efaf4a5231cb79e9c7a7a50 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Wed, 5 Sep 2018 15:10:37 +0300 Subject: [PATCH] Use std::invoke_result_t Also simplify idm code --- Utilities/Thread.h | 2 +- rpcs3/Emu/Cell/PPUModule.h | 2 +- rpcs3/Emu/IdManager.h | 132 +++++++++++++------------------------ 3 files changed, 49 insertions(+), 87 deletions(-) diff --git a/Utilities/Thread.h b/Utilities/Thread.h index 75c6ea3dda..ad891fc8af 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -209,7 +209,7 @@ public: } // Wait until pred(). Abortable, may throw. - template> + template> static inline RT wait(F&& pred) { while (true) diff --git a/rpcs3/Emu/Cell/PPUModule.h b/rpcs3/Emu/Cell/PPUModule.h index 0fe5709d71..ee18c2fb2e 100644 --- a/rpcs3/Emu/Cell/PPUModule.h +++ b/rpcs3/Emu/Cell/PPUModule.h @@ -271,7 +271,7 @@ template ppu_static_function* ppu_module_manager::registered::info = nullptr; // Call specified function directly if LLE is not available, call LLE equivalent in callback style otherwise -template> +template> inline RT ppu_execute_function_or_callback(ppu_thread& ppu, Args&&... args) { vm::ptr func = vm::cast(*ppu_module_manager::find_static_function().export_addr); diff --git a/rpcs3/Emu/IdManager.h b/rpcs3/Emu/IdManager.h index ffb03ed1fe..2919a7c8fa 100644 --- a/rpcs3/Emu/IdManager.h +++ b/rpcs3/Emu/IdManager.h @@ -376,7 +376,7 @@ public: } // Add a new ID for an object returned by provider() - template > + template > static inline u32 import(F&& provider) { if (auto pair = create_id(std::forward(provider))) @@ -417,32 +417,32 @@ public: } // Check the ID, access object under shared lock - template , typename = std::enable_if_t::value>> - static inline Get* check(u32 id, F&& func, int = 0) + template > + static inline auto check(u32 id, F&& func) { reader_lock lock(id_manager::g_mutex); if (const auto ptr = check_unlocked(id)) { - func(*ptr); - return ptr; + if constexpr (!std::is_void_v) + { + return return_pair{ptr, func(*ptr)}; + } + else + { + func(*ptr); + return ptr; + } } - return nullptr; - } - - // Check the ID, access object under reader lock, propagate return value - template , typename = std::enable_if_t::value>> - static inline return_pair check(u32 id, F&& func) - { - reader_lock lock(id_manager::g_mutex); - - if (const auto ptr = check_unlocked(id)) + if constexpr (!std::is_void_v) { - return {ptr, func(*ptr)}; + return return_pair{nullptr}; + } + else + { + return static_cast(nullptr); } - - return {nullptr}; } // Get the object without locking (can be called from other method) @@ -476,45 +476,29 @@ public: } // Get the object, access object under reader lock - template , typename = std::enable_if_t::value>> - static inline auto get(u32 id, F&& func, int = 0) + template > + static inline std::conditional_t, std::shared_ptr, return_pair> get(u32 id, F&& func) { - using result_type = std::shared_ptr; - reader_lock lock(id_manager::g_mutex); const auto found = find_id(id); if (UNLIKELY(found == nullptr)) { - return result_type{nullptr}; + return {nullptr}; } const auto ptr = static_cast(found->second.get()); - func(*ptr); - - return result_type{found->second, ptr}; - } - - // Get the object, access object under reader lock, propagate return value - template , typename = std::enable_if_t::value>> - static inline auto get(u32 id, F&& func) - { - using result_type = return_pair; - - reader_lock lock(id_manager::g_mutex); - - const auto found = find_id(id); - - if (UNLIKELY(found == nullptr)) + if constexpr (std::is_void_v) { - return result_type{nullptr}; + func(*ptr); + return {found->second, ptr}; + } + else + { + return {{found->second, ptr}, func(*ptr)}; } - - const auto ptr = static_cast(found->second.get()); - - return result_type{{found->second, ptr}, func(*ptr)}; } // Access all objects of specified type. Returns the number of objects processed. @@ -615,63 +599,41 @@ public: } // Remove the ID after accessing the object under writer lock, return the object and propagate return value - template , typename = std::enable_if_t::value>> - static inline auto withdraw(u32 id, F&& func, int = 0) + template > + static inline std::conditional_t, std::shared_ptr, return_pair> withdraw(u32 id, F&& func) { - using result_type = std::shared_ptr; + std::unique_lock lock(id_manager::g_mutex); - std::shared_ptr ptr; + if (const auto found = find_id(id)) { - std::lock_guard lock(id_manager::g_mutex); + const auto _ptr = static_cast(found->second.get()); - if (const auto found = find_id(id)) + if constexpr (std::is_void_v) { - func(*static_cast(found->second.get())); - - ptr = std::move(found->second); + func(*_ptr); + std::shared_ptr ptr = std::move(found->second); + lock.unlock(); + id_manager::on_stop::func(static_cast(ptr.get())); + return {ptr, static_cast(ptr.get())}; } else { - return result_type{nullptr}; - } - } - - id_manager::on_stop::func(static_cast(ptr.get())); - return result_type{ptr, static_cast(ptr.get())}; - } - - // Conditionally remove the ID (if return value evaluates to false) after accessing the object under writer lock, return the object and propagate return value - template , typename = std::enable_if_t::value>> - static inline auto withdraw(u32 id, F&& func) - { - using result_type = return_pair; - - std::shared_ptr ptr; - FRT ret; - { - std::lock_guard lock(id_manager::g_mutex); - - if (const auto found = find_id(id)) - { - const auto _ptr = static_cast(found->second.get()); - - ret = func(*_ptr); + FRT ret = func(*_ptr); if (ret) { - return result_type{{found->second, _ptr}, std::move(ret)}; + // If return value evaluates to true, don't delete the object (error code) + return {{found->second, _ptr}, std::move(ret)}; } - ptr = std::move(found->second); - } - else - { - return result_type{nullptr}; + std::shared_ptr ptr = std::move(found->second); + lock.unlock(); + id_manager::on_stop::func(static_cast(ptr.get())); + return {{ptr, static_cast(ptr.get())}, std::move(ret)}; } } - id_manager::on_stop::func(static_cast(ptr.get())); - return result_type{{ptr, static_cast(ptr.get())}, std::move(ret)}; + return {nullptr}; } };