rpcs3/rpcs3/Emu/Cell/lv2/sys_event_flag.cpp
Nekotekina 1b37e775be Migration to named_thread<>
Add atomic_t<>::try_dec instead of fetch_dec_sat
Add atomic_t<>::try_inc
GDBDebugServer is broken (needs rewrite)
Removed old_thread class (former named_thread)
Removed storing/rethrowing exceptions from thread
Emu.Stop doesn't inject an exception anymore
task_stack helper class removed
thread_base simplified (no shared_from_this)
thread_ctrl::spawn simplified (creates detached thread)
Implemented overrideable thread detaching logic
Disabled cellAdec, cellDmux, cellFsAio
SPUThread renamed to spu_thread
RawSPUThread removed, spu_thread used instead
Disabled deriving from ppu_thread
Partial support for thread renaming
lv2_timer... simplified, screw it
idm/fxm: butchered support for on_stop/on_init
vm: improved allocation structure (added size)
2018-10-19 22:22:35 +03:00

409 lines
7.8 KiB
C++

#include "stdafx.h"
#include "Emu/Memory/vm.h"
#include "Emu/System.h"
#include "Emu/IdManager.h"
#include "Emu/IPC.h"
#include "Emu/Cell/ErrorCodes.h"
#include "Emu/Cell/PPUThread.h"
#include "sys_event_flag.h"
#include <algorithm>
LOG_CHANNEL(sys_event_flag);
template<> DECLARE(ipc_manager<lv2_event_flag, u64>::g_ipc) {};
extern u64 get_system_time();
error_code sys_event_flag_create(vm::ptr<u32> id, vm::ptr<sys_event_flag_attribute_t> attr, u64 init)
{
sys_event_flag.warning("sys_event_flag_create(id=*0x%x, attr=*0x%x, init=0x%llx)", id, attr, init);
if (!id || !attr)
{
return CELL_EFAULT;
}
const u32 protocol = attr->protocol;
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");
if (protocol != SYS_SYNC_FIFO && protocol != SYS_SYNC_RETRY && protocol != SYS_SYNC_PRIORITY && protocol != SYS_SYNC_PRIORITY_INHERIT)
{
sys_event_flag.error("sys_event_flag_create(): unknown protocol (0x%x)", protocol);
return CELL_EINVAL;
}
const u32 type = attr->type;
if (type != SYS_SYNC_WAITER_SINGLE && type != SYS_SYNC_WAITER_MULTIPLE)
{
sys_event_flag.error("sys_event_flag_create(): unknown type (0x%x)", type);
return CELL_EINVAL;
}
if (auto error = lv2_obj::create<lv2_event_flag>(attr->pshared, attr->ipc_key, attr->flags, [&]
{
return std::make_shared<lv2_event_flag>(
attr->protocol,
attr->pshared,
attr->ipc_key,
attr->flags,
attr->type,
attr->name_u64,
init);
}))
{
return error;
}
*id = idm::last_id();
return CELL_OK;
}
error_code sys_event_flag_destroy(u32 id)
{
sys_event_flag.warning("sys_event_flag_destroy(id=0x%x)", id);
const auto flag = idm::withdraw<lv2_obj, lv2_event_flag>(id, [&](lv2_event_flag& flag) -> CellError
{
if (flag.waiters)
{
return CELL_EBUSY;
}
return {};
});
if (!flag)
{
return CELL_ESRCH;
}
if (flag.ret)
{
return flag.ret;
}
return CELL_OK;
}
error_code sys_event_flag_wait(ppu_thread& ppu, u32 id, u64 bitptn, u32 mode, vm::ptr<u64> result, u64 timeout)
{
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);
// Fix function arguments for external access
ppu.gpr[3] = -1;
ppu.gpr[4] = bitptn;
ppu.gpr[5] = mode;
ppu.gpr[6] = 0;
// Always set result
if (result) *result = ppu.gpr[6];
if (!lv2_event_flag::check_mode(mode))
{
sys_event_flag.error("sys_event_flag_wait(): unknown mode (0x%x)", mode);
return CELL_EINVAL;
}
const auto flag = idm::get<lv2_obj, lv2_event_flag>(id, [&](lv2_event_flag& flag) -> CellError
{
if (flag.pattern.atomic_op([&](u64& pat)
{
return lv2_event_flag::check_pattern(pat, bitptn, mode, &ppu.gpr[6]);
}))
{
// TODO: is it possible to return EPERM in this case?
return {};
}
std::lock_guard lock(flag.mutex);
if (flag.pattern.atomic_op([&](u64& pat)
{
return lv2_event_flag::check_pattern(pat, 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);
return CELL_EBUSY;
});
if (!flag)
{
return CELL_ESRCH;
}
if (flag.ret)
{
if (flag.ret != CELL_EBUSY)
{
return flag.ret;
}
}
else
{
if (result) *result = ppu.gpr[6];
return CELL_OK;
}
while (!ppu.state.test_and_reset(cpu_flag::signal))
{
if (ppu.is_stopped())
{
return 0;
}
if (timeout)
{
const u64 passed = get_system_time() - ppu.start_time;
if (passed >= timeout)
{
std::lock_guard lock(flag->mutex);
if (!flag->unqueue(flag->sq, &ppu))
{
timeout = 0;
continue;
}
flag->waiters--;
ppu.gpr[3] = CELL_ETIMEDOUT;
ppu.gpr[6] = flag->pattern;
break;
}
thread_ctrl::wait_for(timeout - passed);
}
else
{
thread_ctrl::wait();
}
}
if (ppu.test_stopped())
{
return 0;
}
if (result) *result = ppu.gpr[6];
return not_an_error(ppu.gpr[3]);
}
error_code sys_event_flag_trywait(u32 id, u64 bitptn, u32 mode, vm::ptr<u64> result)
{
sys_event_flag.trace("sys_event_flag_trywait(id=0x%x, bitptn=0x%llx, mode=0x%x, result=*0x%x)", id, bitptn, mode, result);
if (result) *result = 0;
if (!lv2_event_flag::check_mode(mode))
{
sys_event_flag.error("sys_event_flag_trywait(): unknown mode (0x%x)", mode);
return CELL_EINVAL;
}
u64 pattern;
const auto flag = idm::check<lv2_obj, lv2_event_flag>(id, [&](lv2_event_flag& flag)
{
return flag.pattern.atomic_op([&](u64& pat)
{
return lv2_event_flag::check_pattern(pat, bitptn, mode, &pattern);
});
});
if (!flag)
{
return CELL_ESRCH;
}
if (!flag.ret)
{
return not_an_error(CELL_EBUSY);
}
if (result) *result = pattern;
return CELL_OK;
}
error_code sys_event_flag_set(u32 id, u64 bitptn)
{
// Warning: may be called from SPU thread.
sys_event_flag.trace("sys_event_flag_set(id=0x%x, bitptn=0x%llx)", id, bitptn);
const auto flag = idm::get<lv2_obj, lv2_event_flag>(id);
if (!flag)
{
return CELL_ESRCH;
}
if ((flag->pattern & bitptn) == bitptn)
{
return CELL_OK;
}
if (true)
{
std::lock_guard lock(flag->mutex);
// Sort sleep queue in required order
if (flag->protocol != SYS_SYNC_FIFO)
{
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;
});
}
// Process all waiters in single atomic op
const u32 count = flag->pattern.atomic_op([&](u64& value)
{
value |= bitptn;
u32 count = 0;
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++;
}
}
return count;
});
if (!count)
{
return CELL_OK;
}
// Remove waiters
const auto tail = std::remove_if(flag->sq.begin(), flag->sq.end(), [&](cpu_thread* cpu)
{
auto& ppu = static_cast<ppu_thread&>(*cpu);
if (ppu.gpr[3] == CELL_OK)
{
flag->waiters--;
flag->awake(ppu);
return true;
}
return false;
});
flag->sq.erase(tail, flag->sq.end());
}
return CELL_OK;
}
error_code sys_event_flag_clear(u32 id, u64 bitptn)
{
sys_event_flag.trace("sys_event_flag_clear(id=0x%x, bitptn=0x%llx)", id, bitptn);
const auto flag = idm::check<lv2_obj, lv2_event_flag>(id, [&](lv2_event_flag& flag)
{
flag.pattern &= bitptn;
});
if (!flag)
{
return CELL_ESRCH;
}
return CELL_OK;
}
error_code sys_event_flag_cancel(ppu_thread& ppu, u32 id, vm::ptr<u32> num)
{
sys_event_flag.trace("sys_event_flag_cancel(id=0x%x, num=*0x%x)", id, num);
if (num) *num = 0;
const auto flag = idm::get<lv2_obj, lv2_event_flag>(id);
if (!flag)
{
return CELL_ESRCH;
}
u32 value = 0;
{
std::lock_guard lock(flag->mutex);
// Get current pattern
const u64 pattern = flag->pattern;
// Set count
value = ::size32(flag->sq);
// 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);
ppu.gpr[3] = CELL_ECANCELED;
ppu.gpr[6] = pattern;
flag->waiters--;
flag->awake(ppu);
}
}
if (ppu.test_stopped())
{
return 0;
}
if (num) *num = value;
return CELL_OK;
}
error_code sys_event_flag_get(u32 id, vm::ptr<u64> flags)
{
sys_event_flag.trace("sys_event_flag_get(id=0x%x, flags=*0x%x)", id, flags);
if (!flags)
{
return CELL_EFAULT;
}
const auto flag = idm::check<lv2_obj, lv2_event_flag>(id, [](lv2_event_flag& flag)
{
return +flag.pattern;
});
if (!flag)
{
*flags = 0;
return CELL_ESRCH;
}
*flags = flag.ret;
return CELL_OK;
}