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

248 lines
4.9 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"
2014-08-23 16:51:51 +02:00
2016-04-14 00:23:53 +02:00
#include "Emu/Cell/ErrorCodes.h"
2014-08-23 16:51:51 +02:00
#include "Emu/Cell/PPUThread.h"
2014-12-22 01:56:04 +01:00
#include "sys_mutex.h"
#include "sys_cond.h"
2016-05-13 15:55:34 +02:00
#include <algorithm>
logs::channel sys_cond("sys_cond", logs::level::notice);
extern u64 get_system_time();
2016-04-14 00:23:53 +02:00
void lv2_cond_t::notify(lv2_lock_t, cpu_thread* thread)
2015-07-19 03:56:33 +02:00
{
if (mutex->owner)
{
// add thread to the mutex sleep queue if cannot lock immediately
mutex->sq.emplace_back(thread);
}
else
{
mutex->owner = idm::get<ppu_thread>(thread->id);
2016-08-15 02:11:49 +02:00
thread->set_signal();
2015-07-19 03:56:33 +02:00
}
}
s32 sys_cond_create(vm::ptr<u32> cond_id, u32 mutex_id, vm::ptr<sys_cond_attribute_t> attr)
{
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 = idm::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;
}
2015-09-15 18:23:17 +02:00
if (attr->pshared != SYS_SYNC_NOT_PROCESS_SHARED || attr->ipc_key || attr->flags)
2015-03-06 23:10:04 +01:00
{
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
{
fmt::throw_exception("Unexpected cond_count" HERE);
2014-02-13 17:59:13 +01:00
}
*cond_id = idm::make<lv2_cond_t>(mutex, attr->name_u64);
return CELL_OK;
}
s32 sys_cond_destroy(u32 cond_id)
{
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 = idm::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-07-21 13:55:29 +02:00
if (!cond->sq.empty())
2014-02-13 17:59:13 +01:00
{
return CELL_EBUSY;
}
2015-03-06 23:10:04 +01:00
if (!cond->mutex->cond_count--)
{
fmt::throw_exception("Unexpected cond_count" HERE);
2015-03-06 23:10:04 +01:00
}
idm::remove<lv2_cond_t>(cond_id);
2015-03-06 23:10:04 +01:00
return CELL_OK;
}
s32 sys_cond_signal(u32 cond_id)
{
sys_cond.trace("sys_cond_signal(cond_id=0x%x)", cond_id);
2015-03-06 23:10:04 +01:00
LV2_LOCK;
const auto cond = idm::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-07-19 03:56:33 +02:00
// signal one waiting thread; protocol is ignored in current implementation
if (!cond->sq.empty())
2015-03-06 23:10:04 +01:00
{
2015-07-26 13:21:25 +02:00
cond->notify(lv2_lock, cond->sq.front());
2015-07-19 03:56:33 +02:00
cond->sq.pop_front();
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)
{
sys_cond.trace("sys_cond_signal_all(cond_id=0x%x)", cond_id);
2015-03-06 23:10:04 +01:00
LV2_LOCK;
const auto cond = idm::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-07-19 03:56:33 +02:00
// signal all waiting threads; protocol is ignored in current implementation
2015-07-26 13:21:25 +02:00
for (auto& thread : cond->sq)
{
2015-07-26 13:21:25 +02:00
cond->notify(lv2_lock, thread);
}
2015-07-19 03:56:33 +02:00
cond->sq.clear();
return CELL_OK;
}
s32 sys_cond_signal_to(u32 cond_id, u32 thread_id)
{
sys_cond.trace("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 = idm::get<lv2_cond_t>(cond_id);
2015-04-12 03:36:25 +02:00
if (!cond)
{
return CELL_ESRCH;
}
2016-04-14 00:23:53 +02:00
const auto found = std::find_if(cond->sq.begin(), cond->sq.end(), [=](cpu_thread* thread)
2015-07-26 13:21:25 +02:00
{
2016-04-14 00:23:53 +02:00
return thread->id == thread_id;
2015-07-26 13:21:25 +02:00
});
2015-07-26 13:21:25 +02:00
// TODO: check if CELL_ESRCH is returned if thread_id is invalid
if (found == cond->sq.end())
2014-02-26 11:35:30 +01:00
{
2015-07-26 13:21:25 +02:00
return CELL_EPERM;
2014-02-26 11:35:30 +01:00
}
2015-03-06 23:10:04 +01:00
2015-07-26 13:21:25 +02:00
// signal specified thread
cond->notify(lv2_lock, *found);
cond->sq.erase(found);
return CELL_OK;
2014-02-13 17:59:13 +01:00
}
s32 sys_cond_wait(ppu_thread& ppu, u32 cond_id, u64 timeout)
2014-02-13 17:59:13 +01:00
{
sys_cond.trace("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 = idm::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;
}
// check current ownership
if (cond->mutex->owner.get() != &ppu)
{
2014-02-26 11:35:30 +01:00
return CELL_EPERM;
}
2015-07-17 18:27:12 +02:00
// save the 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-07-17 18:27:12 +02:00
// unlock the mutex
cond->mutex->unlock(lv2_lock);
2015-04-12 03:36:25 +02:00
2015-07-19 03:56:33 +02:00
// add waiter; protocol is ignored in current implementation
2016-04-14 00:23:53 +02:00
sleep_entry<cpu_thread> waiter(cond->sq, ppu);
2015-07-21 13:55:29 +02:00
// potential mutex waiter (not added immediately)
2016-04-14 00:23:53 +02:00
sleep_entry<cpu_thread> mutex_waiter(cond->mutex->sq, ppu, defer_sleep);
2015-07-19 03:56:33 +02:00
while (!ppu.state.test_and_reset(cpu_flag::signal))
2015-07-19 03:56:33 +02:00
{
2015-07-19 13:36:32 +02:00
CHECK_EMU_STATUS;
2015-07-19 03:56:33 +02:00
// timeout is ignored if waiting on the cond var is already dropped
if (timeout && waiter)
{
2015-07-19 03:56:33 +02:00
const u64 passed = get_system_time() - start_time;
2015-07-19 03:56:33 +02:00
if (passed >= timeout)
{
// try to reown mutex and exit if timed out
if (!cond->mutex->owner)
{
cond->mutex->owner = idm::get<ppu_thread>(ppu.id);
break;
}
2015-07-19 03:56:33 +02:00
// drop condition variable and start waiting on the mutex queue
mutex_waiter.enter();
waiter.leave();
continue;
2015-04-12 03:36:25 +02:00
}
2015-03-10 15:42:08 +01:00
get_current_thread_cv().wait_for(lv2_lock, std::chrono::microseconds(timeout - passed));
2015-07-19 03:56:33 +02:00
}
else
{
get_current_thread_cv().wait(lv2_lock);
2014-02-26 11:35:30 +01:00
}
}
2015-03-06 23:10:04 +01:00
2015-07-19 03:56:33 +02:00
// mutex owner is restored after notification or unlocking
if (cond->mutex->owner.get() != &ppu)
{
fmt::throw_exception("Unexpected mutex owner" HERE);
}
// restore the recursive value
2015-03-06 23:10:04 +01:00
cond->mutex->recursive_count = recursive_value;
2015-07-19 03:56:33 +02:00
// check timeout (unclear)
if (timeout && get_system_time() - start_time > timeout)
{
return CELL_ETIMEDOUT;
}
2015-03-06 23:10:04 +01:00
2014-03-19 01:32:23 +01:00
return CELL_OK;
}