fix(libkernel): handle available sem_timedwait counts first #3691

Merged
MarcZX10 merged 1 commit from upstream-pr/3691 into main 2026-10-11 22:35:18 +00:00
MarcZX10 commented 2026-10-11 15:23:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T15:23:45Z.


What

sem_timedwait now consumes an available semaphore count before validating abstime, as permitted by POSIX. A null timeout waits indefinitely when the semaphore is empty; malformed and expired timeouts still return EINVAL and ETIMEDOUT when the call would block.

This carries the available-count-before-timeout behavior from Miles Coviello's ready fix branch, with co-author credit in the commit. The implementation follows FreeBSD's _sem_clockwait_np behavior; the added regression cases also cover a blocked null-timeout waiter waking after sem_post.

Fixes #3119.

Tested

Linux, GCC 12.2, Release: guest_posix_sem_post passes. I also rebuilt the test against unmodified main; it fails on the new available-count/null-timeout case, then passes with this change.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4820127051 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3691). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T15:23:45Z. --- ### What `sem_timedwait` now consumes an available semaphore count before validating `abstime`, as permitted by POSIX. A null timeout waits indefinitely when the semaphore is empty; malformed and expired timeouts still return `EINVAL` and `ETIMEDOUT` when the call would block. This carries the available-count-before-timeout behavior from Miles Coviello's [ready fix branch](https://github.com/milescoviello/AnyPS5/tree/fix/sem-timedwait-timeout-check), with co-author credit in the commit. The implementation follows FreeBSD's `_sem_clockwait_np` behavior; the added regression cases also cover a blocked null-timeout waiter waking after `sem_post`. Fixes #3119. ### Tested Linux, GCC 12.2, Release: `guest_posix_sem_post` passes. I also rebuilt the test against unmodified `main`; it fails on the new available-count/null-timeout case, then passes with this change. ### 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 - [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
milescoviello commented 2026-10-11 15:24:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-11T15:24:44Z.


Thanks for picking up #3119, @MarcZX100. It's the change from my branch linked there (fix/sem-timedwait-timeout-check, e7d71abf, verified on Windows by @hikazey in #3119): take an available count first, and a null timeout waits with no limit. Yours is closer to libthr, though, since it skips the cancellation check on that fast path. Would you mind adding Co-authored-by: milescoviello <[email protected]> like on #3436? Thanks either way.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110563275 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3691#issuecomment-6110563275). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-11T15:24:44Z. --- Thanks for picking up #3119, @MarcZX100. It's the change from my branch linked there ([`fix/sem-timedwait-timeout-check`](https://github.com/milescoviello/AnyPS5/tree/fix/sem-timedwait-timeout-check), `e7d71abf`, verified on Windows by @hikazey in #3119): take an available count first, and a null timeout waits with no limit. Yours is closer to libthr, though, since it skips the cancellation check on that fast path. Would you mind adding `Co-authored-by: milescoviello <[email protected]>` like on #3436? Thanks either way.
MarcZX10 commented 2026-10-11 15:25:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T15:25:36Z.


Thanks for pointing out the source branch. I added the commit trailer Co-authored-by: milescoviello <[email protected]>, updated the PR description to link your ready branch, and force-pushed the attribution-only rewrite. The current head is 9b686ec1b2a372d8aa7dbcb7f43e473a91805ae7.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110571455 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3691#issuecomment-6110571455). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T15:25:36Z. --- Thanks for pointing out the source branch. I added the commit trailer `Co-authored-by: milescoviello <[email protected]>`, updated the PR description to link your ready branch, and force-pushed the attribution-only rewrite. The current head is `9b686ec1b2a372d8aa7dbcb7f43e473a91805ae7`.
adriadam10 commented 2026-10-11 22:02:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T22:02:48Z.


Checked: guest_posix_sem_post passes with the change and fails without it (new available-count/null-timeout cases); ctest for core/libs and libkernel 112/112; blocked invalid/expired timeouts still return EINVAL/ETIMEDOUT and keep the count. Matches FreeBSD _sem_clockwait_np and the Windows run reported by @hikazey in #3119. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114316486 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3691#issuecomment-6114316486). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T22:02:48Z. --- Checked: `guest_posix_sem_post` passes with the change and fails without it (new available-count/null-timeout cases); ctest for core/libs and libkernel 112/112; blocked invalid/expired timeouts still return EINVAL/ETIMEDOUT and keep the count. Matches FreeBSD `_sem_clockwait_np` and the Windows run reported by @hikazey in #3119. Merging.
github-actions[bot] commented 2026-10-11 22:26:16 +00:00 (Migrated from github.com)

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


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
#3739 docs/dev/TechnicalDebt.md L369
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114580337 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3691#issuecomment-6114580337). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T22:26:16Z. --- <!-- 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 | |---|---|---|---| | #3739 | `docs/dev/TechnicalDebt.md` [L369](https://github.com/boykopovar/AnyPS5/blob/5aaf26bd325607f65bde28741a4e82ceaaf586c0/docs/dev/TechnicalDebt.md#L369) | | |
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!3691
No description provided.