feat(libc): implement alarm #2765

Merged
TheAggie merged 2 commits from upstream-pr/2765 into main 2026-10-10 23:40:26 +00:00
TheAggie commented 2026-10-10 13:15:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-10T13:15:43Z.


What

RetroArch (PPSA99169) stops at alarm on current main:

FAIL: unresolved ELF import DQXJraCc1rA from libSceLibcInternal.prx

This implements it, following FreeBSD.

  • alarm(seconds) arms one process-wide real-time alarm and returns the seconds left on the previous one. Any partial second counts, as in FreeBSD's alarm.c, which adds one when it_value.tv_usec is non-zero. alarm(0) cancels the pending alarm.
  • When the alarm expires, SIGALRM's disposition as recorded by sigaction/signal applies:
    • ignored: nothing happens;
    • default: the process terminates, as on FreeBSD (exit status 128 + 14, with a line on stderr);
    • a guest handler: it throws alarm: delivering SIGALRM to a guest handler is not implemented, because the signal layer has no asynchronous delivery to a guest handler yet.
  • The timer and the disposition lookup are in libkernel next to the signal dispositions (GuestAlarm_nid_no_patch). libSceLibcInternal, which titles import alarm from, forwards to it.

Tested

Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release.

  • guest_signal_tests: alarm(0) with nothing pending returns 0; arming 5 and then re-arming 3 returns 5; cancelling returns 3, then 0. With SIGALRM ignored, a 1-second alarm expires during a 1.5 s sleep without ending the process, and nothing is left pending.
  • RetroArch, relinked against main cf8b129e2: on main it stops at the line above. With this branch it gets past alarm and stops at FAIL: unresolved ELF import E4wZaG1zSFc from libSceLibcInternal.prx (dirname, closed #1761).
  • Based on main cf8b129e2. Full suite: 550 of 554 pass, 1 skipped. agc_driver_prepared_shaders_debug_failure and guest_memory fail the same way on clean main on this machine. agc_driver_prepared_shaders failed under the parallel run and passes alone (twice). It was run from the generated CTestTestfile.cmake commands because antivirus blocks ctest.exe on this machine.
  • tools/check_conventions.py passes.

The default-disposition and guest-handler paths end the process, so they have no in-process test. Not tested on Linux or macOS.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4811474179 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2765). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-10T13:15:43Z. --- ### What RetroArch (PPSA99169) stops at `alarm` on current `main`: ``` FAIL: unresolved ELF import DQXJraCc1rA from libSceLibcInternal.prx ``` This implements it, following FreeBSD. - `alarm(seconds)` arms one process-wide real-time alarm and returns the seconds left on the previous one. Any partial second counts, as in FreeBSD's `alarm.c`, which adds one when `it_value.tv_usec` is non-zero. `alarm(0)` cancels the pending alarm. - When the alarm expires, SIGALRM's disposition as recorded by `sigaction`/`signal` applies: - ignored: nothing happens; - default: the process terminates, as on FreeBSD (exit status 128 + 14, with a line on stderr); - a guest handler: it throws `alarm: delivering SIGALRM to a guest handler is not implemented`, because the signal layer has no asynchronous delivery to a guest handler yet. - The timer and the disposition lookup are in libkernel next to the signal dispositions (`GuestAlarm_nid_no_patch`). libSceLibcInternal, which titles import `alarm` from, forwards to it. ### Tested Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. - `guest_signal_tests`: `alarm(0)` with nothing pending returns 0; arming 5 and then re-arming 3 returns 5; cancelling returns 3, then 0. With SIGALRM ignored, a 1-second alarm expires during a 1.5 s sleep without ending the process, and nothing is left pending. - RetroArch, relinked against `main` cf8b129e2: on `main` it stops at the line above. With this branch it gets past `alarm` and stops at `FAIL: unresolved ELF import E4wZaG1zSFc from libSceLibcInternal.prx` (`dirname`, closed #1761). - Based on `main` cf8b129e2. Full suite: 550 of 554 pass, 1 skipped. `agc_driver_prepared_shaders_debug_failure` and `guest_memory` fail the same way on clean `main` on this machine. `agc_driver_prepared_shaders` failed under the parallel run and passes alone (twice). It was run from the generated `CTestTestfile.cmake` commands because antivirus blocks `ctest.exe` on this machine. - `tools/check_conventions.py` passes. The default-disposition and guest-handler paths end the process, so they have no in-process test. Not tested on Linux or macOS. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-10 13:16:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T13:16:06Z.


Progress report (181420f → e5ae8df)

➖ System libraries: 85.66% (+0.0%, +1 implemented functions)

✅ 1 implemented
Library Name
libSceLibcInternal alarm_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6097890571 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2765#issuecomment-6097890571). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T13:16:06Z. --- <!-- progress-report --> ### Progress report (181420f → e5ae8df) ➖ **System libraries**: 85.66% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libSceLibcInternal | `alarm_nid_postfix` | </details>
TheAggie commented 2026-10-10 13:39:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-10T13:39:46Z.


