fix(kernel,audioout,relinker): thread/mutex correctness fixes and --windows-gui #25

Merged
QDarkRex merged 8 commits from upstream-pr/25 into main 2026-09-28 08:09:59 +00:00
QDarkRex commented 2026-09-27 05:45:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QDarkRex on 2026-09-27T05:45:41Z.


Summary

Fixes a set of verified bugs in libkernel, libSceAudioOut and the relinker CLI, and adds a --windows-gui relinker option and sceKernelDeleteSema.

libkernel (threads, mutexes, condition variables)

  • Deadlock in scePthreadCondTimedwait (normal/error-check mutexes): _owner was not restored after the wait. If another thread locked and unlocked the mutex during the wait, the waiter woke up owning the mutex while _owner was empty. Its next scePthreadMutexUnlock returned EPERM, leaving the mutex locked forever (lock → timedwait → unlock pattern).
  • Wrong timeout code: SCE_KERNEL_ERROR_ETIMEDOUT in Cond.cpp was 0x80020062; it is 0x8002003C (FreeBSD ETIMEDOUT = 60, matching Semaphore.hpp).
  • scePthreadSelf() returned nullptr on Linux, and on both platforms for threads not created by scePthreadCreate (including the main thread). Such threads now get a lazily adopted, non-joinable handle (scePthreadJoin/Detach on it return EINVAL).
  • Linux scePthreadExit skipped FinishThread, so scePthreadJoin got a garbage return value. Cleanup now runs from an RAII guard, so it survives glibc forced unwinding.
  • Linux detached threads were never freed. They now use the same reference count as on Windows.
  • Recursive mutex unlock by a non-owner, or with a zero count, was UB (std::recursive_timed_mutex::unlock). It now returns SCE_KERNEL_ERROR_EPERM, and _owner/_count are also saved and restored around a recursive cond wait.
  • sceKernelLseek returned int instead of off_t; offsets above INT_MAX threw.
  • sceKernelDeleteSema implemented: waiters are woken with SCE_KERNEL_ERROR_EACCES and the object is freed once they leave.

libSceAudioOut

  • sceAudioOutOpen accepted freq == 0 / len == 0; the frequency was later used as a divisor. It now returns SCE_AUDIO_OUT_ERROR_INVALID_SAMPLE_FREQ / INVALID_SIZE.

Relinker

  • --autorun needed two Enter presses (cin.ignore + cin.get). On POSIX it also returned the raw system() wait status from main, so a guest exit code of 1 (status 256) became 0.
  • New opt-in --windows-gui (requires --windows) writes IMAGE_SUBSYSTEM_WINDOWS_GUI so the game does not open a console window (TechnicalDebt: "opens the console when launched"). The default is unchanged because stderr diagnostics are invisible with the GUI subsystem.

Tests

New tests:

  • guest_thread_self: scePthreadSelf on the main and a created thread, join value via scePthreadExit, recursive unlock from a non-owner returns EPERM.
  • guest_cond_timedwait: regression for the deadlock above. I checked that it aborts with the _owner restore reverted and passes with it.
  • windows_gui: PE subsystem is 3 by default, 2 with --windows-gui, and the flag is rejected without --windows.

Results:

  • Windows, MinGW-w64 GCC 15.2.0 (winlibs x86_64-ucrt-posix-seh, r7): main passes 31/31; this branch passes 34/34.
  • The relinker also builds with MSVC 14.51 (/W4 /permissive-), and its 5 tests pass there.

Not tested on Linux. The Linux #else branches in Thread.cpp (RAII guard, reference counting, pthread_exit path) need a run there.

