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

223 lines
4.4 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
2014-08-26 01:55:37 +02:00
#include "Emu/CPU/CPUThreadManager.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"
2014-12-23 00:31:11 +01:00
#include "sys_time.h"
2014-12-22 01:56:04 +01:00
#include "sys_mutex.h"
#include "sys_cond.h"
SysCallBase sys_cond("sys_cond");
s32 sys_cond_create(vm::ref<u32> cond_id, u32 mutex_id, vm::ptr<sys_cond_attribute_t> attr)
{
2015-04-14 04:00:31 +02:00
sys_cond.Warning("sys_cond_create(cond_id=*0x%x, mutex_id=0x%x, attr=*0x%x)", cond_id, mutex_id, attr);
2014-09-20 02:08:12 +02:00
2015-03-06 23:10:04 +01:00
LV2_LOCK;
const auto mutex = std::move(Emu.GetIdManager().get<lv2_mutex_t>(mutex_id));
2015-03-06 23:10:04 +01:00
2015-04-12 03:36:25 +02:00
if (!mutex)
2014-02-13 17:59:13 +01:00
{
2015-03-06 23:10:04 +01:00
return CELL_ESRCH;
}
if (attr->pshared.data() != se32(0x200) || attr->ipc_key.data() || attr->flags.data())
{
sys_cond.Error("sys_cond_create(): unknown attributes (pshared=0x%x, ipc_key=0x%llx, flags=0x%x)", attr->pshared, attr->ipc_key, attr->flags);
2014-02-13 17:59:13 +01:00
return CELL_EINVAL;
}
2015-03-06 23:10:04 +01:00
if (!++mutex->cond_count)
2014-02-13 17:59:13 +01:00
{
2015-03-06 23:10:04 +01:00
throw __FUNCTION__;
2014-02-13 17:59:13 +01:00
}
cond_id = Emu.GetIdManager().make<lv2_cond_t>(mutex, attr->name_u64);
return CELL_OK;
}
s32 sys_cond_destroy(u32 cond_id)
{
2015-04-14 04:00:31 +02:00
sys_cond.Warning("sys_cond_destroy(cond_id=0x%x)", cond_id);
2015-03-06 23:10:04 +01:00
LV2_LOCK;
const auto cond = Emu.GetIdManager().get<lv2_cond_t>(cond_id);
2015-03-06 23:10:04 +01:00
2015-04-12 03:36:25 +02:00
if (!cond)
2014-02-13 17:59:13 +01:00
{
return CELL_ESRCH;
}
2015-04-12 03:36:25 +02:00
if (!cond->waiters.empty() || cond->signaled)
2014-02-13 17:59:13 +01:00
{
return CELL_EBUSY;
}
2015-03-06 23:10:04 +01:00
if (!cond->mutex->cond_count--)
{
throw __FUNCTION__;
}
Emu.GetIdManager().remove<lv2_cond_t>(cond_id);
2015-03-06 23:10:04 +01:00
return CELL_OK;
}
s32 sys_cond_signal(u32 cond_id)
{
2015-04-14 04:00:31 +02:00
sys_cond.Log("sys_cond_signal(cond_id=0x%x)", cond_id);
2015-03-06 23:10:04 +01:00
LV2_LOCK;
const auto cond = Emu.GetIdManager().get<lv2_cond_t>(cond_id);
2015-03-06 23:10:04 +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())
2015-03-06 23:10:04 +01:00
{
cond->signaled++;
2015-04-12 03:36:25 +02:00
cond->waiters.erase(cond->waiters.begin());
2015-03-09 20:56:55 +01:00
cond->cv.notify_one();
2015-03-06 23:10:04 +01:00
}
2014-02-26 11:35:30 +01:00
return CELL_OK;
}
s32 sys_cond_signal_all(u32 cond_id)
{
2015-04-14 04:00:31 +02:00
sys_cond.Log("sys_cond_signal_all(cond_id=0x%x)", cond_id);
2015-03-06 23:10:04 +01:00
LV2_LOCK;
const auto cond = Emu.GetIdManager().get<lv2_cond_t>(cond_id);
2015-03-06 23:10:04 +01:00
2015-04-12 03:36:25 +02:00
if (!cond)
{
return CELL_ESRCH;
}
2015-06-21 02:17:42 +02:00
if (const u64 count = cond->waiters.size())
{
2015-04-12 03:36:25 +02:00
cond->signaled += count;
cond->waiters.clear();
2015-03-09 20:56:55 +01:00
cond->cv.notify_all();
}
return CELL_OK;
}
s32 sys_cond_signal_to(u32 cond_id, u32 thread_id)
{
2015-04-14 04:00:31 +02:00
sys_cond.Log("sys_cond_signal_to(cond_id=0x%x, thread_id=0x%x)", cond_id, thread_id);
2015-03-06 23:10:04 +01:00
LV2_LOCK;
const auto cond = Emu.GetIdManager().get<lv2_cond_t>(cond_id);
2015-04-12 03:36:25 +02:00
if (!cond)
{
return CELL_ESRCH;
}
if (!Emu.GetIdManager().check_id<CPUThread>(thread_id))
2014-02-26 11:35:30 +01:00
{
return CELL_ESRCH;
}
2015-04-12 03:36:25 +02:00
const auto found = cond->waiters.find(thread_id);
if (found == cond->waiters.end())
2014-02-26 11:35:30 +01:00
{
return CELL_EPERM;
}
2015-03-06 23:10:04 +01:00
cond->signaled++;
2015-04-12 03:36:25 +02:00
cond->waiters.erase(found);
2015-03-09 20:56:55 +01:00
cond->cv.notify_one();
2014-02-13 17:59:13 +01:00
return CELL_OK;
}
2014-12-23 00:31:11 +01:00
s32 sys_cond_wait(PPUThread& CPU, u32 cond_id, u64 timeout)
2014-02-13 17:59:13 +01:00
{
2015-04-14 04:00:31 +02:00
sys_cond.Log("sys_cond_wait(cond_id=0x%x, timeout=%lld)", cond_id, timeout);
const u64 start_time = get_system_time();
2015-03-06 23:10:04 +01:00
LV2_LOCK;
const auto cond = Emu.GetIdManager().get<lv2_cond_t>(cond_id);
2015-03-06 23:10:04 +01:00
2015-04-12 03:36:25 +02:00
if (!cond)
{
return CELL_ESRCH;
}
2015-04-12 03:36:25 +02:00
const auto thread = Emu.GetCPU().GetThread(CPU.GetId());
2015-03-06 23:10:04 +01:00
if (cond->mutex->owner.owner_before(thread) || thread.owner_before(cond->mutex->owner)) // check equality
{
2014-02-26 11:35:30 +01:00
return CELL_EPERM;
}
2015-04-12 03:36:25 +02:00
// add waiter; protocol is ignored in current implementation
cond->waiters.emplace(CPU.GetId());
2014-02-26 11:35:30 +01:00
2015-03-06 23:10:04 +01:00
// unlock mutex
cond->mutex->owner.reset();
2015-03-11 16:30:50 +01:00
if (cond->mutex->waiters)
{
cond->mutex->cv.notify_one();
}
2014-02-26 11:35:30 +01:00
2015-03-09 20:56:55 +01:00
// save recursive value
2015-03-06 23:10:04 +01:00
const u32 recursive_value = cond->mutex->recursive_count.exchange(0);
2014-02-13 17:59:13 +01:00
2015-04-12 03:36:25 +02:00
while (!cond->mutex->owner.expired() || !cond->signaled || cond->waiters.count(CPU.GetId()))
2015-03-06 23:10:04 +01:00
{
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 && cond->mutex->owner.expired())
2014-02-26 11:35:30 +01:00
{
2015-03-10 15:42:08 +01:00
// cancel waiting if the mutex is free, restore its owner and recursive value
cond->mutex->owner = thread;
cond->mutex->recursive_count = recursive_value;
2015-04-12 03:36:25 +02:00
if (!cond->waiters.erase(CPU.GetId()))
{
throw __FUNCTION__;
}
2015-03-10 15:42:08 +01:00
2015-03-06 23:10:04 +01:00
return CELL_ETIMEDOUT;
2014-02-26 11:35:30 +01:00
}
2014-12-23 00:31:11 +01:00
2014-02-26 11:35:30 +01:00
if (Emu.IsStopped())
{
2015-04-14 04:00:31 +02:00
sys_cond.Warning("sys_cond_wait(id=0x%x) aborted", cond_id);
2014-12-23 00:31:11 +01:00
return CELL_OK;
2014-02-26 11:35:30 +01:00
}
2014-03-19 01:32:23 +01:00
2015-03-09 20:56:55 +01:00
// wait on appropriate condition variable
2015-03-10 15:42:08 +01:00
(cond->signaled || is_timedout ? cond->mutex->cv : cond->cv).wait_for(lv2_lock, std::chrono::milliseconds(1));
}
2015-03-06 23:10:04 +01:00
2015-03-10 15:42:08 +01:00
// reown the mutex and restore its recursive value
2015-03-06 23:10:04 +01:00
cond->mutex->owner = thread;
cond->mutex->recursive_count = recursive_value;
2015-03-10 15:42:08 +01:00
cond->signaled--;
2015-03-06 23:10:04 +01:00
2014-03-19 01:32:23 +01:00
return CELL_OK;
}