A second title stops at alarm on main. I triaged the whole homebrew catalog on main cf8b129e2 (Windows 11, MinGW-w64 GCC 15.2.0; each release ZIP checked against its catalog sha256, relinked with relinker --windows --registry, run for 12 s):

Title main with this PR
RetroArch (PPSA99169) DQXJraCc1rA (alarm) E4wZaG1zSFc (dirname)
Porpoise (PPSA99764) DQXJraCc1rA (alarm) E4wZaG1zSFc (dirname)

The "with this PR" column comes from one build of main with this PR plus #1872, #2478 and #2024, which add unrelated imports (pthread_set_name_np, dup/dup2, utime).

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6098084439 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2765#issuecomment-6098084439). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-10T13:39:46Z. --- A second title stops at `alarm` on `main`. I triaged the whole homebrew catalog on `main` cf8b129e2 (Windows 11, MinGW-w64 GCC 15.2.0; each release ZIP checked against its catalog sha256, relinked with `relinker --windows --registry`, run for 12 s): | Title | `main` | with this PR | |---|---|---| | RetroArch (PPSA99169) | `DQXJraCc1rA` (`alarm`) | `E4wZaG1zSFc` (`dirname`) | | Porpoise (PPSA99764) | `DQXJraCc1rA` (`alarm`) | `E4wZaG1zSFc` (`dirname`) | The "with this PR" column comes from one build of `main` with this PR plus #1872, #2478 and #2024, which add unrelated imports (`pthread_set_name_np`, `dup`/`dup2`, `utime`). AI-assisted (Claude Code).
adriadam10 commented 2026-10-10 22:04:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T22:04:20Z.

Review: CHANGES_REQUESTED


Thanks, alarm is a real next stop for RetroArch/Porpoise. One blocker, found on Linux:

  • guest_signals hangs on Linux (killed after 455 s by ctest); on main it passes. All checks finish, then the process never exits. alarmLock/alarmChanged are function-local-namespace statics, and RunAlarms is a detached thread parked in alarmChanged.wait(lock) (core/libs/prx/libkernel/System/src/Signals.cpp:109). At exit the static std::condition_variable is destroyed with a waiter still on it, and glibc's pthread_cond_destroy blocks forever. A 10-line program with a detached thread waiting on a global condition variable reproduces it. MinGW doesn't block there, which is why it passed on your machine. This would also hang any guest process that calls alarm and then exits normally.
    Fix: don't destroy the state at exit (e.g. heap-allocate the mutex/condition variable/deadline and leak them), or make the thread stoppable and joined; then rerun guest_signals on Linux.
  • Once rebased: #3047 also edits the includes of Signals.cpp (lines 7-8); whichever lands second needs a trivial rebase.

The rest matches FreeBSD's alarm.c (round-up remaining seconds, alarm(0) cancels) and the new test pins those values.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5480965125 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2765#pullrequestreview-5480965125). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T22:04:20Z. **Review:** CHANGES_REQUESTED --- Thanks, `alarm` is a real next stop for RetroArch/Porpoise. One blocker, found on Linux: - `guest_signals` hangs on Linux (killed after 455 s by ctest); on `main` it passes. All checks finish, then the process never exits. `alarmLock`/`alarmChanged` are function-local-namespace statics, and `RunAlarms` is a detached thread parked in `alarmChanged.wait(lock)` (`core/libs/prx/libkernel/System/src/Signals.cpp:109`). At exit the static `std::condition_variable` is destroyed with a waiter still on it, and glibc's `pthread_cond_destroy` blocks forever. A 10-line program with a detached thread waiting on a global condition variable reproduces it. MinGW doesn't block there, which is why it passed on your machine. This would also hang any guest process that calls `alarm` and then exits normally. Fix: don't destroy the state at exit (e.g. heap-allocate the mutex/condition variable/deadline and leak them), or make the thread stoppable and joined; then rerun `guest_signals` on Linux. - Once rebased: #3047 also edits the includes of `Signals.cpp` (lines 7-8); whichever lands second needs a trivial rebase. The rest matches FreeBSD's `alarm.c` (round-up remaining seconds, `alarm(0)` cancels) and the new test pins those values.
babyhuey commented 2026-10-10 22:12:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-10T22:12:05Z.


Linux check, since this was only run on Windows: 018097ec merged onto main 262c0316 (clean), Ubuntu 24.04, GCC 13, Release.

