#include "stdafx.h" #include "Emu/Memory/Memory.h" #include "Emu/System.h" #include "Emu/IdManager.h" #include "Emu/Cell/ErrorCodes.h" #include "Emu/Cell/PPUThread.h" #include "sys_semaphore.h" namespace vm { using namespace ps3; } logs::channel sys_semaphore("sys_semaphore", logs::level::notice); extern u64 get_system_time(); error_code sys_semaphore_create(vm::ptr sem_id, vm::ptr attr, s32 initial_val, s32 max_val) { sys_semaphore.warning("sys_semaphore_create(sem_id=*0x%x, attr=*0x%x, initial_val=%d, max_val=%d)", sem_id, attr, initial_val, max_val); if (!sem_id || !attr) { return CELL_EFAULT; } if (max_val <= 0 || initial_val > max_val || initial_val < 0) { sys_semaphore.error("sys_semaphore_create(): invalid parameters (initial_val=%d, max_val=%d)", initial_val, max_val); return CELL_EINVAL; } const u32 protocol = attr->protocol; if (protocol != SYS_SYNC_FIFO && protocol != SYS_SYNC_PRIORITY && protocol != SYS_SYNC_PRIORITY_INHERIT) { sys_semaphore.error("sys_semaphore_create(): unknown protocol (0x%x)", protocol); return CELL_EINVAL; } if (attr->pshared != SYS_SYNC_NOT_PROCESS_SHARED || attr->ipc_key || attr->flags) { sys_semaphore.error("sys_semaphore_create(): unknown attributes (pshared=0x%x, ipc_key=0x%x, flags=0x%x)", attr->pshared, attr->ipc_key, attr->flags); return CELL_EINVAL; } if (const u32 id = idm::make(protocol, attr->name_u64, max_val, initial_val)) { *sem_id = id; return CELL_OK; } return CELL_EAGAIN; } error_code sys_semaphore_destroy(u32 sem_id) { sys_semaphore.warning("sys_semaphore_destroy(sem_id=0x%x)", sem_id); const auto sem = idm::withdraw(sem_id, [](lv2_sema& sema) -> CellError { if (sema.val < 0) { return CELL_EBUSY; } return {}; }); if (!sem) { return CELL_ESRCH; } if (sem.ret) { return sem.ret; } return CELL_OK; } error_code sys_semaphore_wait(ppu_thread& ppu, u32 sem_id, u64 timeout) { sys_semaphore.trace("sys_semaphore_wait(sem_id=0x%x, timeout=0x%llx)", sem_id, timeout); const u64 start_time = get_system_time(); const auto sem = idm::get(sem_id, [&](lv2_sema& sema) { const s32 val = sema.val; if (val > 0) { if (sema.val.compare_and_swap_test(val, val - 1)) { return true; } } semaphore_lock lock(sema.mutex); if (sema.val-- <= 0) { sema.sq.emplace_back(&ppu); return false; } return true; }); if (!sem) { return CELL_ESRCH; } if (sem.ret) { return CELL_OK; } // SLEEP while (!ppu.state.test_and_reset(cpu_flag::signal)) { if (timeout) { const u64 passed = get_system_time() - start_time; if (passed >= timeout) { semaphore_lock lock(sem->mutex); const s32 val = sem->val.fetch_op([](s32& val) { if (val < 0) { val++; } }); if (val >= 0) { timeout = 0; continue; } verify(HERE), sem->unqueue(sem->sq, &ppu); return not_an_error(CELL_ETIMEDOUT); } thread_ctrl::wait_for(timeout - passed); } else { thread_ctrl::wait(); } } return CELL_OK; } error_code sys_semaphore_trywait(u32 sem_id) { sys_semaphore.trace("sys_semaphore_trywait(sem_id=0x%x)", sem_id); const auto sem = idm::check(sem_id, [&](lv2_sema& sema) { const s32 val = sema.val; if (val > 0) { if (sema.val.compare_and_swap_test(val, val - 1)) { return true; } } return false; }); if (!sem) { return CELL_ESRCH; } if (!sem.ret) { return not_an_error(CELL_EBUSY); } return CELL_OK; } error_code sys_semaphore_post(u32 sem_id, s32 count) { sys_semaphore.trace("sys_semaphore_post(sem_id=0x%x, count=%d)", sem_id, count); if (count < 0) { return CELL_EINVAL; } const auto sem = idm::get(sem_id, [&](lv2_sema& sema) { const s32 val = sema.val; if (val >= 0 && count <= sema.max - val) { if (sema.val.compare_and_swap_test(val, val + count)) { return true; } } return false; }); if (!sem) { return CELL_ESRCH; } if (sem.ret) { return CELL_OK; } else { semaphore_lock lock(sem->mutex); const s32 val = sem->val.fetch_op([=](s32& val) { if (val + s64{count} <= sem->max) { val += count; } }); if (val + s64{count} > sem->max) { return not_an_error(CELL_EBUSY); } // Wake threads for (s32 i = std::min(-std::min(val, 0), count); i > 0; i--) { const auto cpu = verify(HERE, sem->schedule(sem->sq, sem->protocol)); cpu->set_signal(); } } return CELL_OK; } error_code sys_semaphore_get_value(u32 sem_id, vm::ptr count) { sys_semaphore.trace("sys_semaphore_get_value(sem_id=0x%x, count=*0x%x)", sem_id, count); if (!count) { return CELL_EFAULT; } if (!idm::check(sem_id, [=](lv2_sema& sema) { *count = std::max(0, sema.val); })) { return CELL_ESRCH; } return CELL_OK; }