From 1c51eac8c9a9427e58463b7f9bb106450c80a33a Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Mon, 30 Jan 2017 16:20:09 +0300 Subject: [PATCH] sys_ppu LV2_LOCK removal error_code introduction --- rpcs3/Emu/Cell/PPUThread.h | 6 +- rpcs3/Emu/Cell/lv2/sys_ppu_thread.cpp | 147 ++++++++++++-------------- rpcs3/Emu/Cell/lv2/sys_ppu_thread.h | 27 ++--- rpcs3/Emu/IdManager.h | 36 +++++-- 4 files changed, 112 insertions(+), 104 deletions(-) diff --git a/rpcs3/Emu/Cell/PPUThread.h b/rpcs3/Emu/Cell/PPUThread.h index ff88544dd991..99b0dee40b01 100644 --- a/rpcs3/Emu/Cell/PPUThread.h +++ b/rpcs3/Emu/Cell/PPUThread.h @@ -120,10 +120,10 @@ class ppu_thread : public cpu_thread u32 prio = 0; // Thread priority (0..3071) const u32 stack_size; // Stack size const u32 stack_addr; // Stack address - bool is_joinable = true; - bool is_joining = false; + + atomic_t joiner{~0u}; // Joining thread (-1 if detached) - lf_fifo, 255> cmd_queue; // Command queue for asynchronous operations. + lf_fifo, 127> cmd_queue; // Command queue for asynchronous operations. void cmd_push(cmd64); void cmd_list(std::initializer_list); diff --git a/rpcs3/Emu/Cell/lv2/sys_ppu_thread.cpp b/rpcs3/Emu/Cell/lv2/sys_ppu_thread.cpp index 0cca39ae6dc2..989d3011e3a7 100644 --- a/rpcs3/Emu/Cell/lv2/sys_ppu_thread.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_ppu_thread.cpp @@ -17,14 +17,10 @@ void _sys_ppu_thread_exit(ppu_thread& ppu, u64 errorcode) { sys_ppu_thread.warning("_sys_ppu_thread_exit(errorcode=0x%llx)", errorcode); - // TODO: shall sys_mutex objects be unlocked? - - LV2_LOCK; - ppu.state += cpu_flag::exit; // Delete detached thread - if (!ppu.is_joinable) + if (ppu.joiner == -1) { idm::remove(ppu.id); } @@ -43,12 +39,10 @@ void sys_ppu_thread_yield() std::this_thread::yield(); } -s32 sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ptr vptr) +error_code sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ptr vptr) { sys_ppu_thread.warning("sys_ppu_thread_join(thread_id=0x%x, vptr=*0x%x)", thread_id, vptr); - LV2_LOCK; - const auto thread = idm::get(thread_id); if (!thread) @@ -56,7 +50,8 @@ s32 sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ptr vptr) return CELL_ESRCH; } - if (!thread->is_joinable || thread->is_joining) + // TODO: this is race condition if EDEADLK check doesn't actually take the precedence + if (thread->joiner) { return CELL_EINVAL; } @@ -66,32 +61,30 @@ s32 sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ptr vptr) return CELL_EDEADLK; } - // mark joining - thread->is_joining = true; - - // join thread - while (!test(thread->state & cpu_flag::exit)) + if (!thread->joiner.compare_and_swap_test(0, ppu.id)) { - CHECK_EMU_STATUS; - - LV2_UNLOCK, thread_ctrl::wait_for(1000); + return CELL_EINVAL; } - // get exit status from the register - if (vptr) *vptr = thread->gpr[3]; + // Actually join + thread->join(); + + // Get the exit status from the register + if (vptr) + { + *vptr = thread->gpr[3]; + } - // cleanup + // Cleanup idm::remove(thread->id); return CELL_OK; } -s32 sys_ppu_thread_detach(u32 thread_id) +error_code sys_ppu_thread_detach(u32 thread_id) { sys_ppu_thread.warning("sys_ppu_thread_detach(thread_id=0x%x)", thread_id); - LV2_LOCK; - const auto thread = idm::get(thread_id); if (!thread) @@ -99,37 +92,38 @@ s32 sys_ppu_thread_detach(u32 thread_id) return CELL_ESRCH; } - if (!thread->is_joinable) + u32 joiner = thread->joiner; + + if (!joiner) + { + // Detach thread by assigning -1 + joiner = thread->joiner.compare_and_swap(0, -1); + } + + if (joiner == -1) { return CELL_EINVAL; } - if (thread->is_joining) + if (joiner) { return CELL_EBUSY; } - // "detach" - thread->is_joinable = false; - return CELL_OK; } void sys_ppu_thread_get_join_state(ppu_thread& ppu, vm::ptr isjoinable) { - sys_ppu_thread.warning("sys_ppu_thread_get_join_state(isjoinable=*0x%x)", isjoinable); - - LV2_LOCK; + sys_ppu_thread.trace("sys_ppu_thread_get_join_state(isjoinable=*0x%x)", isjoinable); - *isjoinable = ppu.is_joinable; + *isjoinable = ppu.joiner != -1; } -s32 sys_ppu_thread_set_priority(u32 thread_id, s32 prio) +error_code sys_ppu_thread_set_priority(u32 thread_id, s32 prio) { sys_ppu_thread.trace("sys_ppu_thread_set_priority(thread_id=0x%x, prio=%d)", thread_id, prio); - LV2_LOCK; - const auto thread = idm::get(thread_id); if (!thread) @@ -147,12 +141,10 @@ s32 sys_ppu_thread_set_priority(u32 thread_id, s32 prio) return CELL_OK; } -s32 sys_ppu_thread_get_priority(u32 thread_id, vm::ptr priop) +error_code sys_ppu_thread_get_priority(u32 thread_id, vm::ptr priop) { sys_ppu_thread.trace("sys_ppu_thread_get_priority(thread_id=0x%x, priop=*0x%x)", thread_id, priop); - LV2_LOCK; - const auto thread = idm::get(thread_id); if (!thread) @@ -165,7 +157,7 @@ s32 sys_ppu_thread_get_priority(u32 thread_id, vm::ptr priop) return CELL_OK; } -s32 sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ptr sp) +error_code sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ptr sp) { sys_ppu_thread.trace("sys_ppu_thread_get_stack_information(sp=*0x%x)", sp); @@ -175,12 +167,10 @@ s32 sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ptr(thread_id); if (!thread) @@ -188,17 +178,13 @@ s32 sys_ppu_thread_stop(u32 thread_id) return CELL_ESRCH; } - //t->Stop(); - return CELL_OK; } -s32 sys_ppu_thread_restart(u32 thread_id) +error_code sys_ppu_thread_restart(u32 thread_id) { sys_ppu_thread.todo("sys_ppu_thread_restart(thread_id=0x%x)", thread_id); - LV2_LOCK; - const auto thread = idm::get(thread_id); if (!thread) @@ -206,19 +192,14 @@ s32 sys_ppu_thread_restart(u32 thread_id) return CELL_ESRCH; } - //t->Stop(); - //t->Run(); - return CELL_OK; } -s32 _sys_ppu_thread_create(vm::ptr thread_id, vm::ptr param, u64 arg, u64 unk, s32 prio, u32 stacksize, u64 flags, vm::cptr threadname) +error_code _sys_ppu_thread_create(vm::ptr thread_id, vm::ptr param, u64 arg, u64 unk, s32 prio, u32 stacksize, u64 flags, vm::cptr threadname) { sys_ppu_thread.warning("_sys_ppu_thread_create(thread_id=*0x%x, param=*0x%x, arg=0x%llx, unk=0x%llx, prio=%d, stacksize=0x%x, flags=0x%llx, threadname=%s)", thread_id, param, arg, unk, prio, stacksize, flags, threadname); - LV2_LOCK; - if (prio < 0 || prio > 3071) { return CELL_EINVAL; @@ -229,36 +210,47 @@ s32 _sys_ppu_thread_create(vm::ptr thread_id, vm::ptr p return CELL_EPERM; } - const auto ppu = idm::make_ptr(threadname ? threadname.get_ptr() : "", prio, stacksize); - - ppu->is_joinable = (flags & SYS_PPU_THREAD_CREATE_JOINABLE) != 0; - ppu->gpr[13] = param->tls.value(); - - if ((flags & SYS_PPU_THREAD_CREATE_INTERRUPT) == 0) + const u32 tid = idm::import([&]() { - // Initialize thread entry point - ppu->cmd_list - ({ - { ppu_cmd::set_args, 2 }, arg, unk, // Actually unknown - { ppu_cmd::lle_call, param->entry.value() }, - }); - } - else + auto ppu = std::make_shared(threadname ? threadname.get_ptr() : "", prio, stacksize); + + if ((flags & SYS_PPU_THREAD_CREATE_JOINABLE) != 0) + { + ppu->joiner = 0; + } + + ppu->gpr[13] = param->tls.value(); + + if ((flags & SYS_PPU_THREAD_CREATE_INTERRUPT) == 0) + { + // Initialize thread entry point + ppu->cmd_list + ({ + { ppu_cmd::set_args, 2 }, arg, unk, // Actually unknown + { ppu_cmd::lle_call, param->entry.value() }, + }); + } + else + { + // Save entry for further use (workaround) + ppu->gpr[2] = param->entry.value(); + } + + return ppu; + }); + + if (!tid) { - // Save entry for further use - ppu->gpr[2] = param->entry.value(); + return CELL_EAGAIN; } - *thread_id = ppu->id; - + *thread_id = tid; return CELL_OK; } -s32 sys_ppu_thread_start(u32 thread_id) +error_code sys_ppu_thread_start(u32 thread_id) { - sys_ppu_thread.warning("sys_ppu_thread_start(thread_id=0x%x)", thread_id); - - LV2_LOCK; + sys_ppu_thread.notice("sys_ppu_thread_start(thread_id=0x%x)", thread_id); const auto thread = idm::get(thread_id); @@ -267,17 +259,16 @@ s32 sys_ppu_thread_start(u32 thread_id) return CELL_ESRCH; } + // TODO thread->run(); return CELL_OK; } -s32 sys_ppu_thread_rename(u32 thread_id, vm::cptr name) +error_code sys_ppu_thread_rename(u32 thread_id, vm::cptr name) { sys_ppu_thread.todo("sys_ppu_thread_rename(thread_id=0x%x, name=%s)", thread_id, name); - LV2_LOCK; - const auto thread = idm::get(thread_id); if (!thread) diff --git a/rpcs3/Emu/Cell/lv2/sys_ppu_thread.h b/rpcs3/Emu/Cell/lv2/sys_ppu_thread.h index 512dd57a7a01..7191c4a13603 100644 --- a/rpcs3/Emu/Cell/lv2/sys_ppu_thread.h +++ b/rpcs3/Emu/Cell/lv2/sys_ppu_thread.h @@ -25,8 +25,8 @@ struct sys_ppu_thread_stack_t struct ppu_thread_param_t { - be_t entry; // vm::bptr - be_t tls; // vm::bptr + be_t entry; // vm::ps3::bptr + be_t tls; // vm::ps3::bptr }; enum : u32 @@ -41,17 +41,18 @@ enum : u32 PPU_THREAD_STATUS_UNKNOWN, }; -// SysCalls +// Syscalls + void _sys_ppu_thread_exit(ppu_thread& ppu, u64 errorcode); void sys_ppu_thread_yield(); -s32 sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ps3::ptr vptr); -s32 sys_ppu_thread_detach(u32 thread_id); +error_code sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ps3::ptr vptr); +error_code sys_ppu_thread_detach(u32 thread_id); void sys_ppu_thread_get_join_state(ppu_thread& ppu, vm::ps3::ptr isjoinable); -s32 sys_ppu_thread_set_priority(u32 thread_id, s32 prio); -s32 sys_ppu_thread_get_priority(u32 thread_id, vm::ps3::ptr priop); -s32 sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ps3::ptr sp); -s32 sys_ppu_thread_stop(u32 thread_id); -s32 sys_ppu_thread_restart(u32 thread_id); -s32 _sys_ppu_thread_create(vm::ps3::ptr thread_id, vm::ps3::ptr param, u64 arg, u64 arg4, s32 prio, u32 stacksize, u64 flags, vm::ps3::cptr threadname); -s32 sys_ppu_thread_start(u32 thread_id); -s32 sys_ppu_thread_rename(u32 thread_id, vm::ps3::cptr name); +error_code sys_ppu_thread_set_priority(u32 thread_id, s32 prio); +error_code sys_ppu_thread_get_priority(u32 thread_id, vm::ps3::ptr priop); +error_code sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ps3::ptr sp); +error_code sys_ppu_thread_stop(u32 thread_id); +error_code sys_ppu_thread_restart(u32 thread_id); +error_code _sys_ppu_thread_create(vm::ps3::ptr thread_id, vm::ps3::ptr param, u64 arg, u64 arg4, s32 prio, u32 stacksize, u64 flags, vm::ps3::cptr threadname); +error_code sys_ppu_thread_start(u32 thread_id); +error_code sys_ppu_thread_rename(u32 thread_id, vm::ps3::cptr name); diff --git a/rpcs3/Emu/IdManager.h b/rpcs3/Emu/IdManager.h index 046edd429b8f..fa92a4f6c829 100644 --- a/rpcs3/Emu/IdManager.h +++ b/rpcs3/Emu/IdManager.h @@ -305,7 +305,11 @@ class idm { // Get object, store it place->second = provider(); - return place; + + if (place->second) + { + return place; + } } return nullptr; @@ -365,15 +369,15 @@ class idm // Add a new ID for an object returned by provider() template > - static inline std::shared_ptr import(F&& provider) + static inline u32 import(F&& provider) { if (auto pair = create_id(std::forward(provider))) { id_manager::on_init::func(static_cast(pair->second.get()), pair->second); - return {pair->second, static_cast(pair->second.get())}; + return pair->first; } - return nullptr; + return id_manager::id_traits::invalid; } // Check the ID @@ -721,10 +725,14 @@ class fxm { ptr = provider(); - pair.first = id_manager::typeinfo::get_stop(); - pair.second = ptr; + if (ptr) + { + pair.first = id_manager::typeinfo::get_stop(); + pair.second = ptr; + } } - else + + if (!ptr) { return nullptr; } @@ -746,10 +754,18 @@ class fxm auto& pair = g_vec[get_type()]; ptr = provider(); - old = std::move(pair.second); - pair.first = id_manager::typeinfo::get_stop(); - pair.second = ptr; + if (ptr) + { + old = std::move(pair.second); + + pair.first = id_manager::typeinfo::get_stop(); + pair.second = ptr; + } + else + { + return nullptr; + } } if (old)