2013-11-09 02:05:58 +01:00
|
|
|
#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"
|
2013-11-09 02:05:58 +01:00
|
|
|
#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"
|
2015-01-02 00:41:29 +01:00
|
|
|
#include "sys_time.h"
|
2014-07-06 01:30:28 +02:00
|
|
|
#include "sys_rwlock.h"
|
2013-11-09 02:05:58 +01:00
|
|
|
|
|
|
|
|
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)
|
2013-11-09 02:05:58 +01:00
|
|
|
{
|
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);
|
2014-01-29 21:31:09 +01:00
|
|
|
|
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)
|
2014-01-29 21:31:09 +01:00
|
|
|
{
|
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-01-29 21:31:09 +01:00
|
|
|
}
|
2014-08-13 20:01:09 +02:00
|
|
|
|
2015-06-24 18:25:37 +02:00
|
|
|
if (attr->pshared != SYS_SYNC_NOT_PROCESS_SHARED || attr->ipc_key.data() || attr->flags.data())
|
2014-01-29 21:31:09 +01:00
|
|
|
{
|
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;
|
|
|
|
|
}
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
*rw_lock_id = Emu.GetIdManager().make<lv2_rwlock_t>(attr->protocol, attr->name_u64);
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2013-11-09 02:05:58 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-25 00:38:34 +02:00
|
|
|
s32 sys_rwlock_destroy(u32 rw_lock_id)
|
2013-11-09 02:05:58 +01:00
|
|
|
{
|
2015-04-14 04:00:31 +02:00
|
|
|
sys_rwlock.Warning("sys_rwlock_destroy(rw_lock_id=0x%x)", rw_lock_id);
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
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-01-02 00:41:29 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-11 16:30:50 +01:00
|
|
|
if (rwlock->readers || rwlock->writer || rwlock->rwaiters || rwlock->wwaiters)
|
2015-01-02 00:41:29 +01:00
|
|
|
{
|
|
|
|
|
return CELL_EBUSY;
|
|
|
|
|
}
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
Emu.GetIdManager().remove<lv2_rwlock_t>(rw_lock_id);
|
2015-03-08 03:32:41 +01:00
|
|
|
|
2013-11-09 02:05:58 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-25 00:38:34 +02:00
|
|
|
s32 sys_rwlock_rlock(u32 rw_lock_id, u64 timeout)
|
2013-11-09 02:05:58 +01:00
|
|
|
{
|
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);
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-01-02 00:41:29 +01:00
|
|
|
const u64 start_time = get_system_time();
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
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-01-02 00:41:29 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-11 16:30:50 +01:00
|
|
|
// waiting threads are not properly registered in current implementation
|
|
|
|
|
rwlock->rwaiters++;
|
|
|
|
|
|
|
|
|
|
while (rwlock->writer || rwlock->wwaiters)
|
2014-01-29 21:31:09 +01:00
|
|
|
{
|
2015-01-02 00:41:29 +01:00
|
|
|
if (timeout && get_system_time() - start_time > timeout)
|
|
|
|
|
{
|
2015-03-11 16:30:50 +01:00
|
|
|
rwlock->rwaiters--;
|
2015-01-02 00:41:29 +01:00
|
|
|
return CELL_ETIMEDOUT;
|
|
|
|
|
}
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-01-02 00:41:29 +01:00
|
|
|
if (Emu.IsStopped())
|
2014-01-29 21:31:09 +01:00
|
|
|
{
|
2015-04-14 04:00:31 +02:00
|
|
|
sys_rwlock.Warning("sys_rwlock_rlock(rw_lock_id=0x%x) aborted", rw_lock_id);
|
2015-01-02 00:41:29 +01:00
|
|
|
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-01-02 00:41:29 +01:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2015-01-02 00:41:29 +01:00
|
|
|
return CELL_OK;
|
2013-11-09 02:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-25 00:38:34 +02:00
|
|
|
s32 sys_rwlock_tryrlock(u32 rw_lock_id)
|
2013-11-09 02:05:58 +01:00
|
|
|
{
|
2015-04-14 04:00:31 +02:00
|
|
|
sys_rwlock.Log("sys_rwlock_tryrlock(rw_lock_id=0x%x)", rw_lock_id);
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
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-01-02 00:41:29 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-11 16:30:50 +01:00
|
|
|
if (rwlock->writer || rwlock->wwaiters)
|
2015-01-02 00:41:29 +01:00
|
|
|
{
|
2015-03-08 03:32:41 +01:00
|
|
|
return CELL_EBUSY;
|
2015-01-02 00:41:29 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
rwlock->readers++;
|
|
|
|
|
|
|
|
|
|
return CELL_OK;
|
2013-11-09 02:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-25 00:38:34 +02:00
|
|
|
s32 sys_rwlock_runlock(u32 rw_lock_id)
|
2013-11-09 02:05:58 +01:00
|
|
|
{
|
2015-04-14 04:00:31 +02:00
|
|
|
sys_rwlock.Log("sys_rwlock_runlock(rw_lock_id=0x%x)", rw_lock_id);
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
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-01-02 00:41:29 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
if (!rwlock->readers)
|
2015-01-02 00:41:29 +01:00
|
|
|
{
|
2015-03-08 03:32:41 +01:00
|
|
|
return CELL_EPERM;
|
|
|
|
|
}
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-11 16:30:50 +01:00
|
|
|
if (!--rwlock->readers && rwlock->wwaiters)
|
2015-01-02 00:41:29 +01:00
|
|
|
{
|
2015-03-11 16:30:50 +01:00
|
|
|
rwlock->wcv.notify_one();
|
2015-01-02 00:41:29 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
return CELL_OK;
|
2013-11-09 02:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-02 00:41:29 +01:00
|
|
|
s32 sys_rwlock_wlock(PPUThread& CPU, u32 rw_lock_id, u64 timeout)
|
2013-11-09 02:05:58 +01:00
|
|
|
{
|
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);
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-01-02 00:41:29 +01:00
|
|
|
const u64 start_time = get_system_time();
|
|
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
LV2_LOCK;
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
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-01-02 00:41:29 +01:00
|
|
|
{
|
2015-03-08 03:32:41 +01:00
|
|
|
return CELL_ESRCH;
|
2015-01-02 00:41:29 +01:00
|
|
|
}
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
if (rwlock->writer == CPU.GetId())
|
2014-01-29 21:31:09 +01:00
|
|
|
{
|
2015-01-02 00:41:29 +01:00
|
|
|
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-01-02 00:41:29 +01:00
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
while (rwlock->readers || rwlock->writer)
|
2015-01-02 00:41:29 +01:00
|
|
|
{
|
|
|
|
|
if (timeout && get_system_time() - start_time > timeout)
|
2014-01-29 21:31:09 +01:00
|
|
|
{
|
2015-03-11 16:30:50 +01:00
|
|
|
rwlock->wwaiters--;
|
2015-01-02 00:41:29 +01:00
|
|
|
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);
|
2015-01-02 00:41:29 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 16:30:50 +01:00
|
|
|
rwlock->wcv.wait_for(lv2_lock, std::chrono::milliseconds(1));
|
2015-01-02 00:41:29 +01:00
|
|
|
}
|
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
|
|
|
|
2015-01-02 00:41:29 +01:00
|
|
|
return CELL_OK;
|
2013-11-09 02:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-02 00:41:29 +01:00
|
|
|
s32 sys_rwlock_trywlock(PPUThread& CPU, u32 rw_lock_id)
|
2013-11-09 02:05:58 +01:00
|
|
|
{
|
2015-04-14 04:00:31 +02:00
|
|
|
sys_rwlock.Log("sys_rwlock_trywlock(rw_lock_id=0x%x)", rw_lock_id);
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
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-01-02 00:41:29 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
if (rwlock->writer == CPU.GetId())
|
2015-01-02 00:41:29 +01:00
|
|
|
{
|
2015-03-08 03:32:41 +01:00
|
|
|
return CELL_EDEADLK;
|
2015-01-02 00:41:29 +01:00
|
|
|
}
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-11 16:30:50 +01:00
|
|
|
if (rwlock->readers || rwlock->writer || rwlock->wwaiters)
|
2015-01-02 00:41:29 +01:00
|
|
|
{
|
2015-03-08 03:32:41 +01:00
|
|
|
return CELL_EBUSY;
|
2015-01-02 00:41:29 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
rwlock->writer = CPU.GetId();
|
|
|
|
|
|
|
|
|
|
return CELL_OK;
|
2013-11-09 02:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-02 00:41:29 +01:00
|
|
|
s32 sys_rwlock_wunlock(PPUThread& CPU, u32 rw_lock_id)
|
2013-11-09 02:05:58 +01:00
|
|
|
{
|
2015-04-14 04:00:31 +02:00
|
|
|
sys_rwlock.Log("sys_rwlock_wunlock(rw_lock_id=0x%x)", rw_lock_id);
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
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-01-02 00:41:29 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
2014-01-29 21:31:09 +01:00
|
|
|
|
2015-03-08 03:32:41 +01:00
|
|
|
if (rwlock->writer != CPU.GetId())
|
2015-01-02 00:41:29 +01:00
|
|
|
{
|
2015-03-08 03:32:41 +01:00
|
|
|
return CELL_EPERM;
|
2015-01-02 00:41:29 +01:00
|
|
|
}
|
|
|
|
|
|
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;
|
2013-11-09 02:05:58 +01:00
|
|
|
}
|