From 3d85a89cc9fa8bf81633061f1239e8ac15cbe3be Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 3 Feb 2017 02:16:09 +0300 Subject: [PATCH] sys_lwcond, sys_lwmutex --- rpcs3/Emu/Cell/Modules/sys_lwcond_.cpp | 21 +- rpcs3/Emu/Cell/Modules/sys_lwmutex_.cpp | 18 +- rpcs3/Emu/Cell/lv2/sys_event_flag.h | 12 +- rpcs3/Emu/Cell/lv2/sys_lwcond.cpp | 308 +++++++++++++++--------- rpcs3/Emu/Cell/lv2/sys_lwcond.h | 34 +-- rpcs3/Emu/Cell/lv2/sys_lwmutex.cpp | 140 ++++++----- rpcs3/Emu/Cell/lv2/sys_lwmutex.h | 33 +-- rpcs3/Emu/System.cpp | 9 + rpcs3/Gui/KernelExplorer.cpp | 4 +- 9 files changed, 366 insertions(+), 213 deletions(-) diff --git a/rpcs3/Emu/Cell/Modules/sys_lwcond_.cpp b/rpcs3/Emu/Cell/Modules/sys_lwcond_.cpp index 1097ef3176..6efa2af1f2 100644 --- a/rpcs3/Emu/Cell/Modules/sys_lwcond_.cpp +++ b/rpcs3/Emu/Cell/Modules/sys_lwcond_.cpp @@ -11,11 +11,17 @@ extern logs::channel sysPrxForUser; s32 sys_lwcond_create(vm::ptr lwcond, vm::ptr lwmutex, vm::ptr attr) { - sysPrxForUser.warning("sys_lwcond_create(lwcond=*0x%x, lwmutex=*0x%x, attr=*0x%x)", lwcond, lwmutex, attr); + sysPrxForUser.trace("sys_lwcond_create(lwcond=*0x%x, lwmutex=*0x%x, attr=*0x%x)", lwcond, lwmutex, attr); - lwcond->lwcond_queue = idm::make(reinterpret_cast(attr->name)); - lwcond->lwmutex = lwmutex; + vm::var out_id; + if (s32 res = _sys_lwcond_create(out_id, lwmutex->sleep_queue, lwcond, attr->name_u64, 0)) + { + return res; + } + + lwcond->lwmutex = lwmutex; + lwcond->lwcond_queue = *out_id; return CELL_OK; } @@ -23,14 +29,13 @@ s32 sys_lwcond_destroy(vm::ptr lwcond) { sysPrxForUser.trace("sys_lwcond_destroy(lwcond=*0x%x)", lwcond); - const s32 res = _sys_lwcond_destroy(lwcond->lwcond_queue); - - if (res == CELL_OK) + if (s32 res = _sys_lwcond_destroy(lwcond->lwcond_queue)) { - lwcond->lwcond_queue = lwmutex_dead; + return res; } - return res; + lwcond->lwcond_queue = lwmutex_dead; + return CELL_OK; } s32 sys_lwcond_signal(ppu_thread& ppu, vm::ptr lwcond) diff --git a/rpcs3/Emu/Cell/Modules/sys_lwmutex_.cpp b/rpcs3/Emu/Cell/Modules/sys_lwmutex_.cpp index 77ca387c6d..bb5fc699d2 100644 --- a/rpcs3/Emu/Cell/Modules/sys_lwmutex_.cpp +++ b/rpcs3/Emu/Cell/Modules/sys_lwmutex_.cpp @@ -11,13 +11,13 @@ extern logs::channel sysPrxForUser; s32 sys_lwmutex_create(vm::ptr lwmutex, vm::ptr attr) { - sysPrxForUser.warning("sys_lwmutex_create(lwmutex=*0x%x, attr=*0x%x)", lwmutex, attr); + sysPrxForUser.trace("sys_lwmutex_create(lwmutex=*0x%x, attr=*0x%x)", lwmutex, attr); - const bool recursive = attr->recursive == SYS_SYNC_RECURSIVE; + const u32 recursive = attr->recursive; - if (!recursive && attr->recursive != SYS_SYNC_NOT_RECURSIVE) + if (recursive != SYS_SYNC_RECURSIVE && recursive != SYS_SYNC_NOT_RECURSIVE) { - sysPrxForUser.error("sys_lwmutex_create(): invalid recursive attribute (0x%x)", attr->recursive); + sysPrxForUser.error("sys_lwmutex_create(): invalid recursive attribute (0x%x)", recursive); return CELL_EINVAL; } @@ -31,11 +31,17 @@ s32 sys_lwmutex_create(vm::ptr lwmutex, vm::ptr out_id; + + if (s32 res = _sys_lwmutex_create(out_id, protocol, lwmutex, 0x80000001, attr->name_u64, 0)) + { + return res; + } + lwmutex->lock_var.store({ lwmutex_free, 0 }); lwmutex->attribute = attr->recursive | attr->protocol; lwmutex->recursive_count = 0; - lwmutex->sleep_queue = idm::make(protocol, reinterpret_cast(attr->name)); - + lwmutex->sleep_queue = *out_id; return CELL_OK; } diff --git a/rpcs3/Emu/Cell/lv2/sys_event_flag.h b/rpcs3/Emu/Cell/lv2/sys_event_flag.h index 84ed53513c..a8bdcaa4a6 100644 --- a/rpcs3/Emu/Cell/lv2/sys_event_flag.h +++ b/rpcs3/Emu/Cell/lv2/sys_event_flag.h @@ -34,6 +34,9 @@ struct lv2_event_flag final : lv2_obj static const u32 id_base = 0x98000000; const u32 protocol; + const u32 shared; + const u64 key; + const s32 flags; const s32 type; const u64 name; @@ -41,11 +44,14 @@ struct lv2_event_flag final : lv2_obj sleep_queue sq; - lv2_event_flag(u64 pattern, u32 protocol, s32 type, u64 name) - : pattern(pattern) - , protocol(protocol) + lv2_event_flag(u32 protocol, s32 type, u64 name, u64 pattern) + : protocol(protocol) + , shared(0) + , key(0) + , flags(0) , type(type) , name(name) + , pattern(pattern) { } diff --git a/rpcs3/Emu/Cell/lv2/sys_lwcond.cpp b/rpcs3/Emu/Cell/lv2/sys_lwcond.cpp index 9b2761c504..1e932f345c 100644 --- a/rpcs3/Emu/Cell/lv2/sys_lwcond.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_lwcond.cpp @@ -8,208 +8,294 @@ #include "sys_lwmutex.h" #include "sys_lwcond.h" -#include +namespace vm { using namespace ps3; } logs::channel sys_lwcond("sys_lwcond", logs::level::notice); extern u64 get_system_time(); -void lv2_lwcond::notify(lv2_lock_t, cpu_thread* thread, const std::shared_ptr& mutex, bool mode2) -{ - auto& ppu = static_cast(*thread); - - ppu.gpr[3] = mode2; // set to return CELL_EBUSY - - if (!mode2) - { - if (!mutex->signaled) - { - return mutex->sq.emplace_back(thread); - } - - mutex->signaled--; - } - - thread->set_signal(); -} - -s32 _sys_lwcond_create(vm::ptr lwcond_id, u32 lwmutex_id, vm::ptr control, u64 name, u32 arg5) +error_code _sys_lwcond_create(vm::ptr lwcond_id, u32 lwmutex_id, vm::ptr control, u64 name, u32 arg5) { sys_lwcond.warning("_sys_lwcond_create(lwcond_id=*0x%x, lwmutex_id=0x%x, control=*0x%x, name=0x%llx, arg5=0x%x)", lwcond_id, lwmutex_id, control, name, arg5); - *lwcond_id = idm::make(name); + // Temporarily + if (!idm::check(lwmutex_id)) + { + return CELL_ESRCH; + } - return CELL_OK; + if (const u32 id = idm::make(name, lwmutex_id)) + { + *lwcond_id = id; + return CELL_OK; + } + + return CELL_EAGAIN; } -s32 _sys_lwcond_destroy(u32 lwcond_id) +error_code _sys_lwcond_destroy(u32 lwcond_id) { sys_lwcond.warning("_sys_lwcond_destroy(lwcond_id=0x%x)", lwcond_id); - LV2_LOCK; + const auto cond = idm::withdraw(lwcond_id, [&](lv2_lwcond& cond) -> CellError + { + if (cond.waiters) + { + return CELL_EBUSY; + } - const auto cond = idm::get(lwcond_id); + return {}; + }); if (!cond) { return CELL_ESRCH; } - if (!cond->sq.empty()) + if (cond.ret) { - return CELL_EBUSY; + return cond.ret; } - idm::remove(lwcond_id); - return CELL_OK; } -s32 _sys_lwcond_signal(u32 lwcond_id, u32 lwmutex_id, u32 ppu_thread_id, u32 mode) +error_code _sys_lwcond_signal(u32 lwcond_id, u32 lwmutex_id, u32 ppu_thread_id, u32 mode) { sys_lwcond.trace("_sys_lwcond_signal(lwcond_id=0x%x, lwmutex_id=0x%x, ppu_thread_id=0x%x, mode=%d)", lwcond_id, lwmutex_id, ppu_thread_id, mode); - LV2_LOCK; + // Mode 1: lwmutex was initially owned by the calling thread + // Mode 2: lwmutex was not owned by the calling thread and waiter hasn't been increased + // Mode 3: lwmutex was forcefully owned by the calling thread - const auto cond = idm::get(lwcond_id); - const auto mutex = idm::get(lwmutex_id); - - if (!cond || (lwmutex_id && !mutex)) - { - return CELL_ESRCH; - } - - if (mode != 1 && mode != 2 && mode != 3) + if (mode < 1 || mode > 3) { fmt::throw_exception("Unknown mode (%d)" HERE, mode); } - // mode 1: lightweight mutex was initially owned by the calling thread - // mode 2: lightweight mutex was not owned by the calling thread and waiter hasn't been increased - // mode 3: lightweight mutex was forcefully owned by the calling thread + lv2_lwmutex* mutex = nullptr; - // pick waiter; protocol is ignored in current implementation - const auto found = !~ppu_thread_id ? cond->sq.begin() : std::find_if(cond->sq.begin(), cond->sq.end(), [=](cpu_thread* thread) + const auto cond = idm::check(lwcond_id, [&](lv2_lwcond& cond) -> cpu_thread* { - return thread->id == ppu_thread_id; + mutex = idm::check_unlocked(lwmutex_id); + + if (mutex && cond.waiters) + { + semaphore_lock lock(mutex->mutex); + + cpu_thread* result = nullptr; + + if (ppu_thread_id != -1) + { + for (auto cpu : cond.sq) + { + if (cpu->id == ppu_thread_id) + { + verify(HERE), cond.unqueue(cond.sq, cpu); + result = cpu; + break; + } + } + } + else + { + result = cond.schedule(cond.sq, mutex->protocol); + } + + if (result) + { + cond.waiters--; + + static_cast(result)->gpr[3] = mode == 2; + + if (mode != 2 && !mutex->signaled.fetch_op([](u32& v) { if (v) v--; })) + { + mutex->sq.emplace_back(result); + result = nullptr; + mode = 2; // Enforce CELL_OK + } + + return result; + } + } + + return nullptr; }); - - if (found == cond->sq.end()) + + if ((lwmutex_id && !mutex) || !cond) { - if (mode == 1) - { - return CELL_EPERM; - } - else if (mode == 2) - { - return CELL_OK; - } - else - { - return ~ppu_thread_id ? CELL_ENOENT : CELL_EPERM; - } + return CELL_ESRCH; } - // signal specified waiting thread - cond->notify(lv2_lock, *found, mutex, mode == 2); - - cond->sq.erase(found); + if (cond.ret) + { + cond.ret->set_signal(); + } + else if (mode == 2) + { + return CELL_OK; + } + else if (mode == 1 || ppu_thread_id == -1) + { + return not_an_error(CELL_EPERM); + } + else + { + return not_an_error(CELL_ENOENT); + } return CELL_OK; } -s32 _sys_lwcond_signal_all(u32 lwcond_id, u32 lwmutex_id, u32 mode) +error_code _sys_lwcond_signal_all(u32 lwcond_id, u32 lwmutex_id, u32 mode) { sys_lwcond.trace("_sys_lwcond_signal_all(lwcond_id=0x%x, lwmutex_id=0x%x, mode=%d)", lwcond_id, lwmutex_id, mode); - LV2_LOCK; + // Mode 1: lwmutex was initially owned by the calling thread + // Mode 2: lwmutex was not owned by the calling thread and waiter hasn't been increased - const auto cond = idm::get(lwcond_id); - const auto mutex = idm::get(lwmutex_id); - - if (!cond || (lwmutex_id && !mutex)) - { - return CELL_ESRCH; - } - - if (mode != 1 && mode != 2) + if (mode < 1 || mode > 2) { fmt::throw_exception("Unknown mode (%d)" HERE, mode); } - // mode 1: lightweight mutex was initially owned by the calling thread - // mode 2: lightweight mutex was not owned by the calling thread and waiter hasn't been increased + std::basic_string threads; - // signal all waiting threads; protocol is ignored in current implementation - for (auto& thread : cond->sq) + lv2_lwmutex* mutex = nullptr; + + const auto cond = idm::check(lwcond_id, [&](lv2_lwcond& cond) -> u32 { - cond->notify(lv2_lock, thread, mutex, mode == 2); + mutex = idm::check_unlocked(lwmutex_id); + + if (mutex && cond.waiters) + { + semaphore_lock lock(mutex->mutex); + + u32 result = 0; + + while (const auto cpu = cond.schedule(cond.sq, mutex->protocol)) + { + cond.waiters--; + + static_cast(cpu)->gpr[3] = mode == 2; + + if (mode != 2 && !mutex->signaled.fetch_op([](u32& v) { if (v) v--; })) + { + mutex->sq.emplace_back(cpu); + } + else + { + threads.push_back(cpu); + } + + result++; + } + + return result; + } + + return 0; + }); + + if ((lwmutex_id && !mutex) || !cond) + { + return CELL_ESRCH; } - // in mode 1, return the amount of threads signaled - const s32 result = mode == 2 ? CELL_OK : static_cast(cond->sq.size()); + // TODO: signal only one thread + for (auto cpu : threads) + { + cpu->set_signal(); + } - cond->sq.clear(); + if (mode == 1) + { + // Mode 1: return the amount of threads (TODO) + return not_an_error(cond.ret); + } - return result; + return CELL_OK; } -s32 _sys_lwcond_queue_wait(ppu_thread& ppu, u32 lwcond_id, u32 lwmutex_id, u64 timeout) +error_code _sys_lwcond_queue_wait(ppu_thread& ppu, u32 lwcond_id, u32 lwmutex_id, u64 timeout) { sys_lwcond.trace("_sys_lwcond_queue_wait(lwcond_id=0x%x, lwmutex_id=0x%x, timeout=0x%llx)", lwcond_id, lwmutex_id, timeout); const u64 start_time = get_system_time(); - LV2_LOCK; + std::shared_ptr mutex; - const auto cond = idm::get(lwcond_id); - const auto mutex = idm::get(lwmutex_id); + const auto cond = idm::get(lwcond_id, [&](lv2_lwcond& cond) -> cpu_thread* + { + mutex = idm::get_unlocked(lwmutex_id); + + if (!mutex) + { + return nullptr; + } + + semaphore_lock lock(mutex->mutex); + + // Add a waiter + cond.waiters++; + cond.sq.emplace_back(&ppu); + + // Process lwmutex sleep queue + if (const auto cpu = mutex->schedule(mutex->sq, mutex->protocol)) + { + return cpu; + } + + mutex->signaled++; + return nullptr; + }); if (!cond || !mutex) { return CELL_ESRCH; } - // finalize unlocking the mutex - mutex->unlock(lv2_lock); + if (cond.ret) + { + cond.ret->set_signal(); + } - // add waiter; protocol is ignored in current implementation - sleep_entry waiter(cond->sq, ppu); - - // potential mutex waiter (not added immediately) - sleep_entry mutex_waiter(cond->sq, ppu, defer_sleep); + // SLEEP while (!ppu.state.test_and_reset(cpu_flag::signal)) { - CHECK_EMU_STATUS; - - if (timeout && waiter) + if (timeout) { const u64 passed = get_system_time() - start_time; if (passed >= timeout) { - // try to reown the mutex if timed out - if (mutex->signaled) - { - mutex->signaled--; + semaphore_lock lock(mutex->mutex); - return CELL_EDEADLK; - } - else + if (!cond->unqueue(cond->sq, &ppu)) { - return CELL_ETIMEDOUT; + timeout = 0; + continue; } + + cond->waiters--; + + if (mutex->signaled.fetch_op([](u32& v) { if (v) v--; })) + { + return not_an_error(CELL_EDEADLK); + } + + return not_an_error(CELL_ETIMEDOUT); } - LV2_UNLOCK, thread_ctrl::wait_for(timeout - passed); + thread_ctrl::wait_for(timeout - passed); } else { - LV2_UNLOCK, thread_ctrl::wait(); + thread_ctrl::wait(); } } - // return cause - return ppu.gpr[3] ? CELL_EBUSY : CELL_OK; + // Return cause + return not_an_error(ppu.gpr[3] ? CELL_EBUSY : CELL_OK); } diff --git a/rpcs3/Emu/Cell/lv2/sys_lwcond.h b/rpcs3/Emu/Cell/lv2/sys_lwcond.h index 19a1ebf5fe..7379adf3b6 100644 --- a/rpcs3/Emu/Cell/lv2/sys_lwcond.h +++ b/rpcs3/Emu/Cell/lv2/sys_lwcond.h @@ -1,19 +1,21 @@ #pragma once -#include "Utilities/SleepQueue.h" - -namespace vm { using namespace ps3; } +#include "sys_sync.h" struct sys_lwmutex_t; struct sys_lwcond_attribute_t { - char name[8]; + union + { + char name[8]; + u64 name_u64; + }; }; struct sys_lwcond_t { - vm::bptr lwmutex; + vm::ps3::bptr lwmutex; be_t lwcond_queue; // lwcond pseudo-id }; @@ -22,23 +24,25 @@ struct lv2_lwcond final : lv2_obj static const u32 id_base = 0x97000000; const u64 name; + const u32 lwid; - sleep_queue sq; + atomic_t waiters{0}; + std::deque sq; - lv2_lwcond(u64 name) + lv2_lwcond(u64 name, u32 lwid) : name(name) + , lwid(lwid) { } - - void notify(lv2_lock_t, cpu_thread* thread, const std::shared_ptr& mutex, bool mode2); }; // Aux class ppu_thread; -// SysCalls -s32 _sys_lwcond_create(vm::ptr lwcond_id, u32 lwmutex_id, vm::ptr control, u64 name, u32 arg5); -s32 _sys_lwcond_destroy(u32 lwcond_id); -s32 _sys_lwcond_signal(u32 lwcond_id, u32 lwmutex_id, u32 ppu_thread_id, u32 mode); -s32 _sys_lwcond_signal_all(u32 lwcond_id, u32 lwmutex_id, u32 mode); -s32 _sys_lwcond_queue_wait(ppu_thread& ppu, u32 lwcond_id, u32 lwmutex_id, u64 timeout); +// Syscalls + +error_code _sys_lwcond_create(vm::ps3::ptr lwcond_id, u32 lwmutex_id, vm::ps3::ptr control, u64 name, u32 arg5); +error_code _sys_lwcond_destroy(u32 lwcond_id); +error_code _sys_lwcond_signal(u32 lwcond_id, u32 lwmutex_id, u32 ppu_thread_id, u32 mode); +error_code _sys_lwcond_signal_all(u32 lwcond_id, u32 lwmutex_id, u32 mode); +error_code _sys_lwcond_queue_wait(ppu_thread& ppu, u32 lwcond_id, u32 lwmutex_id, u64 timeout); diff --git a/rpcs3/Emu/Cell/lv2/sys_lwmutex.cpp b/rpcs3/Emu/Cell/lv2/sys_lwmutex.cpp index 7f8cde2ef1..b569386b7a 100644 --- a/rpcs3/Emu/Cell/lv2/sys_lwmutex.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_lwmutex.cpp @@ -13,27 +13,7 @@ logs::channel sys_lwmutex("sys_lwmutex", logs::level::notice); extern u64 get_system_time(); -void lv2_lwmutex::unlock(lv2_lock_t) -{ - if (signaled) - { - fmt::throw_exception("Unexpected" HERE); - } - - if (sq.size()) - { - const auto thread = sq.front(); - thread->set_signal(); - - sq.pop_front(); - } - else - { - signaled++; - } -} - -s32 _sys_lwmutex_create(vm::ptr lwmutex_id, u32 protocol, vm::ptr control, u32 arg4, u64 name, u32 arg6) +error_code _sys_lwmutex_create(vm::ptr lwmutex_id, u32 protocol, vm::ptr control, u32 arg4, u64 name, u32 arg6) { sys_lwmutex.warning("_sys_lwmutex_create(lwmutex_id=*0x%x, protocol=0x%x, control=*0x%x, arg4=0x%x, name=0x%llx, arg6=0x%x)", lwmutex_id, protocol, control, arg4, name, arg6); @@ -48,120 +28,172 @@ s32 _sys_lwmutex_create(vm::ptr lwmutex_id, u32 protocol, vm::ptr(protocol, name); + if (const u32 id = idm::make(protocol, control, name)) + { + *lwmutex_id = id; + return CELL_OK; + } - return CELL_OK; + return CELL_EAGAIN; } -s32 _sys_lwmutex_destroy(u32 lwmutex_id) +error_code _sys_lwmutex_destroy(u32 lwmutex_id) { sys_lwmutex.warning("_sys_lwmutex_destroy(lwmutex_id=0x%x)", lwmutex_id); - LV2_LOCK; + const auto mutex = idm::withdraw(lwmutex_id, [&](lv2_lwmutex& mutex) -> CellError + { + semaphore_lock lock(mutex.mutex); - const auto mutex = idm::get(lwmutex_id); + if (!mutex.sq.empty()) + { + return CELL_EBUSY; + } + + return {}; + }); if (!mutex) { return CELL_ESRCH; } - if (!mutex->sq.empty()) + if (mutex.ret) { - return CELL_EBUSY; + return mutex.ret; } - idm::remove(lwmutex_id); - return CELL_OK; } -s32 _sys_lwmutex_lock(ppu_thread& ppu, u32 lwmutex_id, u64 timeout) +error_code _sys_lwmutex_lock(ppu_thread& ppu, u32 lwmutex_id, u64 timeout) { sys_lwmutex.trace("_sys_lwmutex_lock(lwmutex_id=0x%x, timeout=0x%llx)", lwmutex_id, timeout); const u64 start_time = get_system_time(); - LV2_LOCK; + const auto mutex = idm::get(lwmutex_id, [&](lv2_lwmutex& mutex) + { + if (u32 value = mutex.signaled) + { + if (mutex.signaled.compare_and_swap_test(value, value - 1)) + { + return true; + } + } - const auto mutex = idm::get(lwmutex_id); + semaphore_lock lock(mutex.mutex); + + if (u32 value = mutex.signaled) + { + if (mutex.signaled.compare_and_swap_test(value, value - 1)) + { + return true; + } + } + + mutex.sq.emplace_back(&ppu); + return false; + }); if (!mutex) { return CELL_ESRCH; } - if (mutex->signaled) + if (mutex.ret) { - mutex->signaled--; - return CELL_OK; } - // add waiter; protocol is ignored in current implementation - sleep_entry waiter(mutex->sq, ppu); + // SLEEP while (!ppu.state.test_and_reset(cpu_flag::signal)) { - CHECK_EMU_STATUS; - if (timeout) { const u64 passed = get_system_time() - start_time; if (passed >= timeout) { - return CELL_ETIMEDOUT; + semaphore_lock lock(mutex->mutex); + + if (!mutex->unqueue(mutex->sq, &ppu)) + { + timeout = 0; + continue; + } + + return not_an_error(CELL_ETIMEDOUT); } - LV2_UNLOCK, thread_ctrl::wait_for(timeout - passed); + thread_ctrl::wait_for(timeout - passed); } else { - LV2_UNLOCK, thread_ctrl::wait(); + thread_ctrl::wait(); } } return CELL_OK; } -s32 _sys_lwmutex_trylock(u32 lwmutex_id) +error_code _sys_lwmutex_trylock(u32 lwmutex_id) { sys_lwmutex.trace("_sys_lwmutex_trylock(lwmutex_id=0x%x)", lwmutex_id); - LV2_LOCK; + const auto mutex = idm::check(lwmutex_id, [&](lv2_lwmutex& mutex) + { + if (u32 value = mutex.signaled) + { + if (mutex.signaled.compare_and_swap_test(value, value - 1)) + { + return true; + } + } - const auto mutex = idm::get(lwmutex_id); + return false; + }); if (!mutex) { return CELL_ESRCH; } - if (!mutex->sq.empty() || !mutex->signaled) + if (!mutex.ret) { - return CELL_EBUSY; + return not_an_error(CELL_EBUSY); } - mutex->signaled--; - return CELL_OK; } -s32 _sys_lwmutex_unlock(u32 lwmutex_id) +error_code _sys_lwmutex_unlock(u32 lwmutex_id) { sys_lwmutex.trace("_sys_lwmutex_unlock(lwmutex_id=0x%x)", lwmutex_id); - LV2_LOCK; + const auto mutex = idm::check(lwmutex_id, [&](lv2_lwmutex& mutex) -> cpu_thread* + { + semaphore_lock lock(mutex.mutex); - const auto mutex = idm::get(lwmutex_id); + if (const auto cpu = mutex.schedule(mutex.sq, mutex.protocol)) + { + return cpu; + } + + mutex.signaled++; + return nullptr; + }); if (!mutex) { return CELL_ESRCH; } - mutex->unlock(lv2_lock); + if (mutex.ret) + { + mutex.ret->set_signal(); + } return CELL_OK; } diff --git a/rpcs3/Emu/Cell/lv2/sys_lwmutex.h b/rpcs3/Emu/Cell/lv2/sys_lwmutex.h index 86d22657c2..f4a00092e9 100644 --- a/rpcs3/Emu/Cell/lv2/sys_lwmutex.h +++ b/rpcs3/Emu/Cell/lv2/sys_lwmutex.h @@ -6,7 +6,12 @@ struct sys_lwmutex_attribute_t { be_t protocol; be_t recursive; - char name[8]; + + union + { + char name[8]; + u64 name_u64; + }; }; enum : u32 @@ -49,28 +54,28 @@ struct lv2_lwmutex final : lv2_obj static const u32 id_base = 0x95000000; const u32 protocol; + const vm::ps3::ptr control; const u64 name; - // this object is not truly a mutex and its syscall names may be wrong, it's probably a sleep queue or something - atomic_t signaled{ 0 }; + semaphore<> mutex; + atomic_t signaled{0}; + std::deque sq; - sleep_queue sq; - - lv2_lwmutex(u32 protocol, u64 name) + lv2_lwmutex(u32 protocol, vm::ps3::ptr control, u64 name) : protocol(protocol) + , control(control) , name(name) { } - - void unlock(lv2_lock_t); }; // Aux class ppu_thread; -// SysCalls -s32 _sys_lwmutex_create(vm::ps3::ptr lwmutex_id, u32 protocol, vm::ps3::ptr control, u32 arg4, u64 name, u32 arg6); -s32 _sys_lwmutex_destroy(u32 lwmutex_id); -s32 _sys_lwmutex_lock(ppu_thread& ppu, u32 lwmutex_id, u64 timeout); -s32 _sys_lwmutex_trylock(u32 lwmutex_id); -s32 _sys_lwmutex_unlock(u32 lwmutex_id); +// Syscalls + +error_code _sys_lwmutex_create(vm::ps3::ptr lwmutex_id, u32 protocol, vm::ps3::ptr control, u32 arg4, u64 name, u32 arg6); +error_code _sys_lwmutex_destroy(u32 lwmutex_id); +error_code _sys_lwmutex_lock(ppu_thread& ppu, u32 lwmutex_id, u64 timeout); +error_code _sys_lwmutex_trylock(u32 lwmutex_id); +error_code _sys_lwmutex_unlock(u32 lwmutex_id); diff --git a/rpcs3/Emu/System.cpp b/rpcs3/Emu/System.cpp index c956ddcd35..85f467a24f 100644 --- a/rpcs3/Emu/System.cpp +++ b/rpcs3/Emu/System.cpp @@ -520,6 +520,15 @@ s32 error_code::error_report(const fmt_type_info* sup, u64 arg) break; } + case CELL_ESRCH: + { + if (ppu.m_name == "_sys_MixerChStripMain" && std::memcmp(ppu.last_function, "sys_lwmutex_lock", 17) == 0) + { + level = logs::level::trace; + } + + break; + } } if (ppu.last_function) diff --git a/rpcs3/Gui/KernelExplorer.cpp b/rpcs3/Gui/KernelExplorer.cpp index 7d76cd02f9..f054f8f021 100644 --- a/rpcs3/Gui/KernelExplorer.cpp +++ b/rpcs3/Gui/KernelExplorer.cpp @@ -204,7 +204,7 @@ void KernelExplorer::Update() case SYS_LWMUTEX_OBJECT: { auto& lwm = static_cast(obj); - m_tree->AppendItem(node, fmt::format("LWMutex: ID = 0x%08x \"%s\"", id, +name64(lwm.name))); + m_tree->AppendItem(node, fmt::format("LWMutex: ID = 0x%08x \"%s\", Wq = %zu", id, +name64(lwm.name), lwm.sq.size())); break; } case SYS_TIMER_OBJECT: @@ -223,7 +223,7 @@ void KernelExplorer::Update() case SYS_LWCOND_OBJECT: { auto& lwc = static_cast(obj); - m_tree->AppendItem(node, fmt::format("LWCond: ID = 0x%08x \"%s\"", id, +name64(lwc.name))); + m_tree->AppendItem(node, fmt::format("LWCond: ID = 0x%08x \"%s\", Waiters = %zu", id, +name64(lwc.name), +lwc.waiters)); break; } case SYS_EVENT_FLAG_OBJECT: