https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127425
--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jonathan Wakely <[email protected]>: https://gcc.gnu.org/g:409e74cc30b03afda59fc0d5711be71cd2e1bb59 commit r17-4395-g409e74cc30b03afda59fc0d5711be71cd2e1bb59 Author: Jonathan Wakely <[email protected]> Date: Wed Sep 16 10:56:07 2026 +0100 libstdc++: Make __gnu_test::semaphore work without SysV semget [PR127425] Port the testsuite's simple semaphore type to use POSIX semaphores when System V semaphores are not supported, as the latter are part of the XSI option and not mandated by POSIX. For the POSIX semaphore path we need to use mmap to create the semaphore in shared memory and also need to handle ancient systems without MAP_ANONYMOUS support. We use unnamed semaphores because using sem_open to create a named semaphore requires creating two or more unique names per test that work even when the tests are run in parallel, which would get awkward. Although it might appear better to prefer the new path and use POSIX semaphores by default, that wouldn't work because macOS defines _POSIX_SEMAPHORES to a positive value but its sem_init always returns ENOSYS at runtime. Instead of trying sem_init and then switching to shmget as a runtime fallback, we might as well just continue using the code that we know works fine - i.e. the SysV semaphore implementation. The new sem_init path is only used for systems where the existing code doesn't work, which currently just PASS the tests incorrectly as described in Bug 127425. For the case where neither SysV nor POSIX semaphores are available, make the __gnu_test::semaphore constructor abort, so the tests FAIL loudly instead of a silent PASS. This fixes the main points raised in the bug report. Move the definition of the semaphore to a new header, so that it's only defined in the handful of tests that actually use it, and move the definitions of the member functions to a new .cc file. In passing, fix a resource leak in the existing code for the case where semget creates a new semaphore set but semctl fails to initialize it. Because SysV semaphores are kernel objects, the set would not be destroyed when the process exits. Use semctl with IPC_RMID before throwing an exception, so that the kernel removes the semaphore set. Also make the __gnu_test::semaphore class non-copyable to prevent misuse and double-frees. Also add a dejagnu dg-require-sysv-or-posix-semaphore directive so that tests which don't support either type of semaphore will be UNSUPPORTED instead of PASS (or FAIL with the new aborting constructor). This test uses _XOPEN_UNIX rather than the three autoconf macros that are checked in testsuite_semaphore.cc but that is a reasonable proxy for SysV semaphore support. It might mean that some very old systems mark those tests as UNSUPPORTED when they do actually support the SysV semaphores without defining the _XOPEN_UNIX macro. I can live with that. It would be nice to use std::unique_ptr with a custom deleter to manage the SysV semaphore set or the mmap region and POSIX semaphore. That could only be done as local variables in testsuite_semaphore.cc not as a non-static data member of __gnu_test::semaphore, because the tests which use __gnu_test::semaphore need to run as C++98 and so they can't use std::unique_ptr in the header. The additional logic to define and use a custom deleter doesn't seem worth it if it wouldn't actually remove the __gnu_test::semaphore destructor. libstdc++-v3/ChangeLog: PR libstdc++/127425 * doc/xml/manual/test.xml: Document new testsuite header. * doc/html/manual/test.html: Regenerate. * testsuite/27_io/basic_filebuf/close/char/4879.cc: Use new dg-require-sysv-or-posix-semaphore directive. * testsuite/27_io/basic_filebuf/close/char/9964.cc: Likewise. * testsuite/27_io/basic_filebuf/imbue/char/13171-2.cc: Likewise. * testsuite/27_io/basic_filebuf/imbue/wchar_t/14975-2.cc: Likewise. * testsuite/27_io/basic_filebuf/seekoff/char/26777.cc: Likewise. * testsuite/27_io/basic_filebuf/underflow/char/10097.cc: Likewise. * testsuite/27_io/objects/char/7.cc: Likewise. * testsuite/27_io/objects/char/9661-1.cc: Likewise. * testsuite/27_io/objects/wchar_t/7.cc: Likewise. * testsuite/27_io/objects/wchar_t/9661-1.cc: Likewise. * testsuite/lib/dg-options.exp (dg-require-sysv-or-posix-semaphore): New proc. * testsuite/lib/libstdc++.exp (v3-build_support): Add new file to list of testsuite source files. (check_v3_target_posix_or_sysv_semaphore): New proc. * testsuite/util/testsuite_hooks.cc (semaphore): Move to new file. * testsuite/util/testsuite_hooks.h (semaphore): Likewise. * testsuite/util/testsuite_semaphore.cc: Move definition of semaphore member functions here. Fix resource leak in constructor if semctl fails. Add fallback using mmap+sem_init instead of semget+semctl. * testsuite/util/testsuite_semaphore.h: Move definition of semaphore to here. Replace sem_set_ data member with anonymous union. Add deleted copy operations. Reviewed-by: Tomasz KamiÅski <[email protected]>
