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

166 lines
3 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"
2016-05-13 15:55:34 +02:00
logs::channel sys_lwmutex("sys_lwmutex", logs::level::notice);
2014-08-23 16:51:51 +02:00
extern u64 get_system_time();
2016-04-14 00:23:53 +02:00
void lv2_lwmutex_t::unlock(lv2_lock_t)
{
if (signaled)
{
fmt::throw_exception("Unexpected" HERE);
}
if (sq.size())
{
2016-08-15 02:11:49 +02:00
const auto thread = sq.front();
thread->set_signal();
sq.pop_front();
}
else
{
signaled++;
}
}
2015-03-08 22:56:45 +01:00
s32 _sys_lwmutex_create(vm::ptr<u32> lwmutex_id, u32 protocol, vm::ptr<sys_lwmutex_t> 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);
2015-07-19 23:29:40 +02:00
if (protocol != SYS_SYNC_FIFO && protocol != SYS_SYNC_RETRY && protocol != SYS_SYNC_PRIORITY)
{
sys_lwmutex.error("_sys_lwmutex_create(): unknown protocol (0x%x)", protocol);
2015-07-19 23:29:40 +02:00
return CELL_EINVAL;
}
if (arg4 != 0x80000001 || arg6)
{
fmt::throw_exception("Unknown arguments (arg4=0x%x, arg6=0x%x)" HERE, arg4, arg6);
}
*lwmutex_id = idm::make<lv2_lwmutex_t>(protocol, name);
return CELL_OK;
}
s32 _sys_lwmutex_destroy(u32 lwmutex_id)
{
sys_lwmutex.warning("_sys_lwmutex_destroy(lwmutex_id=0x%x)", lwmutex_id);
LV2_LOCK;
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 (!mutex)
{
return CELL_ESRCH;
}
if (!mutex->sq.empty())
{
return CELL_EBUSY;
}
idm::remove<lv2_lwmutex_t>(lwmutex_id);
return CELL_OK;
}
s32 _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<lv2_lwmutex_t>(lwmutex_id);
2015-03-11 16:30:50 +01:00
2015-04-12 03:36:25 +02:00
if (!mutex)
{
return CELL_ESRCH;
}
if (mutex->signaled)
{
mutex->signaled--;
return CELL_OK;
}
// add waiter; protocol is ignored in current implementation
2016-04-14 00:23:53 +02:00
sleep_entry<cpu_thread> waiter(mutex->sq, ppu);
while (!ppu.state.test_and_reset(cpu_flag::signal))
{
2015-07-04 01:22:24 +02:00
CHECK_EMU_STATUS;
if (timeout)
{
const u64 passed = get_system_time() - start_time;
if (passed >= timeout)
{
return CELL_ETIMEDOUT;
}
get_current_thread_cv().wait_for(lv2_lock, std::chrono::microseconds(timeout - passed));
}
else
{
get_current_thread_cv().wait(lv2_lock);
}
}
return CELL_OK;
}
s32 _sys_lwmutex_trylock(u32 lwmutex_id)
{
sys_lwmutex.trace("_sys_lwmutex_trylock(lwmutex_id=0x%x)", lwmutex_id);
LV2_LOCK;
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 (!mutex)
{
return CELL_ESRCH;
}
if (!mutex->sq.empty() || !mutex->signaled)
{
return CELL_EBUSY;
}
2015-03-09 20:56:55 +01:00
mutex->signaled--;
return CELL_OK;
}
s32 _sys_lwmutex_unlock(u32 lwmutex_id)
{
sys_lwmutex.trace("_sys_lwmutex_unlock(lwmutex_id=0x%x)", lwmutex_id);
LV2_LOCK;
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 (!mutex)
{
return CELL_ESRCH;
}
mutex->unlock(lv2_lock);
return CELL_OK;
}