Skip to content

feat(libc): implement ftrylockfile - #3564

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
Aathish-D:feat/libc-ftrylockfile
Oct 11, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
Aathish-D:feat/libc-ftrylockfile

Conversation

@Aathish-D

Copy link
Copy Markdown
Contributor

What

Implement ftrylockfile (NID h05OHOMZNMw) in libc. Resolves #3548.

Porpoise (PPSA99764) stops after pthread_attr_* (#3494) with the predicted:

FAIL: unresolved ELF import h05OHOMZNMw from libSceLibcInternal.prx

(idx 311, call site 0x8EC37); RetroArch (PPSA99169) imports it at idx 448 (call site 0xA4A27).

ftrylockfile tries the same host CRT stream lock that flockfile, funlockfile and every locking host stdio call on the stream take. It returns 0 when acquired or already owned by the caller, and -1 when another thread holds it, following FreeBSD 11.4 _flock_stub.c (8c639945). Linux calls glibc ftrylockfile; Windows calls TryEnterCriticalSection on the UCRT stream lock (__crt_stdio_stream_data::_lock). Null or closed streams throw. Recorded in TechnicalDebt.md.

Carried from @screamm's branch referenced in #3548.

Tested

Linux (x86_64, GCC 15, Ninja, Release), full build with libs:

  • ctest --test-dir build -R "^guest_standard_streams$" --output-on-failure passed (0.16 s)
  • python3 tools/check_conventions.py --base upstream/main passed
  • python3 tools/check_test_registration.py build passed (647 tests registered)
  • Checked readelf -s build/core/libs/libs/libc.prx exports h05OHOMZNMw

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

Porpoise PPSA99764 stops after pthread_attr_* (boykopovar#3494) with the
predicted `FAIL: unresolved ELF import h05OHOMZNMw from
libSceLibcInternal.prx` (idx 311, call site 0x8EC37); RetroArch
PPSA99169 imports it at idx 448 (call site 0xA4A27). h05OHOMZNMw is
ftrylockfile by tools/nid_names.py and aerolib.csv (zecoxao/sce_symbols
2883963a); ps5-payload-dev/sdk f5d40f02 declares
`int ftrylockfile(FILE *)` in include/freebsd/stdio.h.

ftrylockfile tries the same host CRT stream lock that flockfile,
funlockfile and every locking host stdio call on the stream take, so
no second lock exists beside the one stdio uses. It returns 0 when the
lock is acquired or already owned by the caller (recursive, released by
as many funlockfile calls) and -1 when another thread holds it, as
FreeBSD 8c639945 lib/libc/stdio/_flock_stub.c:89-104 does. Linux calls
glibc ftrylockfile. UCRT has no try variant of _lock_file, so Windows
calls TryEnterCriticalSection on the CRITICAL_SECTION that _lock_file
enters (__crt_stdio_stream_data::_lock, ucrt/inc/corecrt_internal_stdio.h,
same layout in Windows SDK 10.0.19041, 10.0.22621 and 10.0.26100). A
null or closed stream throws. The UCRT layout dependency is recorded in
TechnicalDebt.

guest_standard_streams covers trylock on an unlocked stream, re-entry
by the owner, failure from another thread while held (also after one of
two unlocks and while another thread holds flockfile), stdio calls
from another thread blocking on the trylock-held lock, release order,
and null and closed streams.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Progress report (a11c8f7 → 9c7aa0d)

📈 System libraries: 85.82% (+0.01%, +1 implemented functions)

✅ 1 implemented
Library Name
libc ftrylockfile_nid_postfix

@adriadam10

Copy link
Copy Markdown
Collaborator

Checked build and ctest on Linux (guest_standard_streams passes, and fails to link without the change), ftrylockfile semantics against FreeBSD _flock_stub.c, TechnicalDebt entry, no overlap with #3527/#3412; merging.

@adriadam10
adriadam10 merged commit 3ccef6a into boykopovar:main Oct 11, 2026
13 checks passed
@Aathish-D
Aathish-D deleted the feat/libc-ftrylockfile branch October 12, 2026 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libc: ftrylockfile is absent (Porpoise, RetroArch); fix branch ready

2 participants