guest_signals hangs on Linux. On main the same binary exits 0 at once; with this PR it never exits (killed after 45 s with SIGKILL; plain timeout can't stop it because the test ignores signal 15). A marker printed at the end of main() does show up, so every check passes and the hang is at process exit. At that point both threads sit in futex_do_wait: the main thread and the detached RunAlarms thread.

That fits static destruction: alarmChanged and alarmLock are namespace-scope statics, and glibc's pthread_cond_destroy blocks while the detached thread is still a waiter on the condition variable. A title that calls alarm and then returns from main would hang the same way on Linux. Keeping the alarm state alive for the whole process should avoid it, for example putting the mutex, condition variable and deadline in a struct allocated once with new and never destroyed, or having RunAlarms take no lock that is destroyed at exit.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6102725818 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2765#issuecomment-6102725818). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-10T22:12:05Z. --- Linux check, since this was only run on Windows: 018097ec merged onto `main` 262c0316 (clean), Ubuntu 24.04, GCC 13, Release. `guest_signals` hangs on Linux. On `main` the same binary exits 0 at once; with this PR it never exits (killed after 45 s with `SIGKILL`; plain `timeout` can't stop it because the test ignores signal 15). A marker printed at the end of `main()` does show up, so every check passes and the hang is at process exit. At that point both threads sit in `futex_do_wait`: the main thread and the detached `RunAlarms` thread. That fits static destruction: `alarmChanged` and `alarmLock` are namespace-scope statics, and glibc's `pthread_cond_destroy` blocks while the detached thread is still a waiter on the condition variable. A title that calls `alarm` and then returns from `main` would hang the same way on Linux. Keeping the alarm state alive for the whole process should avoid it, for example putting the mutex, condition variable and deadline in a struct allocated once with `new` and never destroyed, or having `RunAlarms` take no lock that is destroyed at exit.
github-actions[bot] commented 2026-10-10 23:27:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T23:27:18Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#3047 core/libs/prx/libkernel/System/src/Signals.cpp L7-8
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103309018 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2765#issuecomment-6103309018). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T23:27:18Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #3047 | `core/libs/prx/libkernel/System/src/Signals.cpp` [L7-8](https://github.com/boykopovar/AnyPS5/blob/c07638834db522535c6c436c0e77fa97f8fbb328/core/libs/prx/libkernel/System/src/Signals.cpp#L7-L8) | | |
TheAggie commented 2026-10-10 23:28:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-10T23:28:01Z.


Thanks @adriadam10 and @babyhuey for running it on Linux. Fixed in e5ae8dfc:

  • The mutex, condition variable, deadline and started flag now live in one AlarmState allocated once with new and never destroyed.
  • So nothing the detached RunAlarms thread waits on is torn down at exit.

I also rebased onto current main (no conflicts; #3047 hasn't landed yet, so the include change will be a one-line rebase whichever goes second).

guest_signals passes on Windows. I don't have a Linux machine here to confirm the exit hang is gone. Could one of you rerun it there?

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103314180 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2765#issuecomment-6103314180). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-10T23:28:01Z. --- Thanks @adriadam10 and @babyhuey for running it on Linux. Fixed in e5ae8dfc: - The mutex, condition variable, deadline and started flag now live in one `AlarmState` allocated once with `new` and never destroyed. - So nothing the detached `RunAlarms` thread waits on is torn down at exit. I also rebased onto current `main` (no conflicts; #3047 hasn't landed yet, so the include change will be a one-line rebase whichever goes second). `guest_signals` passes on Windows. I don't have a Linux machine here to confirm the exit hang is gone. Could one of you rerun it there? AI-assisted (Claude Code).
babyhuey commented 2026-10-10 23:32:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-10T23:32:37Z.


Confirmed on Linux: e5ae8dfc merged onto main ad336923 (clean), Ubuntu 24.04, GCC 13, Release. guest_signals now exits 0 in 1 to 2 s on each of five runs, and passes 5/5 under ctest --repeat until-fail:5. Porpoise (PPSA99764) gets past alarm and stops at undefined symbol: 2eQpqTjJ5Y4 (asinh).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103347885 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2765#issuecomment-6103347885). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-10T23:32:37Z. --- Confirmed on Linux: e5ae8dfc merged onto `main` ad336923 (clean), Ubuntu 24.04, GCC 13, Release. `guest_signals` now exits 0 in 1 to 2 s on each of five runs, and passes 5/5 under `ctest --repeat until-fail:5`. Porpoise (PPSA99764) gets past `alarm` and stops at `undefined symbol: 2eQpqTjJ5Y4` (`asinh`).
adriadam10 commented 2026-10-10 23:40:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T23:40:24Z.

Review: APPROVED


Checked on Linux: build and guest_signals pass on the merge with main (and fail to link without the change); the exit hang is gone, also confirmed by @babyhuey on Ubuntu. alarm semantics match FreeBSD alarm.c (rounding up partial seconds, 0 cancels), and the unmodelled guest-handler path throws. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481211736 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2765#pullrequestreview-5481211736). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T23:40:24Z. **Review:** APPROVED --- Checked on Linux: build and `guest_signals` pass on the merge with main (and fail to link without the change); the exit hang is gone, also confirmed by @babyhuey on Ubuntu. `alarm` semantics match FreeBSD `alarm.c` (rounding up partial seconds, 0 cancels), and the unmodelled guest-handler path throws. Merging.
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!2765
No description provided.