rpcsx/rpcs3/Emu/SysCalls/lv2/sys_rwlock.cpp

262 lines
4.9 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-08-23 16:51:51 +02:00
#include "Emu/Memory/Memory.h"
2014-08-22 18:36:27 +02:00
#include "Emu/System.h"
2015-03-06 23:58:42 +01:00
#include "Emu/IdManager.h"
#include "Emu/SysCalls/SysCalls.h"
2014-08-23 16:51:51 +02:00
#include "Emu/Cell/PPUThread.h"
2015-03-02 22:09:20 +01:00
#include "sleep_queue.h"
#include "sys_time.h"
#include "sys_rwlock.h"
SysCallBase sys_rwlock("sys_rwlock");
2014-10-11 19:20:01 +02:00
s32 sys_rwlock_create(vm::ptr<u32> rw_lock_id, vm::ptr<sys_rwlock_attribute_t> attr)
{
2015-03-08 03:32:41 +01:00
sys_rwlock.Warning("sys_rwlock_create(rw_lock_id=*0x%x, attr=*0x%x)", rw_lock_id, attr);
2015-03-08 03:32:41 +01:00
if (!rw_lock_id || !attr)
2015-01-04 23:45:09 +01:00
{
return CELL_EFAULT;
}
2015-03-08 03:32:41 +01:00
const u32 protocol = attr->protocol;
switch (protocol)
{
2015-03-08 03:32:41 +01:00
case SYS_SYNC_FIFO: break;
case SYS_SYNC_PRIORITY: break;
case SYS_SYNC_PRIORITY_INHERIT: break;
default: sys_rwlock.Error("sys_rwlock_create(): unknown protocol (0x%x)", protocol); return CELL_EINVAL;
}
2014-08-13 20:01:09 +02:00
if (attr->pshared != SYS_SYNC_NOT_PROCESS_SHARED || attr->ipc_key.data() || attr->flags.data())
{
2015-03-08 03:32:41 +01:00
sys_rwlock.Error("sys_rwlock_create(): unknown attributes (pshared=0x%x, ipc_key=0x%llx, flags=0x%x)", attr->pshared, attr->ipc_key, attr->flags);
2014-08-13 20:01:09 +02:00
return CELL_EINVAL;
}
*rw_lock_id = Emu.GetIdManager().make<lv2_rwlock_t>(attr->protocol, attr->name_u64);
return CELL_OK;
}
s32 sys_rwlock_destroy(u32 rw_lock_id)
{
2015-04-14 04:00:31 +02:00
sys_rwlock.Warning("sys_rwlock_destroy(rw_lock_id=0x%x)", rw_lock_id);
2015-03-08 03:32:41 +01:00
LV2_LOCK;
const auto rwlock = Emu.GetIdManager().get<lv2_rwlock_t>(rw_lock_id);
2015-03-11 16:30:50 +01:00
2015-04-12 22:16:30 +02:00
if (!rwlock)
{
return CELL_ESRCH;
}
2015-03-11 16:30:50 +01:00
if (rwlock->readers || rwlock->writer || rwlock->rwaiters || rwlock->wwaiters)
{
return CELL_EBUSY;
}
Emu.GetIdManager().remove<lv2_rwlock_t>(rw_lock_id);
2015-03-08 03:32:41 +01:00
return CELL_OK;
}
s32 sys_rwlock_rlock(u32 rw_lock_id, u64 timeout)
{
2015-04-14 04:00:31 +02:00
sys_rwlock.Log("sys_rwlock_rlock(rw_lock_id=0x%x, timeout=0x%llx)", rw_lock_id, timeout);
const u64 start_time = get_system_time();
2015-03-08 03:32:41 +01:00
LV2_LOCK;
const auto rwlock = Emu.GetIdManager().get<lv2_rwlock_t>(rw_lock_id);
2015-03-11 16:30:50 +01:00
2015-04-12 22:16:30 +02:00
if (!rwlock)
{
return CELL_ESRCH;
}
2015-03-11 16:30:50 +01:00
// waiting threads are not properly registered in current implementation
rwlock->rwaiters++;
while (rwlock->writer || rwlock->wwaiters)
{
if (timeout && get_system_time() - start_time > timeout)
{
2015-03-11 16:30:50 +01:00
rwlock->rwaiters--;
return CELL_ETIMEDOUT;
}
if (Emu.IsStopped())
{
2015-04-14 04:00:31 +02:00
sys_rwlock.Warning("sys_rwlock_rlock(rw_lock_id=0x%x) aborted", rw_lock_id);
return CELL_OK;
}
2015-03-08 03:32:41 +01:00
2015-03-11 16:30:50 +01:00
rwlock->rcv.wait_for(lv2_lock, std::chrono::milliseconds(1));
}
2015-03-08 03:32:41 +01:00
rwlock->readers++;
2015-03-11 16:30:50 +01:00
rwlock->rwaiters--;
2015-03-08 03:32:41 +01:00
return CELL_OK;
}
s32 sys_rwlock_tryrlock(u32 rw_lock_id)
{
2015-04-14 04:00:31 +02:00
sys_rwlock.Log("sys_rwlock_tryrlock(rw_lock_id=0x%x)", rw_lock_id);
2015-03-08 03:32:41 +01:00
LV2_LOCK;
const auto rwlock = Emu.GetIdManager().get<lv2_rwlock_t>(rw_lock_id);
2015-03-11 16:30:50 +01:00
2015-04-12 22:16:30 +02:00
if (!rwlock)
{
return CELL_ESRCH;
}
2015-03-11 16:30:50 +01:00
if (rwlock->writer || rwlock->wwaiters)
{
2015-03-08 03:32:41 +01:00
return CELL_EBUSY;
}
2015-03-08 03:32:41 +01:00
rwlock->readers++;
return CELL_OK;
}
s32 sys_rwlock_runlock(u32 rw_lock_id)
{
2015-04-14 04:00:31 +02:00
sys_rwlock.Log("sys_rwlock_runlock(rw_lock_id=0x%x)", rw_lock_id);
2015-03-08 03:32:41 +01:00
LV2_LOCK;
const auto rwlock = Emu.GetIdManager().get<lv2_rwlock_t>(rw_lock_id);
2015-03-11 16:30:50 +01:00
2015-04-12 22:16:30 +02:00
if (!rwlock)
{
return CELL_ESRCH;
}
2015-03-08 03:32:41 +01:00
if (!rwlock->readers)
{
2015-03-08 03:32:41 +01:00
return CELL_EPERM;
}
2015-03-11 16:30:50 +01:00
if (!--rwlock->readers && rwlock->wwaiters)
{
2015-03-11 16:30:50 +01:00
rwlock->wcv.notify_one();
}
2015-03-08 03:32:41 +01:00
return CELL_OK;
}
s32 sys_rwlock_wlock(PPUThread& CPU, u32 rw_lock_id, u64 timeout)
{
2015-04-14 04:00:31 +02:00
sys_rwlock.Log("sys_rwlock_wlock(rw_lock_id=0x%x, timeout=0x%llx)", rw_lock_id, timeout);
const u64 start_time = get_system_time();
2015-03-08 03:32:41 +01:00
LV2_LOCK;
const auto rwlock = Emu.GetIdManager().get<lv2_rwlock_t>(rw_lock_id);
2015-03-11 16:30:50 +01:00
2015-04-12 22:16:30 +02:00
if (!rwlock)
{
2015-03-08 03:32:41 +01:00
return CELL_ESRCH;
}
2015-03-08 03:32:41 +01:00
if (rwlock->writer == CPU.GetId())
{
return CELL_EDEADLK;
}
2015-03-08 03:32:41 +01:00
// protocol is ignored in current implementation
2015-03-11 16:30:50 +01:00
rwlock->wwaiters++;
2015-03-08 03:32:41 +01:00
while (rwlock->readers || rwlock->writer)
{
if (timeout && get_system_time() - start_time > timeout)
{
2015-03-11 16:30:50 +01:00
rwlock->wwaiters--;
return CELL_ETIMEDOUT;
}
if (Emu.IsStopped())
{
2015-04-14 04:00:31 +02:00
sys_rwlock.Warning("sys_rwlock_wlock(rw_lock_id=0x%x) aborted", rw_lock_id);
return CELL_OK;
}
2015-03-11 16:30:50 +01:00
rwlock->wcv.wait_for(lv2_lock, std::chrono::milliseconds(1));
}
2015-03-08 03:32:41 +01:00
rwlock->writer = CPU.GetId();
2015-03-11 16:30:50 +01:00
rwlock->wwaiters--;
2015-03-08 03:32:41 +01:00
return CELL_OK;
}
s32 sys_rwlock_trywlock(PPUThread& CPU, u32 rw_lock_id)
{
2015-04-14 04:00:31 +02:00
sys_rwlock.Log("sys_rwlock_trywlock(rw_lock_id=0x%x)", rw_lock_id);
2015-03-08 03:32:41 +01:00
LV2_LOCK;
const auto rwlock = Emu.GetIdManager().get<lv2_rwlock_t>(rw_lock_id);
2015-03-11 16:30:50 +01:00
2015-04-12 22:16:30 +02:00
if (!rwlock)
{
return CELL_ESRCH;
}
2015-03-08 03:32:41 +01:00
if (rwlock->writer == CPU.GetId())
{
2015-03-08 03:32:41 +01:00
return CELL_EDEADLK;
}
2015-03-11 16:30:50 +01:00
if (rwlock->readers || rwlock->writer || rwlock->wwaiters)
{
2015-03-08 03:32:41 +01:00
return CELL_EBUSY;
}
2015-03-08 03:32:41 +01:00
rwlock->writer = CPU.GetId();
return CELL_OK;
}
s32 sys_rwlock_wunlock(PPUThread& CPU, u32 rw_lock_id)
{
2015-04-14 04:00:31 +02:00
sys_rwlock.Log("sys_rwlock_wunlock(rw_lock_id=0x%x)", rw_lock_id);
2015-03-08 03:32:41 +01:00
LV2_LOCK;
const auto rwlock = Emu.GetIdManager().get<lv2_rwlock_t>(rw_lock_id);
2015-03-11 16:30:50 +01:00
2015-04-12 22:16:30 +02:00
if (!rwlock)
{
return CELL_ESRCH;
}
2015-03-08 03:32:41 +01:00
if (rwlock->writer != CPU.GetId())
{
2015-03-08 03:32:41 +01:00
return CELL_EPERM;
}
2015-03-08 03:32:41 +01:00
rwlock->writer = 0;
2015-03-11 16:30:50 +01:00
if (rwlock->wwaiters)
{
rwlock->wcv.notify_one();
}
else if (rwlock->rwaiters)
{
rwlock->rcv.notify_all();
}
2015-03-08 03:32:41 +01:00
return CELL_OK;
}