Notes for review

  • On Windows, scePthreadExit from an adopted thread (e.g. the main thread) now reaches _endthreadex instead of throwing. This is closer to FreeBSD semantics but untested.
  • sceKernelDeleteSema does not protect against a new call on the handle racing with the delete. There is no handle registry for semaphores, same as the rest of Semaphore.cpp.
  • Possibly related, not changed: EQUEUE_ERROR_ETIMEDOUT in Equeue.hpp is 0x80020023 (EAGAIN) rather than 0x8002003C; worth checking against real behaviour.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4651919906 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25). Originally posted by [QDarkRex](https://github.com/QDarkRex) on 2026-09-27T05:45:41Z. --- ## Summary Fixes a set of verified bugs in `libkernel`, `libSceAudioOut` and the relinker CLI, and adds a `--windows-gui` relinker option and `sceKernelDeleteSema`. ### libkernel (threads, mutexes, condition variables) - **Deadlock in `scePthreadCondTimedwait` (normal/error-check mutexes):** `_owner` was not restored after the wait. If another thread locked and unlocked the mutex during the wait, the waiter woke up owning the mutex while `_owner` was empty. Its next `scePthreadMutexUnlock` returned `EPERM`, leaving the mutex locked forever (`lock → timedwait → unlock` pattern). - **Wrong timeout code:** `SCE_KERNEL_ERROR_ETIMEDOUT` in `Cond.cpp` was `0x80020062`; it is `0x8002003C` (FreeBSD `ETIMEDOUT` = 60, matching `Semaphore.hpp`). - **`scePthreadSelf()`** returned `nullptr` on Linux, and on both platforms for threads not created by `scePthreadCreate` (including the main thread). Such threads now get a lazily adopted, non-joinable handle (`scePthreadJoin`/`Detach` on it return `EINVAL`). - **Linux `scePthreadExit`** skipped `FinishThread`, so `scePthreadJoin` got a garbage return value. Cleanup now runs from an RAII guard, so it survives glibc forced unwinding. - **Linux detached threads** were never freed. They now use the same reference count as on Windows. - **Recursive mutex unlock** by a non-owner, or with a zero count, was UB (`std::recursive_timed_mutex::unlock`). It now returns `SCE_KERNEL_ERROR_EPERM`, and `_owner`/`_count` are also saved and restored around a recursive cond wait. - **`sceKernelLseek`** returned `int` instead of `off_t`; offsets above `INT_MAX` threw. - **`sceKernelDeleteSema`** implemented: waiters are woken with `SCE_KERNEL_ERROR_EACCES` and the object is freed once they leave. ### libSceAudioOut - `sceAudioOutOpen` accepted `freq == 0` / `len == 0`; the frequency was later used as a divisor. It now returns `SCE_AUDIO_OUT_ERROR_INVALID_SAMPLE_FREQ` / `INVALID_SIZE`. ### Relinker - `--autorun` needed two Enter presses (`cin.ignore` + `cin.get`). On POSIX it also returned the raw `system()` wait status from `main`, so a guest exit code of 1 (status 256) became 0. - New opt-in `--windows-gui` (requires `--windows`) writes `IMAGE_SUBSYSTEM_WINDOWS_GUI` so the game does not open a console window (TechnicalDebt: *"opens the console when launched"*). The default is unchanged because stderr diagnostics are invisible with the GUI subsystem. ## Tests New tests: - `guest_thread_self`: `scePthreadSelf` on the main and a created thread, join value via `scePthreadExit`, recursive unlock from a non-owner returns `EPERM`. - `guest_cond_timedwait`: regression for the deadlock above. I checked that it aborts with the `_owner` restore reverted and passes with it. - `windows_gui`: PE subsystem is 3 by default, 2 with `--windows-gui`, and the flag is rejected without `--windows`. Results: - Windows, MinGW-w64 GCC 15.2.0 (winlibs `x86_64-ucrt-posix-seh`, r7): `main` passes 31/31; this branch passes **34/34**. - The relinker also builds with MSVC 14.51 (`/W4 /permissive-`), and its 5 tests pass there. **Not tested on Linux.** The Linux `#else` branches in `Thread.cpp` (RAII guard, reference counting, `pthread_exit` path) need a run there. ## Notes for review - On Windows, `scePthreadExit` from an *adopted* thread (e.g. the main thread) now reaches `_endthreadex` instead of throwing. This is closer to FreeBSD semantics but untested. - `sceKernelDeleteSema` does not protect against a *new* call on the handle racing with the delete. There is no handle registry for semaphores, same as the rest of `Semaphore.cpp`. - Possibly related, not changed: `EQUEUE_ERROR_ETIMEDOUT` in `Equeue.hpp` is `0x80020023` (EAGAIN) rather than `0x8002003C`; worth checking against real behaviour. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
boykopovar commented 2026-09-27 13:11:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by boykopovar on 2026-09-27T13:11:21Z.


PR #10 changed files in core/libs/prx/libkernel/Pthread/src/. Conflicts need to be resolved. Apparently, this affected several of your public PRs. But those changes were extremely important.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5856161640 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25#issuecomment-5856161640). Originally posted by [boykopovar](https://github.com/boykopovar) on 2026-09-27T13:11:21Z. --- PR #10 changed files in `core/libs/prx/libkernel/Pthread/src/`. Conflicts need to be resolved. Apparently, this affected several of your public PRs. But those changes were extremely important.
QDarkRex commented 2026-09-28 07:48:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QDarkRex on 2026-09-28T07:48:57Z.


Thanks for the heads-up! I merged the latest main into #25, #26 and #27 and resolved the conflicts with #10. None of them conflict now.

#25: #10 already covered the cond-wait deadlock, the ETIMEDOUT code and the mutex-unlock ownership check, so I kept your versions of Cond.cpp and Mutex.cpp. On top of your new Thread.cpp I re-applied the POSIX thread fixes: reference-counted release on join/detach/exit, rejecting self-join, and scePthreadSelf for threads the emulator didn't create. I also re-applied sceKernelDeleteSema on top of TimedWait::Condition. I also fixed scePthreadExit on Linux. pthread_exit's forced unwind ran libc.prx's __gxx_personality_v0, which aborted, so it now jumps back to the thread start routine instead, like _endthreadex on Windows.
#26: #10 already fixed the mutex EDEADLK code, so I took your version. The equeue and direct-memory error-code fixes are unchanged.
#27: Still stacked on #25 and #26. I re-applied the KernelErrors.hpp refactor to the rewritten files.

On Linux, every test passes except agc_driver, agc_driver_flip and agc_driver_pm4, which fail the same way on current main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5865710146 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25#issuecomment-5865710146). Originally posted by [QDarkRex](https://github.com/QDarkRex) on 2026-09-28T07:48:57Z. --- Thanks for the heads-up! I merged the latest main into #25, #26 and #27 and resolved the conflicts with #10. None of them conflict now. #25: #10 already covered the cond-wait deadlock, the ETIMEDOUT code and the mutex-unlock ownership check, so I kept your versions of Cond.cpp and Mutex.cpp. On top of your new Thread.cpp I re-applied the POSIX thread fixes: reference-counted release on join/detach/exit, rejecting self-join, and scePthreadSelf for threads the emulator didn't create. I also re-applied sceKernelDeleteSema on top of TimedWait::Condition. I also fixed scePthreadExit on Linux. pthread_exit's forced unwind ran libc.prx's __gxx_personality_v0, which aborted, so it now jumps back to the thread start routine instead, like _endthreadex on Windows. #26: #10 already fixed the mutex EDEADLK code, so I took your version. The equeue and direct-memory error-code fixes are unchanged. #27: Still stacked on #25 and #26. I re-applied the KernelErrors.hpp refactor to the rewritten files. On Linux, every test passes except agc_driver, agc_driver_flip and agc_driver_pm4, which fail the same way on current main.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!25
No description provided.