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 = ppu.gpr[10] = get_system_time(); const auto sem = idm::get<lv2_obj, lv2_sema>(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); sema.sleep(ppu, start_time, timeout); return false; } return true; }); if (!sem) { return CELL_ESRCH; } if (sem.ret) { return CELL_OK; } ppu.gpr[3] = CELL_OK; 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); ppu.gpr[3] = CELL_ETIMEDOUT; break; } thread_ctrl::wait_for(timeout - passed); } else { thread_ctrl::wait(); } } ppu.check_state(); return not_an_error(ppu.gpr[3]); }
error_code sys_semaphore_post(ppu_thread& ppu, 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<lv2_obj, lv2_sema>(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<s32>(-std::min<s32>(val, 0), count); i > 0; i--) { const auto cpu = verify(HERE, sem->schedule<ppu_thread>(sem->sq, sem->protocol)); sem->awake(*cpu); } } ppu.check_state(); return CELL_OK; }