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

381 lines
7.7 KiB
C++
Raw Normal View History

2014-08-08 19:55:12 +04:00
#include "stdafx.h"
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
2015-03-07 01:58:42 +03:00
#include "Emu/IdManager.h"
2014-08-08 19:55:12 +04:00
2016-04-14 01:23:53 +03:00
#include "Emu/Cell/ErrorCodes.h"
2014-08-23 18:51:51 +04:00
#include "Emu/Cell/PPUThread.h"
2014-08-08 19:55:12 +04:00
#include "sys_event_flag.h"
2016-05-13 16:55:34 +03:00
#include <algorithm>
2016-08-20 00:14:10 +03:00
namespace vm { using namespace ps3; }
2017-05-13 21:30:37 +03:00
logs::channel sys_event_flag("sys_event_flag");
2014-08-08 19:55:12 +04:00
extern u64 get_system_time();
2017-02-03 19:27:03 +03:00
error_code sys_event_flag_create(vm::ptr<u32> id, vm::ptr<sys_event_flag_attribute_t> attr, u64 init)
2014-08-23 04:16:54 +04:00
{
sys_event_flag.warning("sys_event_flag_create(id=*0x%x, attr=*0x%x, init=0x%llx)", id, attr, init);
2014-08-23 04:16:54 +04:00
2015-03-06 00:29:05 +03:00
if (!id || !attr)
{
return CELL_EFAULT;
}
2015-03-06 00:29:05 +03:00
const u32 protocol = attr->protocol;
2017-02-06 21:36:46 +03:00
if (protocol == SYS_SYNC_RETRY)
sys_event_flag.todo("sys_event_flag_create(): SYS_SYNC_RETRY");
if (protocol == SYS_SYNC_PRIORITY_INHERIT)
sys_event_flag.todo("sys_event_flag_create(): SYS_SYNC_PRIORITY_INHERIT");
2015-07-20 00:29:40 +03:00
if (protocol != SYS_SYNC_FIFO && protocol != SYS_SYNC_RETRY && protocol != SYS_SYNC_PRIORITY && protocol != SYS_SYNC_PRIORITY_INHERIT)
2014-08-08 19:55:12 +04:00
{
sys_event_flag.error("sys_event_flag_create(): unknown protocol (0x%x)", protocol);
2015-07-20 00:29:40 +03:00
return CELL_EINVAL;
2014-08-08 19:55:12 +04:00
}
2015-09-15 19:23:17 +03:00
if (attr->pshared != SYS_SYNC_NOT_PROCESS_SHARED || attr->ipc_key || attr->flags)
{
sys_event_flag.error("sys_event_flag_create(): unknown attributes (pshared=0x%x, ipc_key=0x%llx, flags=0x%x)", attr->pshared, attr->ipc_key, attr->flags);
2014-08-08 19:55:12 +04:00
return CELL_EINVAL;
}
2014-08-08 19:55:12 +04:00
2015-03-06 00:29:05 +03:00
const u32 type = attr->type;
2015-07-20 00:29:40 +03:00
if (type != SYS_SYNC_WAITER_SINGLE && type != SYS_SYNC_WAITER_MULTIPLE)
2014-08-08 19:55:12 +04:00
{
sys_event_flag.error("sys_event_flag_create(): unknown type (0x%x)", type);
2015-07-20 00:29:40 +03:00
return CELL_EINVAL;
2014-08-08 19:55:12 +04:00
}
2017-02-03 19:27:03 +03:00
if (const u32 _id = idm::make<lv2_obj, lv2_event_flag>(protocol, type, attr->name_u64, init))
{
*id = _id;
return CELL_OK;
}
2014-08-08 19:55:12 +04:00
2017-02-03 19:27:03 +03:00
return CELL_EAGAIN;
2014-08-08 19:55:12 +04:00
}
2017-02-03 19:27:03 +03:00
error_code sys_event_flag_destroy(u32 id)
2014-08-08 19:55:12 +04:00
{
sys_event_flag.warning("sys_event_flag_destroy(id=0x%x)", id);
2015-03-06 00:29:05 +03:00
2017-02-03 19:27:03 +03:00
const auto flag = idm::withdraw<lv2_obj, lv2_event_flag>(id, [&](lv2_event_flag& flag) -> CellError
{
if (flag.waiters)
{
return CELL_EBUSY;
}
2014-08-08 19:55:12 +04:00
2017-02-03 19:27:03 +03:00
return {};
});
2014-08-08 19:55:12 +04:00
2017-02-03 19:27:03 +03:00
if (!flag)
2015-03-06 00:29:05 +03:00
{
return CELL_ESRCH;
}
2017-02-03 19:27:03 +03:00
if (flag.ret)
2014-08-08 19:55:12 +04:00
{
2017-02-03 19:27:03 +03:00
return flag.ret;
2014-08-08 19:55:12 +04:00
}
return CELL_OK;
}
2017-02-03 19:27:03 +03:00
error_code sys_event_flag_wait(ppu_thread& ppu, u32 id, u64 bitptn, u32 mode, vm::ptr<u64> result, u64 timeout)
2014-08-08 19:55:12 +04:00
{
sys_event_flag.trace("sys_event_flag_wait(id=0x%x, bitptn=0x%llx, mode=0x%x, result=*0x%x, timeout=0x%llx)", id, bitptn, mode, result, timeout);
2015-03-06 00:29:05 +03:00
2017-02-03 19:27:03 +03:00
// Fix function arguments for external access
2017-02-06 21:36:46 +03:00
ppu.gpr[3] = -1;
ppu.gpr[4] = bitptn;
ppu.gpr[5] = mode;
2017-02-03 19:27:03 +03:00
ppu.gpr[6] = 0;
2015-03-06 00:29:05 +03:00
2017-02-03 19:27:03 +03:00
// Always set result
if (result) *result = ppu.gpr[6];
2014-08-08 19:55:12 +04:00
if (!lv2_event_flag::check_mode(mode))
2014-08-08 19:55:12 +04:00
{
sys_event_flag.error("sys_event_flag_wait(): unknown mode (0x%x)", mode);
2015-07-20 00:29:40 +03:00
return CELL_EINVAL;
2014-08-08 19:55:12 +04:00
}
2017-02-03 19:27:03 +03:00
const auto flag = idm::get<lv2_obj, lv2_event_flag>(id, [&](lv2_event_flag& flag) -> CellError
{
if (flag.pattern.atomic_op(lv2_event_flag::check_pattern, bitptn, mode, &ppu.gpr[6]))
{
// TODO: is it possible to return EPERM in this case?
return {};
}
semaphore_lock lock(flag.mutex);
if (flag.pattern.atomic_op(lv2_event_flag::check_pattern, bitptn, mode, &ppu.gpr[6]))
{
return {};
}
if (flag.type == SYS_SYNC_WAITER_SINGLE && flag.sq.size())
{
return CELL_EPERM;
}
flag.waiters++;
flag.sq.emplace_back(&ppu);
flag.sleep(ppu, timeout);
2017-02-03 19:27:03 +03:00
return CELL_EBUSY;
});
2015-03-06 00:29:05 +03:00
2017-02-03 19:27:03 +03:00
if (!flag)
2015-03-06 00:29:05 +03:00
{
return CELL_ESRCH;
}
2014-08-08 19:55:12 +04:00
2017-02-03 19:27:03 +03:00
if (flag.ret)
2015-03-06 00:29:05 +03:00
{
2017-02-03 19:27:03 +03:00
if (flag.ret != CELL_EBUSY)
{
return flag.ret;
}
2015-03-06 00:29:05 +03:00
}
2017-02-03 19:27:03 +03:00
else
2014-08-08 19:55:12 +04:00
{
2017-02-03 19:27:03 +03:00
if (result) *result = ppu.gpr[6];
2015-07-20 00:29:40 +03:00
return CELL_OK;
}
2014-08-08 19:55:12 +04:00
while (!ppu.state.test_and_reset(cpu_flag::signal))
2015-07-20 00:29:40 +03:00
{
if (timeout)
2015-03-06 00:29:05 +03:00
{
const u64 passed = get_system_time() - ppu.start_time;
2015-07-20 00:29:40 +03:00
if (passed >= timeout)
2015-03-07 01:10:04 +03:00
{
2017-02-03 19:27:03 +03:00
semaphore_lock lock(flag->mutex);
if (!flag->unqueue(flag->sq, &ppu))
{
timeout = 0;
continue;
}
2015-07-20 00:29:40 +03:00
2017-02-03 19:27:03 +03:00
flag->waiters--;
2017-02-06 21:36:46 +03:00
ppu.gpr[3] = CELL_ETIMEDOUT;
ppu.gpr[6] = flag->pattern;
break;
2015-03-07 01:10:04 +03:00
}
2017-02-03 19:27:03 +03:00
thread_ctrl::wait_for(timeout - passed);
2014-08-08 19:55:12 +04:00
}
2015-07-20 00:29:40 +03:00
else
2014-08-08 19:55:12 +04:00
{
2017-02-03 19:27:03 +03:00
thread_ctrl::wait();
2014-08-08 19:55:12 +04:00
}
2015-03-06 00:29:05 +03:00
}
2015-07-20 00:29:40 +03:00
ppu.test_state();
2017-02-03 19:27:03 +03:00
if (result) *result = ppu.gpr[6];
2017-02-06 21:36:46 +03:00
return not_an_error(ppu.gpr[3]);
2014-08-08 19:55:12 +04:00
}
2017-02-03 19:27:03 +03:00
error_code sys_event_flag_trywait(u32 id, u64 bitptn, u32 mode, vm::ptr<u64> result)
2014-08-08 19:55:12 +04:00
{
sys_event_flag.trace("sys_event_flag_trywait(id=0x%x, bitptn=0x%llx, mode=0x%x, result=*0x%x)", id, bitptn, mode, result);
2014-08-08 19:55:12 +04:00
2017-02-03 19:27:03 +03:00
if (result) *result = 0;
2014-08-08 19:55:12 +04:00
if (!lv2_event_flag::check_mode(mode))
2014-08-08 19:55:12 +04:00
{
sys_event_flag.error("sys_event_flag_trywait(): unknown mode (0x%x)", mode);
2015-07-20 00:29:40 +03:00
return CELL_EINVAL;
2014-08-08 19:55:12 +04:00
}
2017-02-03 19:27:03 +03:00
u64 pattern;
const auto flag = idm::check<lv2_obj, lv2_event_flag>(id, [&](lv2_event_flag& flag)
{
return flag.pattern.atomic_op(lv2_event_flag::check_pattern, bitptn, mode, &pattern);
});
2014-08-08 19:55:12 +04:00
2017-02-03 19:27:03 +03:00
if (!flag)
2015-03-06 00:29:05 +03:00
{
return CELL_ESRCH;
}
2014-08-08 19:55:12 +04:00
2017-02-03 19:27:03 +03:00
if (!flag.ret)
2015-03-06 00:29:05 +03:00
{
2017-02-03 19:27:03 +03:00
return not_an_error(CELL_EBUSY);
2014-08-08 19:55:12 +04:00
}
2017-02-03 19:27:03 +03:00
if (result) *result = pattern;
return CELL_OK;
2014-08-08 19:55:12 +04:00
}
2017-02-03 19:27:03 +03:00
error_code sys_event_flag_set(u32 id, u64 bitptn)
2014-08-08 19:55:12 +04:00
{
2017-02-03 19:27:03 +03:00
// Warning: may be called from SPU thread.
sys_event_flag.trace("sys_event_flag_set(id=0x%x, bitptn=0x%llx)", id, bitptn);
2014-08-08 19:55:12 +04:00
2017-02-03 19:27:03 +03:00
const auto flag = idm::get<lv2_obj, lv2_event_flag>(id);
2014-08-08 19:55:12 +04:00
2017-02-03 19:27:03 +03:00
if (!flag)
2014-08-08 19:55:12 +04:00
{
2015-03-06 00:29:05 +03:00
return CELL_ESRCH;
2014-08-08 19:55:12 +04:00
}
2015-03-06 00:29:05 +03:00
2017-02-03 19:27:03 +03:00
if ((flag->pattern & bitptn) == bitptn)
2015-03-06 00:29:05 +03:00
{
2017-02-03 19:27:03 +03:00
return CELL_OK;
}
2017-02-06 21:36:46 +03:00
if (true)
2017-02-03 19:27:03 +03:00
{
2017-02-06 21:36:46 +03:00
semaphore_lock lock(flag->mutex);
// Sort sleep queue in required order
if (flag->protocol != SYS_SYNC_FIFO)
2017-02-03 19:27:03 +03:00
{
2017-02-06 21:36:46 +03:00
std::stable_sort(flag->sq.begin(), flag->sq.end(), [](cpu_thread* a, cpu_thread* b)
{
return static_cast<ppu_thread*>(a)->prio < static_cast<ppu_thread*>(b)->prio;
});
}
2017-02-03 19:27:03 +03:00
2017-02-06 21:36:46 +03:00
// Process all waiters in single atomic op
const u32 count = flag->pattern.atomic_op([&](u64& value)
2017-02-06 21:36:46 +03:00
{
value |= bitptn;
u32 count = 0;
2017-02-03 19:27:03 +03:00
2017-02-06 21:36:46 +03:00
for (auto cpu : flag->sq)
{
auto& ppu = static_cast<ppu_thread&>(*cpu);
const u64 pattern = ppu.gpr[4];
const u64 mode = ppu.gpr[5];
if (lv2_event_flag::check_pattern(value, pattern, mode, &ppu.gpr[6]))
{
ppu.gpr[3] = CELL_OK;
count++;
2017-02-06 21:36:46 +03:00
}
}
return count;
2017-02-06 21:36:46 +03:00
});
if (!count)
{
return CELL_OK;
}
2017-02-06 21:36:46 +03:00
// Remove waiters
const auto tail = std::remove_if(flag->sq.begin(), flag->sq.end(), [&](cpu_thread* cpu)
2017-02-03 19:27:03 +03:00
{
auto& ppu = static_cast<ppu_thread&>(*cpu);
2017-02-06 21:36:46 +03:00
if (ppu.gpr[3] == CELL_OK)
{
flag->waiters--;
flag->awake(ppu);
return true;
}
2017-02-03 19:27:03 +03:00
2017-02-06 21:36:46 +03:00
return false;
});
flag->sq.erase(tail, flag->sq.end());
}
2015-03-06 00:29:05 +03:00
2014-08-08 19:55:12 +04:00
return CELL_OK;
}
2017-02-03 19:27:03 +03:00
error_code sys_event_flag_clear(u32 id, u64 bitptn)
2014-08-08 19:55:12 +04:00
{
sys_event_flag.trace("sys_event_flag_clear(id=0x%x, bitptn=0x%llx)", id, bitptn);
2015-03-06 00:29:05 +03:00
2017-02-03 19:27:03 +03:00
const auto flag = idm::check<lv2_obj, lv2_event_flag>(id, [&](lv2_event_flag& flag)
{
flag.pattern &= bitptn;
});
2015-03-06 00:29:05 +03:00
2017-02-03 19:27:03 +03:00
if (!flag)
2015-03-06 00:29:05 +03:00
{
return CELL_ESRCH;
}
2014-08-08 19:55:12 +04:00
return CELL_OK;
}
2017-02-06 21:36:46 +03:00
error_code sys_event_flag_cancel(ppu_thread& ppu, u32 id, vm::ptr<u32> num)
2014-08-08 19:55:12 +04:00
{
sys_event_flag.trace("sys_event_flag_cancel(id=0x%x, num=*0x%x)", id, num);
2014-08-08 19:55:12 +04:00
2017-02-03 19:27:03 +03:00
if (num) *num = 0;
2014-08-08 19:55:12 +04:00
2017-02-03 19:27:03 +03:00
const auto flag = idm::get<lv2_obj, lv2_event_flag>(id);
2015-03-06 00:29:05 +03:00
2017-02-03 19:27:03 +03:00
if (!flag)
2014-08-08 19:55:12 +04:00
{
2015-03-06 00:29:05 +03:00
return CELL_ESRCH;
2014-08-08 19:55:12 +04:00
}
2017-02-06 21:36:46 +03:00
u32 value = 0;
{
semaphore_lock lock(flag->mutex);
2017-02-03 19:27:03 +03:00
2017-02-06 21:36:46 +03:00
// Get current pattern
const u64 pattern = flag->pattern;
2014-08-08 19:55:12 +04:00
2017-02-06 21:36:46 +03:00
// Set count
value = ::size32(flag->sq);
2015-07-20 00:29:40 +03:00
2017-02-06 21:36:46 +03:00
// Signal all threads to return CELL_ECANCELED
while (auto thread = flag->schedule<ppu_thread>(flag->sq, flag->protocol))
{
auto& ppu = static_cast<ppu_thread&>(*thread);
2015-07-20 00:29:40 +03:00
2017-02-06 21:36:46 +03:00
ppu.gpr[3] = CELL_ECANCELED;
ppu.gpr[6] = pattern;
2015-07-20 00:29:40 +03:00
2017-02-06 21:36:46 +03:00
flag->waiters--;
flag->awake(ppu);
}
2015-03-11 18:30:50 +03:00
}
2015-07-20 00:29:40 +03:00
ppu.test_state();
2017-02-06 21:36:46 +03:00
if (num) *num = value;
2014-08-08 19:55:12 +04:00
return CELL_OK;
}
2017-02-03 19:27:03 +03:00
error_code sys_event_flag_get(u32 id, vm::ptr<u64> flags)
2014-08-08 19:55:12 +04:00
{
sys_event_flag.trace("sys_event_flag_get(id=0x%x, flags=*0x%x)", id, flags);
2015-03-06 00:29:05 +03:00
if (!flags)
{
return CELL_EFAULT;
}
2017-02-03 19:27:03 +03:00
const auto flag = idm::check<lv2_obj, lv2_event_flag>(id, [](lv2_event_flag& flag)
2015-03-06 00:29:05 +03:00
{
2017-02-03 19:27:03 +03:00
return +flag.pattern;
});
2015-03-06 00:29:05 +03:00
2017-02-03 19:27:03 +03:00
if (!flag)
{
*flags = 0;
2015-03-06 00:29:05 +03:00
return CELL_ESRCH;
}
2017-02-03 19:27:03 +03:00
*flags = flag.ret;
2014-08-08 19:55:12 +04:00
return CELL_OK;
2015-03-06 00:29:05 +03:00
}