rpcsx/rpcs3/Emu/Cell/lv2/sys_lwcond.cpp

217 lines
4.9 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
2015-03-06 23:58:42 +01:00
#include "Emu/IdManager.h"
2014-08-23 16:51:51 +02:00
2016-04-14 00:23:53 +02:00
#include "Emu/Cell/ErrorCodes.h"
2014-08-23 16:51:51 +02:00
#include "Emu/Cell/PPUThread.h"
#include "sys_lwmutex.h"
#include "sys_lwcond.h"
2016-05-13 15:55:34 +02:00
#include <algorithm>
logs::channel sys_lwcond("sys_lwcond", logs::level::notice);
extern u64 get_system_time();
2016-04-14 00:23:53 +02:00
void lv2_lwcond_t::notify(lv2_lock_t, cpu_thread* thread, const std::shared_ptr<lv2_lwmutex_t>& mutex, bool mode2)
{
auto& ppu = static_cast<ppu_thread&>(*thread);
ppu.gpr[3] = mode2; // set to return CELL_EBUSY
if (!mode2)
{
if (!mutex->signaled)
{
2015-07-26 13:21:25 +02:00
return mutex->sq.emplace_back(thread);
}
mutex->signaled--;
}
2016-05-13 15:55:34 +02:00
VERIFY(!thread->state.test_and_set(cpu_state::signal));
thread->notify();
}
2015-03-09 20:56:55 +01:00
s32 _sys_lwcond_create(vm::ptr<u32> lwcond_id, u32 lwmutex_id, vm::ptr<sys_lwcond_t> 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<lv2_lwcond_t>(name);
return CELL_OK;
}
2015-03-09 20:56:55 +01:00
s32 _sys_lwcond_destroy(u32 lwcond_id)
{
sys_lwcond.warning("_sys_lwcond_destroy(lwcond_id=0x%x)", lwcond_id);
2015-03-09 20:56:55 +01:00
LV2_LOCK;
const auto cond = idm::get<lv2_lwcond_t>(lwcond_id);
2015-03-11 16:30:50 +01:00
2015-04-12 03:36:25 +02:00
if (!cond)
{
return CELL_ESRCH;
}
if (!cond->sq.empty())
{
2015-03-09 20:56:55 +01:00
return CELL_EBUSY;
}
idm::remove<lv2_lwcond_t>(lwcond_id);
2015-03-09 20:56:55 +01:00
return CELL_OK;
}
2015-03-09 20:56:55 +01:00
s32 _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);
2015-03-10 15:42:08 +01:00
LV2_LOCK;
const auto cond = idm::get<lv2_lwcond_t>(lwcond_id);
const auto mutex = idm::get<lv2_lwmutex_t>(lwmutex_id);
2015-03-11 16:30:50 +01:00
2015-04-12 03:36:25 +02:00
if (!cond || (lwmutex_id && !mutex))
2015-03-10 15:42:08 +01:00
{
return CELL_ESRCH;
}
if (mode != 1 && mode != 2 && mode != 3)
{
throw EXCEPTION("Unknown mode (%d)", mode);
2015-03-10 15:42:08 +01:00
}
// 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
2015-03-10 15:42:08 +01:00
// pick waiter; protocol is ignored in current implementation
2016-04-14 00:23:53 +02:00
const auto found = !~ppu_thread_id ? cond->sq.begin() : std::find_if(cond->sq.begin(), cond->sq.end(), [=](cpu_thread* thread)
2015-03-10 15:42:08 +01:00
{
2016-04-14 00:23:53 +02:00
return thread->id == ppu_thread_id;
});
2015-03-10 15:42:08 +01:00
if (found == cond->sq.end())
{
if (mode == 1)
2015-03-10 15:42:08 +01:00
{
return CELL_EPERM;
}
else if (mode == 2)
2015-03-10 15:42:08 +01:00
{
return CELL_OK;
}
else
2015-03-10 15:42:08 +01:00
{
return ~ppu_thread_id ? CELL_ENOENT : CELL_EPERM;
}
}
// signal specified waiting thread
2015-07-26 13:21:25 +02:00
cond->notify(lv2_lock, *found, mutex, mode == 2);
cond->sq.erase(found);
return CELL_OK;
}
2015-03-09 20:56:55 +01:00
s32 _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);
2015-03-10 15:42:08 +01:00
LV2_LOCK;
const auto cond = idm::get<lv2_lwcond_t>(lwcond_id);
const auto mutex = idm::get<lv2_lwmutex_t>(lwmutex_id);
2015-03-11 16:30:50 +01:00
2015-04-12 03:36:25 +02:00
if (!cond || (lwmutex_id && !mutex))
2015-03-10 22:47:13 +01:00
{
return CELL_ESRCH;
}
2015-03-10 15:42:08 +01:00
if (mode != 1 && mode != 2)
{
throw EXCEPTION("Unknown mode (%d)", mode);
2015-03-10 15:42:08 +01:00
}
// 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
2015-03-11 16:30:50 +01:00
// signal all waiting threads; protocol is ignored in current implementation
2015-07-26 13:21:25 +02:00
for (auto& thread : cond->sq)
2015-03-11 16:30:50 +01:00
{
2015-07-26 13:21:25 +02:00
cond->notify(lv2_lock, thread, mutex, mode == 2);
2015-03-11 16:30:50 +01:00
}
2015-03-10 15:42:08 +01:00
// in mode 1, return the amount of threads signaled
const s32 result = mode == 2 ? CELL_OK : static_cast<s32>(cond->sq.size());
2015-03-10 15:42:08 +01:00
cond->sq.clear();
2015-03-10 15:42:08 +01:00
return result;
}
s32 _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);
2014-03-22 22:04:55 +01:00
2015-03-10 15:42:08 +01:00
const u64 start_time = get_system_time();
LV2_LOCK;
const auto cond = idm::get<lv2_lwcond_t>(lwcond_id);
const auto mutex = idm::get<lv2_lwmutex_t>(lwmutex_id);
2015-03-11 16:30:50 +01:00
2015-04-12 03:36:25 +02:00
if (!cond || !mutex)
2015-03-10 22:47:13 +01:00
{
return CELL_ESRCH;
}
// finalize unlocking the mutex
mutex->unlock(lv2_lock);
2015-03-10 22:47:13 +01:00
2015-04-12 03:36:25 +02:00
// add waiter; protocol is ignored in current implementation
2016-04-14 00:23:53 +02:00
sleep_entry<cpu_thread> waiter(cond->sq, ppu);
2015-03-10 15:42:08 +01:00
// potential mutex waiter (not added immediately)
2016-04-14 00:23:53 +02:00
sleep_entry<cpu_thread> mutex_waiter(cond->sq, ppu, defer_sleep);
2016-04-14 00:23:53 +02:00
while (!ppu.state.test_and_reset(cpu_state::signal))
2015-03-10 15:42:08 +01:00
{
2015-07-04 01:22:24 +02:00
CHECK_EMU_STATUS;
if (timeout && waiter)
2015-03-10 15:42:08 +01:00
{
const u64 passed = get_system_time() - start_time;
if (passed >= timeout)
2015-04-12 03:36:25 +02:00
{
// try to reown the mutex if timed out
if (mutex->signaled)
2015-04-12 03:36:25 +02:00
{
mutex->signaled--;
return CELL_EDEADLK;
2015-04-12 03:36:25 +02:00
}
else
{
return CELL_ETIMEDOUT;
2015-04-12 03:36:25 +02:00
}
}
2015-03-10 15:42:08 +01:00
get_current_thread_cv().wait_for(lv2_lock, std::chrono::microseconds(timeout - passed));
}
else
{
get_current_thread_cv().wait(lv2_lock);
2015-03-10 15:42:08 +01:00
}
}
// return cause
return ppu.gpr[3] ? CELL_EBUSY : CELL_OK;
}