feat(libkernel): implement named POSIX semaphores #9167
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9167"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by slider on 2026-10-21T14:09:19Z.
What
Implements named POSIX semaphores in libkernel:
sem_open,sem_closeandsem_unlink, following FreeBSD 11.4 libc'ssem_new.c.sem_open(name, flags, ...)checks in FreeBSD's order:/givesEINVAL;/tmp/SEMD<name>path ofPATH_MAXor more givesENAMETOOLONG;O_CREAT/O_EXCLgivesEINVAL;NAME_MAXgivesENAMETOOLONG;O_CREATgivesENOENT;O_CREAT | O_EXCLon an existing name givesEEXIST.The mode and initial value are read from the variadic arguments only with
O_CREAT, as_openreads its mode. Opening a name that is already open returns the samesem_tand counts the open.sem_closedrops one open and returnsEINVALfor a semaphore that was not opened by name. The semaphore and its count stay untilsem_unlinkand the last close, as with FreeBSD's file.sem_unlinkreturnsENOENTfor a missing name or one without a leading/. Semaphores still open stay usable, and a newsem_opencreates a new semaphore.sem_destroynow returnsEINVALfor a named semaphore, as FreeBSD's_sem_destroydoes.A named semaphore is a
sem_twhose first word points to the same host semaphore assem_init's, sosem_wait,sem_trywait,sem_timedwait,sem_postandsem_getvaluework on it unchanged.Removes the three
NotImplementedplaceholders fromlibkernel_sys/Export.Discovery.cpp(duplicate-export rule). Imports bind by NID, so titles importing fromlibkernel_sysstill resolve.Debt (
docs/dev/debt/libkernel.md): named semaphores live in a table inside the process instead of/tmp/SEMD<name>files. They are not shared between processes, and the mode is ignored. Whether/tmpis writable for a title on the console is unverified. A name with a further/and an initial value aboveSEM_VALUE_MAXthrow.No other open PR implements these; the branch merges cleanly with boykopovar/AnyPS5#8616, boykopovar/AnyPS5#8987 and boykopovar/AnyPS5#9141 (
git merge-tree). boykopovar/AnyPS5#6740 already conflicts withmainindocs/dev/debt/libkernel.md, independently of this change.Tested
Linux (Ubuntu 24.04, GCC 13.3, Release):
guest_posix_named_sempasses. It covers every name, flag and length error and the order of the checks,EEXIST, and same-pointer reopens with open counting. It also checks a post from another thread through its ownsem_open,sem_destroy/sem_closemisuse, the count persisting across a full close, and unlink while open followed by re-creation.guest_posix_sem_init,guest_posix_sem_post,guest_pthread_semandguest_kernel_syspass.sem_destroyaccept a named semaphore, dropping theEEXISTcheck, and checkingNAME_MAXbefore the flags.tools/check_conventions.py --base origin/mainpasses.Windows: the changed sources and the test pass a syntax-only check with MinGW-w64 GCC 13 (not the supported 15.2); not built or run.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T05:47:45Z.
Review: COMMENTED
Checked against main
4a17c7175and FreeBSDlib/libc/gen/sem_new.c(releng/11.4):_sem_open: the leading/, thestrlcatPATH_MAX check, flags, va_arg, then NAME_MAX and ENOENT/EEXIST at_open. The errno values and O_CREAT/O_EXCL bits match too, and so do reopen refcounting,_sem_close/_sem_destroyEINVAL and_sem_unlinkENOENT. The PS5 SDK'ssce_stubs/libkernel.cexports all three from libkernel, so moving them out of libkernel_sys is right.docs/dev/debt/libkernel.md:126, last sentence: I think this divergence doesn't exist. In_sem_open'ssem_listloop, a name that is already open and still linked makes_open(path, O_CREAT|O_EXCL|…)fail with EEXIST, and the code takesni = NULL; goto error;without touchingni->name.ni->name = NULLhappens only when that_opensucceeds, which needs the file to have been unlinked first. The PR already models that: unlink eraseslinked, so the next create gets a new address. Could you remove the sentence so the debt file doesn't record a gap that isn't there?ksem_*syscall exports inSemaphore/src/Ksem.cpp, which are different NIDs from the libcsem_*here. Their hunks inExport.Discovery.cpp,libkernel/CMakeLists.txtandlibkernel.mdare well away from this PR's, so I don't see a duplicate or a conflict.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Imported from boykopovar/AnyPS5. Originally posted by slider on 2026-10-22T09:15:46Z.
Thanks @harisibrar1, you're right: with the name still linked,
_openfails with EEXIST beforeni->name = NULL, which only runs after an unlink and re-create, already modelled. Removed the sentence in https://github.com/boykopovar/AnyPS5/pull/9167/commits/f2766b244740052b40b5df046821787b874e8fb1.Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T12:54:54Z.
Review: APPROVED
Rechecked at
f2766b2447:_sem_opendoesn't have. The rest of the entry still describes what Sem.cpp does.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.