From 7ccdea78226a971f3e47afb3bc2024c875e0a8fd Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sun, 24 Jul 2016 02:56:03 +0300 Subject: [PATCH] Removed std::enable_shared_from_this Minor ID manager refactoring --- Utilities/Thread.cpp | 12 ++++++------ Utilities/Thread.h | 10 +++++----- rpcs3/Emu/Cell/RawSPUThread.cpp | 4 ++-- rpcs3/Emu/Cell/RawSPUThread.h | 2 +- rpcs3/Emu/Cell/lv2/sys_cond.cpp | 4 ++-- rpcs3/Emu/Cell/lv2/sys_mutex.cpp | 6 +++--- rpcs3/Emu/Cell/lv2/sys_rwlock.cpp | 6 +++--- rpcs3/Emu/IdManager.h | 32 ++++++++++++++----------------- rpcs3/Emu/RSX/RSXThread.cpp | 5 ++++- rpcs3/Emu/RSX/RSXThread.h | 2 +- 10 files changed, 41 insertions(+), 42 deletions(-) diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index 1475256ce7..95e2282f51 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -2238,18 +2238,18 @@ std::string named_thread::get_name() const return fmt::format("('%s') Unnamed Thread", typeid(*this).name()); } -void named_thread::start() +void named_thread::start_thread(const std::shared_ptr& _this) { - // Get shared_ptr instance (will throw if called from the constructor or the object has been created incorrectly) - auto&& ptr = shared_from_this(); + // Ensure it's not called from the constructor and the correct object is passed + ENSURES(_this.get() == this); // Run thread - m_thread = thread_ctrl::spawn(get_name(), [thread = std::move(ptr)]() + m_thread = thread_ctrl::spawn(get_name(), [this, _this]() { try { LOG_TRACE(GENERAL, "Thread started"); - thread->on_task(); + on_task(); LOG_TRACE(GENERAL, "Thread ended"); } catch (const std::exception& e) @@ -2262,6 +2262,6 @@ void named_thread::start() LOG_NOTICE(GENERAL, "Thread aborted"); } - thread->on_exit(); + on_exit(); }); } diff --git a/Utilities/Thread.h b/Utilities/Thread.h index 4e560d6f14..10aee157c0 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -312,7 +312,7 @@ public: } }; -class named_thread : public std::enable_shared_from_this +class named_thread { // Pointer to managed resource (shared with actual thread) std::shared_ptr m_thread; @@ -329,8 +329,8 @@ public: virtual std::string get_name() const; protected: - // Start thread (cannot be called from the constructor: should throw bad_weak_ptr in such case) - void start(); + // Start thread (cannot be called from the constructor: should throw in such case) + void start_thread(const std::shared_ptr& _this); // Thread task (called in the thread) virtual void on_task() = 0; @@ -340,9 +340,9 @@ protected: public: // ID initialization - virtual void on_init() + virtual void on_init(const std::shared_ptr& _this) { - start(); + return start_thread(_this); } // ID finalization diff --git a/rpcs3/Emu/Cell/RawSPUThread.cpp b/rpcs3/Emu/Cell/RawSPUThread.cpp index 22de49dcca..2003b47f32 100644 --- a/rpcs3/Emu/Cell/RawSPUThread.cpp +++ b/rpcs3/Emu/Cell/RawSPUThread.cpp @@ -24,7 +24,7 @@ void RawSPUThread::cpu_task() npc = pc | ((ch_event_stat & SPU_EVENT_INTR_ENABLED) != 0); } -void RawSPUThread::on_init() +void RawSPUThread::on_init(const std::shared_ptr& _this) { if (!offset) { @@ -33,7 +33,7 @@ void RawSPUThread::on_init() const_cast(offset) = vm::falloc(RAW_SPU_BASE_ADDR + RAW_SPU_OFFSET * index, 0x40000); VERIFY(offset); - SPUThread::on_init(); + SPUThread::on_init(_this); } } diff --git a/rpcs3/Emu/Cell/RawSPUThread.h b/rpcs3/Emu/Cell/RawSPUThread.h index 5e37357e0d..871fafda06 100644 --- a/rpcs3/Emu/Cell/RawSPUThread.h +++ b/rpcs3/Emu/Cell/RawSPUThread.h @@ -14,7 +14,7 @@ public: static constexpr u32 id_min = 0; static constexpr u32 id_max = 4; - void on_init() override; + void on_init(const std::shared_ptr&) override; RawSPUThread(const std::string& name); diff --git a/rpcs3/Emu/Cell/lv2/sys_cond.cpp b/rpcs3/Emu/Cell/lv2/sys_cond.cpp index db5f2478fb..cbab40e538 100644 --- a/rpcs3/Emu/Cell/lv2/sys_cond.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_cond.cpp @@ -23,7 +23,7 @@ void lv2_cond_t::notify(lv2_lock_t, cpu_thread* thread) } else { - mutex->owner = std::static_pointer_cast(thread->shared_from_this()); + mutex->owner = idm::get(thread->id); VERIFY(!thread->state.test_and_set(cpu_state::signal)); (*thread)->notify(); @@ -212,7 +212,7 @@ s32 sys_cond_wait(PPUThread& ppu, u32 cond_id, u64 timeout) // try to reown mutex and exit if timed out if (!cond->mutex->owner) { - cond->mutex->owner = std::static_pointer_cast(ppu.shared_from_this()); + cond->mutex->owner = idm::get(ppu.id); break; } diff --git a/rpcs3/Emu/Cell/lv2/sys_mutex.cpp b/rpcs3/Emu/Cell/lv2/sys_mutex.cpp index fe0687cf67..88d00bfe4b 100644 --- a/rpcs3/Emu/Cell/lv2/sys_mutex.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_mutex.cpp @@ -18,7 +18,7 @@ void lv2_mutex_t::unlock(lv2_lock_t) if (sq.size()) { // pick new owner; protocol is ignored in current implementation - owner = std::static_pointer_cast(sq.front()->shared_from_this()); + owner = idm::get(sq.front()->id); VERIFY(!owner->state.test_and_set(cpu_state::signal)); (*owner)->notify(); @@ -127,7 +127,7 @@ s32 sys_mutex_lock(PPUThread& ppu, u32 mutex_id, u64 timeout) // lock immediately if not locked if (!mutex->owner) { - mutex->owner = std::static_pointer_cast(ppu.shared_from_this()); + mutex->owner = idm::get(ppu.id); return CELL_OK; } @@ -202,7 +202,7 @@ s32 sys_mutex_trylock(PPUThread& ppu, u32 mutex_id) } // own the mutex if free - mutex->owner = std::static_pointer_cast(ppu.shared_from_this()); + mutex->owner = idm::get(ppu.id); return CELL_OK; } diff --git a/rpcs3/Emu/Cell/lv2/sys_rwlock.cpp b/rpcs3/Emu/Cell/lv2/sys_rwlock.cpp index 89c43c48f5..90893d916b 100644 --- a/rpcs3/Emu/Cell/lv2/sys_rwlock.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_rwlock.cpp @@ -16,7 +16,7 @@ void lv2_rwlock_t::notify_all(lv2_lock_t) // pick a new writer if possible; protocol is ignored in current implementation if (!readers && !writer && wsq.size()) { - writer = std::static_pointer_cast(wsq.front()->shared_from_this()); + writer = idm::get(wsq.front()->id); VERIFY(!writer->state.test_and_set(cpu_state::signal)); (*writer)->notify(); @@ -221,7 +221,7 @@ s32 sys_rwlock_wlock(PPUThread& ppu, u32 rw_lock_id, u64 timeout) if (!rwlock->readers && !rwlock->writer) { - rwlock->writer = std::static_pointer_cast(ppu.shared_from_this()); + rwlock->writer = idm::get(ppu.id); return CELL_OK; } @@ -293,7 +293,7 @@ s32 sys_rwlock_trywlock(PPUThread& ppu, u32 rw_lock_id) return CELL_EBUSY; } - rwlock->writer = std::static_pointer_cast(ppu.shared_from_this()); + rwlock->writer = idm::get(ppu.id); return CELL_OK; } diff --git a/rpcs3/Emu/IdManager.h b/rpcs3/Emu/IdManager.h index c78175fdc8..ce86d726fb 100644 --- a/rpcs3/Emu/IdManager.h +++ b/rpcs3/Emu/IdManager.h @@ -35,7 +35,7 @@ namespace id_manager template struct on_init { - static inline void func(T*) + static inline void func(T*, const std::shared_ptr&) { // Forbid forward declarations static constexpr auto size = sizeof(std::conditional_t::value, void*, T>); @@ -43,11 +43,11 @@ namespace id_manager }; template - struct on_init().on_init())> + struct on_init().on_init(std::declval&>()))> { - static inline void func(T* ptr) + static inline void func(T* ptr, const std::shared_ptr&_ptr) { - if (ptr) ptr->on_init(); + if (ptr) ptr->on_init(_ptr); } }; @@ -87,7 +87,6 @@ namespace id_manager static u32 add_type(); public: - void(*on_init)(void*) = nullptr; void(*on_stop)(void*) = nullptr; // Get type index @@ -101,10 +100,7 @@ namespace id_manager template static inline void update() { - auto& info = access()[get_index()]; - - info.on_init = [](void* ptr) { return_ id_manager::on_init::func(static_cast(ptr)); }; - info.on_stop = [](void* ptr) { return_ id_manager::on_stop::func(static_cast(ptr)); }; + access()[get_index()].on_stop = [](void* ptr) { return_ id_manager::on_stop::func(static_cast(ptr)); }; } // Read all registered types @@ -251,7 +247,7 @@ public: { if (auto pair = create_id(WRAP_EXPR(std::make_shared(std::forward(args)...)))) { - id_manager::on_init::func(static_cast(pair->second.get())); + id_manager::on_init::func(static_cast(pair->second.get()), pair->second); id_manager::on_stop::func(nullptr); return{ pair->second, static_cast(pair->second.get()) }; } @@ -265,7 +261,7 @@ public: { if (auto pair = create_id(WRAP_EXPR(std::make_shared(std::forward(args)...)))) { - id_manager::on_init::func(static_cast(pair->second.get())); + id_manager::on_init::func(static_cast(pair->second.get()), pair->second); id_manager::on_stop::func(nullptr); return pair->first; } @@ -279,7 +275,7 @@ public: { if (auto pair = create_id(WRAP_EXPR(ptr))) { - id_manager::on_init::func(static_cast(pair->second.get())); + id_manager::on_init::func(static_cast(pair->second.get()), pair->second); id_manager::on_stop::func(nullptr); return pair->first; } @@ -293,7 +289,7 @@ public: { if (auto pair = create_id(std::forward(provider))) { - id_manager::on_init::func(static_cast(pair->second.get())); + id_manager::on_init::func(static_cast(pair->second.get()), pair->second); id_manager::on_stop::func(nullptr); return { pair->second, static_cast(pair->second.get()) }; } @@ -477,7 +473,7 @@ public: if (ptr) { - id_manager::on_init::func(ptr.get()); + id_manager::on_init::func(ptr.get(), ptr); id_manager::on_stop::func(nullptr); } @@ -506,7 +502,7 @@ public: id_manager::on_stop::func(static_cast(old.get())); } - id_manager::on_init::func(ptr.get()); + id_manager::on_init::func(ptr.get(), ptr); return ptr; } @@ -530,7 +526,7 @@ public: if (ptr) { - id_manager::on_init::func(ptr.get()); + id_manager::on_init::func(ptr.get(), ptr); id_manager::on_stop::func(nullptr); } @@ -559,7 +555,7 @@ public: id_manager::on_stop::func(static_cast(old.get())); } - id_manager::on_init::func(ptr.get()); + id_manager::on_init::func(ptr.get(), ptr); return ptr; } @@ -585,7 +581,7 @@ public: } } - id_manager::on_init::func(ptr.get()); + id_manager::on_init::func(ptr.get(), ptr); id_manager::on_stop::func(nullptr); return ptr; } diff --git a/rpcs3/Emu/RSX/RSXThread.cpp b/rpcs3/Emu/RSX/RSXThread.cpp index 462d6a163a..ab094404bd 100644 --- a/rpcs3/Emu/RSX/RSXThread.cpp +++ b/rpcs3/Emu/RSX/RSXThread.cpp @@ -2,6 +2,7 @@ #include "Utilities/Config.h" #include "Emu/Memory/Memory.h" #include "Emu/System.h" +#include "Emu/IdManager.h" #include "RSXThread.h" #include "Emu/Cell/PPUCallback.h" @@ -14,6 +15,8 @@ #include +class GSRender; + #define CMD_DEBUG 0 cfg::bool_entry g_cfg_rsx_write_color_buffers(cfg::root.video, "Write Color Buffers"); @@ -881,7 +884,7 @@ namespace rsx m_used_gcm_commands.clear(); on_init_rsx(); - start(); + start_thread(fxm::get()); } GcmTileInfo *thread::find_tile(u32 offset, u32 location) diff --git a/rpcs3/Emu/RSX/RSXThread.h b/rpcs3/Emu/RSX/RSXThread.h index 11ec5deb57..fd2331968a 100644 --- a/rpcs3/Emu/RSX/RSXThread.h +++ b/rpcs3/Emu/RSX/RSXThread.h @@ -274,7 +274,7 @@ namespace rsx public: virtual std::string get_name() const override; - virtual void on_init() override {} // disable start() (TODO) + virtual void on_init(const std::shared_ptr&) override {} // disable start() (TODO) virtual void on_stop() override {} // disable join() virtual void begin();