fix(libkernel): let pthread_barrier_destroy wait for released waiters #4002

Merged
zmeizy merged 1 commit from upstream-pr/4002 into main 2026-10-12 05:19:22 +00:00
zmeizy commented 2026-10-11 21:11:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by zmeizy on 2026-10-11T21:11:22Z.


What

pthread_barrier_destroy returned EBUSY while any thread released by the last pthread_barrier_wait had not yet returned from it. The common pattern where the thread that gets PTHREAD_BARRIER_SERIAL_THREAD destroys the barrier therefore failed almost every time, and the barrier leaked.

FreeBSD 11 libthr (thr_barrier.c, _pthread_barrier_destroy) returns EBUSY only while threads are still blocked in the current cycle (b_waiters). It waits for released threads to leave (b_refcount) before it frees the barrier.

The barrier now does the same:

  • destroy returns EBUSY only while threads wait in the current cycle.
  • Otherwise it waits until the released threads have left pthread_barrier_wait.
  • The last released thread to leave wakes it.

Tested

Ubuntu 24.04, GCC 13.3, on main 0a8b1d32.

  • guest_pthread_barrier: a new check runs 50 rounds of 4 threads in which the serial thread destroys the barrier. Each destroy must return 0 and leave the pointer null.
  • The check fails on main without the change and passes with it (3 runs).

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 (n/a)
  • 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 (n/a)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ha6Tsy62AFUyp9a5Mf7vjA

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4822379576 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4002). Originally posted by [zmeizy](https://github.com/zmeizy) on 2026-10-11T21:11:22Z. --- ### What `pthread_barrier_destroy` returned `EBUSY` while any thread released by the last `pthread_barrier_wait` had not yet returned from it. The common pattern where the thread that gets `PTHREAD_BARRIER_SERIAL_THREAD` destroys the barrier therefore failed almost every time, and the barrier leaked. FreeBSD 11 libthr (`thr_barrier.c`, `_pthread_barrier_destroy`) returns `EBUSY` only while threads are still blocked in the current cycle (`b_waiters`). It waits for released threads to leave (`b_refcount`) before it frees the barrier. The barrier now does the same: - `destroy` returns `EBUSY` only while threads wait in the current cycle. - Otherwise it waits until the released threads have left `pthread_barrier_wait`. - The last released thread to leave wakes it. ### Tested Ubuntu 24.04, GCC 13.3, on `main` `0a8b1d32`. - `guest_pthread_barrier`: a new check runs 50 rounds of 4 threads in which the serial thread destroys the barrier. Each destroy must return 0 and leave the pointer null. - The check fails on `main` without the change and passes with it (3 runs). ### 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) (n/a) - [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 (n/a) - [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) https://claude.ai/code/session_01Ha6Tsy62AFUyp9a5Mf7vjA
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!4002
No description provided.