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

242 lines
5.1 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"
#include "Emu/SysCalls/SysCalls.h"
2014-08-23 16:51:51 +02:00
#include "Emu/Cell/PPUThread.h"
2015-03-08 22:56:45 +01:00
#include "Emu/SysCalls/Modules/sysPrxForUser.h"
2015-03-02 22:09:20 +01:00
#include "sleep_queue.h"
2014-12-23 00:31:11 +01:00
#include "sys_time.h"
#include "sys_lwmutex.h"
#include "sys_lwcond.h"
SysCallBase sys_lwcond("sys_lwcond");
2015-03-09 20:56:55 +01:00
void lwcond_create(sys_lwcond_t& lwcond, sys_lwmutex_t& lwmutex, u64 name)
{
2015-03-10 15:42:08 +01:00
std::shared_ptr<lwcond_t> cond(new lwcond_t(name));
2015-03-10 15:42:08 +01:00
lwcond.lwcond_queue = Emu.GetIdManager().GetNewID(cond, TYPE_LWCOND);
2014-09-19 02:19:22 +02:00
}
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)
{
2015-03-09 20:56:55 +01:00
sys_lwcond.Warning("_sys_lwcond_create(lwcond_id=*0x%x, lwmutex_id=%d, control=*0x%x, name=0x%llx, arg5=0x%x)", lwcond_id, lwmutex_id, control, name, arg5);
2015-03-10 15:42:08 +01:00
std::shared_ptr<lwcond_t> cond(new lwcond_t(name));
2015-03-10 15:42:08 +01:00
*lwcond_id = Emu.GetIdManager().GetNewID(cond, TYPE_LWCOND);
return CELL_OK;
}
2015-03-09 20:56:55 +01:00
s32 _sys_lwcond_destroy(u32 lwcond_id)
{
2015-03-09 20:56:55 +01:00
sys_lwcond.Warning("_sys_lwcond_destroy(lwcond_id=%d)", lwcond_id);
2015-03-09 20:56:55 +01:00
LV2_LOCK;
2015-03-10 15:42:08 +01:00
std::shared_ptr<lwcond_t> cond;
if (!Emu.GetIdManager().GetIDData(lwcond_id, cond))
{
return CELL_ESRCH;
}
2015-03-10 15:42:08 +01:00
if (cond->waiters)
{
2015-03-09 20:56:55 +01:00
return CELL_EBUSY;
}
2015-03-10 15:42:08 +01:00
Emu.GetIdManager().RemoveID<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)
{
2015-03-10 15:42:08 +01:00
sys_lwcond.Log("_sys_lwcond_signal(lwcond_id=%d, lwmutex_id=%d, ppu_thread_id=%d, mode=%d)", lwcond_id, lwmutex_id, ppu_thread_id, mode);
LV2_LOCK;
std::shared_ptr<lwcond_t> cond;
if (!Emu.GetIdManager().GetIDData(lwcond_id, cond))
{
return CELL_ESRCH;
}
2015-03-10 22:47:13 +01:00
std::shared_ptr<lwmutex_t> mutex;
if (lwmutex_id && !Emu.GetIdManager().GetIDData(lwmutex_id, mutex))
{
return CELL_ESRCH;
}
// ppu_thread_id is ignored in current implementation
2015-03-10 15:42:08 +01:00
if (mode != 1 && mode != 2 && mode != 3)
{
sys_lwcond.Error("_sys_lwcond_signal(%d): invalid mode (%d)", lwcond_id, mode);
}
if (~ppu_thread_id)
{
sys_lwcond.Todo("_sys_lwcond_signal(%d): ppu_thread_id (%d)", lwcond_id, ppu_thread_id);
}
if (mode == 1)
{
// mode 1: lightweight mutex was initially owned by the calling thread
if (!cond->waiters)
{
return CELL_EPERM;
}
cond->signaled1++;
}
else if (mode == 2)
{
// mode 2: lightweight mutex was not owned by the calling thread and waiter hasn't been increased
if (!cond->waiters)
{
return CELL_OK;
}
cond->signaled2++;
}
else
{
// in mode 3, lightweight mutex was forcefully owned by the calling thread
if (!cond->waiters)
{
return ~ppu_thread_id ? CELL_ENOENT : CELL_EPERM;
}
cond->signaled1++;
}
cond->waiters--;
cond->cv.notify_one();
return CELL_OK;
}
2015-03-09 20:56:55 +01:00
s32 _sys_lwcond_signal_all(u32 lwcond_id, u32 lwmutex_id, u32 mode)
{
2015-03-10 15:42:08 +01:00
sys_lwcond.Log("_sys_lwcond_signal_all(lwcond_id=%d, lwmutex_id=%d, mode=%d)", lwcond_id, lwmutex_id, mode);
2015-03-10 15:42:08 +01:00
LV2_LOCK;
std::shared_ptr<lwcond_t> cond;
if (!Emu.GetIdManager().GetIDData(lwcond_id, cond))
{
return CELL_ESRCH;
}
2015-03-10 22:47:13 +01:00
std::shared_ptr<lwmutex_t> mutex;
if (lwmutex_id && !Emu.GetIdManager().GetIDData(lwmutex_id, mutex))
{
return CELL_ESRCH;
}
2015-03-10 15:42:08 +01:00
if (mode != 1 && mode != 2)
{
sys_lwcond.Error("_sys_lwcond_signal_all(%d): invalid mode (%d)", lwcond_id, mode);
}
const s32 count = cond->waiters.exchange(0);
cond->cv.notify_all();
if (mode == 1)
{
// in mode 1, lightweight mutex was initially owned by the calling thread
cond->signaled1 += count;
return count;
}
else
{
// in mode 2, lightweight mutex was not owned by the calling thread and waiter hasn't been increased
cond->signaled2 += count;
return CELL_OK;
}
}
2015-03-09 20:56:55 +01:00
s32 _sys_lwcond_queue_wait(u32 lwcond_id, u32 lwmutex_id, u64 timeout)
{
2015-03-10 15:42:08 +01:00
sys_lwcond.Log("_sys_lwcond_queue_wait(lwcond_id=%d, lwmutex_id=%d, 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;
std::shared_ptr<lwcond_t> cond;
if (!Emu.GetIdManager().GetIDData(lwcond_id, cond))
{
return CELL_ESRCH;
}
2015-03-10 22:47:13 +01:00
std::shared_ptr<lwmutex_t> mutex;
if (!Emu.GetIdManager().GetIDData(lwmutex_id, mutex))
{
return CELL_ESRCH;
}
// finalize unlocking the mutex
mutex->signaled++;
mutex->cv.notify_one();
// protocol is ignored in current implementation
2015-03-10 15:42:08 +01:00
cond->waiters++; assert(cond->waiters > 0);
2015-03-10 22:47:13 +01:00
while (!(cond->signaled1 && mutex->signaled) && !cond->signaled2)
2015-03-10 15:42:08 +01:00
{
const bool is_timedout = timeout && get_system_time() - start_time > timeout;
2015-03-11 12:45:58 +01:00
// check timeout only if no thread signaled in mode 1 (the flaw of avoiding sleep queue)
if (is_timedout && !cond->signaled1)
2015-03-10 15:42:08 +01:00
{
// cancel waiting
cond->waiters--; assert(cond->waiters >= 0);
2015-03-10 22:47:13 +01:00
if (mutex->signaled)
{
mutex->signaled--;
return CELL_EDEADLK;
}
else
{
return CELL_ETIMEDOUT;
}
2015-03-10 15:42:08 +01:00
}
if (Emu.IsStopped())
{
sys_lwcond.Warning("_sys_lwcond_queue_wait(lwcond_id=%d) aborted", lwcond_id);
return CELL_OK;
}
2015-03-11 12:35:23 +01:00
(cond->signaled1 ? mutex->cv : cond->cv).wait_for(lv2_lock, std::chrono::milliseconds(1));
2015-03-10 15:42:08 +01:00
}
2015-03-11 12:27:29 +01:00
if (cond->signaled1 && mutex->signaled)
2015-03-10 15:42:08 +01:00
{
2015-03-10 22:47:13 +01:00
mutex->signaled--;
2015-03-10 15:42:08 +01:00
cond->signaled1--;
return CELL_OK;
}
else
{
cond->signaled2--;
return CELL_EBUSY;
}
}