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

236 lines
5 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
s32 _sys_lwcond_create(vm::ptr<u32> lwcond_id, u32 lwmutex_id, vm::ptr<sys_lwcond_t> control, u64 name, u32 arg5)
{
2015-04-14 04:00:31 +02:00
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 = Emu.GetIdManager().make<lv2_lwcond_t>(name);
return CELL_OK;
}
2015-03-09 20:56:55 +01:00
s32 _sys_lwcond_destroy(u32 lwcond_id)
{
2015-04-14 04:00:31 +02:00
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 = Emu.GetIdManager().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;
}
2015-04-12 03:36:25 +02:00
if (!cond->waiters.empty() || cond->signaled1 || cond->signaled2)
{
2015-03-09 20:56:55 +01:00
return CELL_EBUSY;
}
Emu.GetIdManager().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)
{
2015-04-14 04:00:31 +02:00
sys_lwcond.Log("_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 = Emu.GetIdManager().get<lv2_lwcond_t>(lwcond_id);
const auto mutex = Emu.GetIdManager().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)
{
sys_lwcond.Error("_sys_lwcond_signal(%d): invalid mode (%d)", lwcond_id, mode);
}
2015-04-12 03:36:25 +02:00
const auto found = ~ppu_thread_id ? cond->waiters.find(ppu_thread_id) : cond->waiters.begin();
2015-03-10 15:42:08 +01:00
if (mode == 1)
{
// mode 1: lightweight mutex was initially owned by the calling thread
2015-04-12 03:36:25 +02:00
if (found == cond->waiters.end())
2015-03-10 15:42:08 +01:00
{
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
2015-04-12 03:36:25 +02:00
if (found == cond->waiters.end())
2015-03-10 15:42:08 +01:00
{
return CELL_OK;
}
cond->signaled2++;
}
else
{
// in mode 3, lightweight mutex was forcefully owned by the calling thread
2015-04-12 03:36:25 +02:00
if (found == cond->waiters.end())
2015-03-10 15:42:08 +01:00
{
return ~ppu_thread_id ? CELL_ENOENT : CELL_EPERM;
}
cond->signaled1++;
}
2015-04-12 03:36:25 +02:00
cond->waiters.erase(found);
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-04-14 04:00:31 +02:00
sys_lwcond.Log("_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 = Emu.GetIdManager().get<lv2_lwcond_t>(lwcond_id);
const auto mutex = Emu.GetIdManager().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)
{
sys_lwcond.Error("_sys_lwcond_signal_all(%d): invalid mode (%d)", lwcond_id, mode);
}
2015-06-21 02:17:42 +02:00
const u32 count = (u32)cond->waiters.size();
2015-03-11 16:30:50 +01:00
if (count)
{
2015-04-12 03:36:25 +02:00
cond->waiters.clear();
2015-03-11 16:30:50 +01:00
cond->cv.notify_all();
}
2015-03-10 15:42:08 +01:00
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-04-12 03:36:25 +02:00
s32 _sys_lwcond_queue_wait(PPUThread& CPU, u32 lwcond_id, u32 lwmutex_id, u64 timeout)
{
2015-04-14 04:00:31 +02:00
sys_lwcond.Log("_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 = Emu.GetIdManager().get<lv2_lwcond_t>(lwcond_id);
const auto mutex = Emu.GetIdManager().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->signaled++;
2015-03-11 16:30:50 +01:00
if (mutex->waiters)
{
mutex->cv.notify_one();
}
2015-03-10 22:47:13 +01:00
2015-04-12 03:36:25 +02:00
// add waiter; protocol is ignored in current implementation
cond->waiters.emplace(CPU.GetId());
2015-03-10 15:42:08 +01:00
2015-04-12 03:36:25 +02:00
while ((!(cond->signaled1 && mutex->signaled) && !cond->signaled2) || cond->waiters.count(CPU.GetId()))
2015-03-10 15:42:08 +01:00
{
const bool is_timedout = timeout && get_system_time() - start_time > timeout;
2015-04-12 03:36:25 +02:00
// check timeout
if (is_timedout)
2015-03-10 15:42:08 +01:00
{
// cancel waiting
2015-04-12 03:36:25 +02:00
if (!cond->waiters.erase(CPU.GetId()))
{
if (cond->signaled1 && !mutex->signaled)
{
cond->signaled1--;
}
else
{
2015-07-01 19:09:26 +02:00
throw EXCEPTION("Unexpected values");
2015-04-12 03:36:25 +02:00
}
}
2015-03-10 15:42:08 +01:00
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())
{
2015-04-14 04:00:31 +02:00
sys_lwcond.Warning("_sys_lwcond_queue_wait(lwcond_id=0x%x) aborted", lwcond_id);
2015-03-10 15:42:08 +01:00
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;
}
}