| 341 | } |
| 342 | |
| 343 | error_code sys_cond_wait(ppu_thread &ppu, u32 cond_id, u64 timeout) { |
| 344 | ppu.state += cpu_flag::wait; |
| 345 | |
| 346 | sys_cond.trace("sys_cond_wait(cond_id=0x%x, timeout=%lld)", cond_id, timeout); |
| 347 | |
| 348 | // Further function result |
| 349 | ppu.gpr[3] = CELL_OK; |
| 350 | |
| 351 | auto &sstate = *ppu.optional_savestate_state; |
| 352 | |
| 353 | const auto cond = idm::get<lv2_obj, lv2_cond>( |
| 354 | cond_id, [&, notify = lv2_obj::notify_all_t()](lv2_cond &cond) -> s64 { |
| 355 | if (!ppu.loaded_from_savestate && |
| 356 | atomic_storage<u32>::load(cond.mutex->control.raw().owner) != |
| 357 | ppu.id) { |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | lv2_obj::prepare_for_sleep(ppu); |
| 362 | |
| 363 | std::lock_guard lock(cond.mutex->mutex); |
| 364 | |
| 365 | const u64 syscall_state = sstate.try_read<u64>().second; |
| 366 | sstate.clear(); |
| 367 | |
| 368 | if (ppu.loaded_from_savestate) { |
| 369 | if (syscall_state & 1) { |
| 370 | // Mutex sleep |
| 371 | ensure(!cond.mutex->try_own(ppu)); |
| 372 | } else { |
| 373 | lv2_obj::emplace(cond.sq, &ppu); |
| 374 | } |
| 375 | |
| 376 | cond.sleep(ppu, timeout); |
| 377 | return static_cast<u32>(syscall_state >> 32); |
| 378 | } |
| 379 | |
| 380 | // Register waiter |
| 381 | lv2_obj::emplace(cond.sq, &ppu); |
| 382 | |
| 383 | // Unlock the mutex |
| 384 | const u32 count = cond.mutex->lock_count.exchange(0); |
| 385 | |
| 386 | if (const auto cpu = cond.mutex->reown<ppu_thread>()) { |
| 387 | if (cpu->state & cpu_flag::again) { |
| 388 | ensure(cond.unqueue(cond.sq, &ppu)); |
| 389 | ppu.state += cpu_flag::again; |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | cond.mutex->append(cpu); |
| 394 | } |
| 395 | |
| 396 | // Sleep current thread and schedule mutex waiter |
| 397 | cond.sleep(ppu, timeout); |
| 398 | |
| 399 | // Save the recursive value |
| 400 | return count; |
no test coverage detected