From 0674a5850226eb79f70153c95769ea5b1c2cc02d Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Thu, 2 Feb 2017 20:32:49 +0300 Subject: [PATCH] idm::check extended idm::check_unlocked idm::find_unlocked idm::get_unlocked --- rpcs3/Emu/Cell/lv2/sys_memory.cpp | 8 +-- rpcs3/Emu/Cell/lv2/sys_mmapper.cpp | 8 +-- rpcs3/Emu/Cell/lv2/sys_semaphore.cpp | 10 ++-- rpcs3/Emu/IdManager.h | 83 ++++++++++++++++++++-------- 4 files changed, 72 insertions(+), 37 deletions(-) diff --git a/rpcs3/Emu/Cell/lv2/sys_memory.cpp b/rpcs3/Emu/Cell/lv2/sys_memory.cpp index 1f512b0a8d..5eb1f52b0f 100644 --- a/rpcs3/Emu/Cell/lv2/sys_memory.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_memory.cpp @@ -102,9 +102,9 @@ error_code sys_memory_allocate_from_container(u32 size, u32 cid, u64 flags, vm:: return CELL_ESRCH; } - if (ct.value) + if (ct.ret) { - return ct.value; + return ct.ret; } // Allocate memory, write back the start address of the allocated area, use cid as the supplementary info @@ -219,9 +219,9 @@ error_code sys_memory_container_destroy(u32 cid) return CELL_ESRCH; } - if (ct.value) + if (ct.ret) { - return ct.value; + return ct.ret; } // Return "physical memory" to the default container diff --git a/rpcs3/Emu/Cell/lv2/sys_mmapper.cpp b/rpcs3/Emu/Cell/lv2/sys_mmapper.cpp index 096d6bbe7d..573e04330f 100644 --- a/rpcs3/Emu/Cell/lv2/sys_mmapper.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_mmapper.cpp @@ -157,9 +157,9 @@ error_code sys_mmapper_allocate_shared_memory_from_container(u64 unk, u32 size, return CELL_ESRCH; } - if (ct.value) + if (ct.ret) { - return ct.value; + return ct.ret; } // Generate a new mem ID @@ -215,9 +215,9 @@ error_code sys_mmapper_free_shared_memory(u32 mem_id) return CELL_ESRCH; } - if (mem.value) + if (mem.ret) { - return mem.value; + return mem.ret; } // Return "physical memory" to the memory container diff --git a/rpcs3/Emu/Cell/lv2/sys_semaphore.cpp b/rpcs3/Emu/Cell/lv2/sys_semaphore.cpp index be9e9e6ae1..5c89459519 100644 --- a/rpcs3/Emu/Cell/lv2/sys_semaphore.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_semaphore.cpp @@ -70,9 +70,9 @@ error_code sys_semaphore_destroy(u32 sem_id) return CELL_ESRCH; } - if (sem.value) + if (sem.ret) { - return sem.value; + return sem.ret; } return CELL_OK; @@ -112,7 +112,7 @@ error_code sys_semaphore_wait(ppu_thread& ppu, u32 sem_id, u64 timeout) return CELL_ESRCH; } - if (sem.value) + if (sem.ret) { return CELL_OK; } @@ -182,7 +182,7 @@ error_code sys_semaphore_trywait(u32 sem_id) return CELL_ESRCH; } - if (!sem.value) + if (!sem.ret) { return not_an_error(CELL_EBUSY); } @@ -219,7 +219,7 @@ error_code sys_semaphore_post(u32 sem_id, s32 count) return CELL_ESRCH; } - if (sem.value) + if (sem.ret) { return CELL_OK; } diff --git a/rpcs3/Emu/IdManager.h b/rpcs3/Emu/IdManager.h index f8d522d2da..6fadac64f8 100644 --- a/rpcs3/Emu/IdManager.h +++ b/rpcs3/Emu/IdManager.h @@ -227,33 +227,39 @@ class idm using void_type = void; }; - // Helper + // Helper type: pointer + return value propagated template struct return_pair { std::shared_ptr ptr; - RT value; + RT ret; explicit operator bool() const { return ptr.operator bool(); } - auto operator->() const + T* operator->() const { return ptr.get(); } }; - template - struct return_pair + // Unsafe specialization (not refcounted) + template + struct return_pair { - bool result; - RT value; + T* ptr; + RT ret; explicit operator bool() const { - return result; + return ptr != nullptr; + } + + T* operator->() const + { + return ptr; } }; @@ -382,46 +388,75 @@ public: return id_manager::id_traits::invalid; } + // Access the ID record without locking (unsafe) + template + static inline id_manager::id_map::pointer find_unlocked(u32 id) + { + return find_id(id); + } + + // Check the ID without locking (can be called from other method) + template + static inline Get* check_unlocked(u32 id) + { + if (const auto found = find_id(id)) + { + return static_cast(found->second.get()); + } + + return nullptr; + } + // Check the ID template - static inline explicit_bool_t check(u32 id) + static inline Get* check(u32 id) { reader_lock lock(id_manager::g_mutex); - return find_id(id) != nullptr; + return check_unlocked(id); } // Check the ID, access object under shared lock template , typename = std::enable_if_t::value>> - static inline explicit_bool_t check(u32 id, F&& func, int = 0) + static inline Get* check(u32 id, F&& func, int = 0) { reader_lock lock(id_manager::g_mutex); - const auto found = find_id(id); - - if (UNLIKELY(found == nullptr)) + if (const auto ptr = check_unlocked(id)) { - return false; + func(*ptr); + return ptr; } - - func(*static_cast(found->second.get())); - return true; + + 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) + static inline return_pair check(u32 id, F&& func) { reader_lock lock(id_manager::g_mutex); + if (const auto ptr = check_unlocked(id)) + { + return {ptr, func(*ptr)}; + } + + return {nullptr}; + } + + // Get the object without locking (can be called from other method) + template + static inline std::shared_ptr get_unlocked(u32 id) + { const auto found = find_id(id); if (UNLIKELY(found == nullptr)) { - return {false}; + return nullptr; } - return {true, func(*static_cast(found->second.get()))}; + return {found->second, static_cast(found->second.get())}; } // Get the object @@ -807,12 +842,12 @@ public: } // Check whether the object exists - template - static inline explicit_bool_t check() + template + static inline T* check() { reader_lock lock(id_manager::g_mutex); - return g_vec[get_type()].second != nullptr; + return static_cast(g_vec[get_type()].second.get()); } // Get the object (returns nullptr if it doesn't exist)