GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
libstdc++

Make libstdc++ test semaphore work with POSIX semaphores.

libstdc++ test semaphore now uses POSIX semaphores as a fallback for SysV.

This commit enhances the libstdc++ test suite’s semaphore implementation to support POSIX semaphores when System V semaphores are unavailable. It uses mmap for shared memory and handles systems lacking MAP_ANONYMOUS. The change ensures tests fail loudly if neither semaphore type is available and fixes a resource leak in the existing SysV semaphore code. The semaphore class is also refactored into separate header and source files.

In Details

The __gnu_test::semaphore class, previously relying solely on System V semaphores, is updated to use POSIX semaphores via sem_init and mmap when SysV semaphores are not available. This addresses issues on systems where SysV semaphores are not part of the XSI extension. The implementation avoids named POSIX semaphores (sem_open) due to naming conflicts in parallel test execution, opting for unnamed semaphores in shared memory. It also ensures tests fail explicitly if neither semaphore type is functional and fixes a resource leak in the SysV path by ensuring IPC_RMID is called on `sem…

For Context
POSIX semaphores
A set of synchronization primitives defined by the POSIX standard, allowing processes to signal each other. They can be named or unnamed.
System V semaphores
An older set of semaphore primitives originating from the System V Unix implementation, managed by the kernel.
mmap
A system call that maps files or devices into memory, allowing them to be accessed like arrays in memory.
MAP_ANONYMOUS
A flag used with mmap to create a mapping that is not backed by a file, typically used for anonymous memory.
sem_init
A POSIX function to initialize an unnamed semaphore.
semget
A System V function used to obtain a semaphore identifier.
semctl
A System V function used to control or query semaphore status.
IPC_RMID
A command used with semctl to remove a semaphore set from the kernel.
Filed Under: testsuiteconcurrencyposix