https://gcc.gnu.org/g:37fe7e7069c2bb999c95a9807a8f895f38b9cacf
commit r16-1271-g37fe7e7069c2bb999c95a9807a8f895f38b9cacf Author: Jonathan Wakely <jwak...@redhat.com> Date: Fri Jun 6 17:18:40 2025 +0100 libstdc++: Add more tests for semaphores libstdc++-v3/ChangeLog: * testsuite/30_threads/semaphore/1.cc: Check type properties and max() values. * testsuite/30_threads/semaphore/3.cc: New test. * testsuite/30_threads/semaphore/cons_neg.cc: New test. Diff: --- libstdc++-v3/testsuite/30_threads/semaphore/1.cc | 22 ++++++++++++++++++++++ libstdc++-v3/testsuite/30_threads/semaphore/3.cc | 19 +++++++++++++++++++ .../testsuite/30_threads/semaphore/cons_neg.cc | 12 ++++++++++++ 3 files changed, 53 insertions(+) diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/1.cc b/libstdc++-v3/testsuite/30_threads/semaphore/1.cc index cc6befcc1a50..9472def4cba1 100644 --- a/libstdc++-v3/testsuite/30_threads/semaphore/1.cc +++ b/libstdc++-v3/testsuite/30_threads/semaphore/1.cc @@ -27,3 +27,25 @@ #elif __cpp_lib_semaphore != 201907L # error "Feature-test macro for semaphore has wrong value in <semaphore>" #endif + +static_assert(std::is_same_v<std::counting_semaphore<1>, + std::binary_semaphore>); + +static_assert(! std::is_same_v<std::counting_semaphore<2>, + std::binary_semaphore>); + +static_assert(! std::is_same_v<std::counting_semaphore<>, + std::binary_semaphore>); + +// The standard permits max() to be greater than the template argument, +// but for the current libstdc++ implementation it's always equal to it. +static_assert(std::binary_semaphore::max() == 1); +static_assert(std::counting_semaphore<0>::max() == 0); +static_assert(std::counting_semaphore<2>::max() == 2); + +#include <limits.h> + +static_assert(std::counting_semaphore<INT_MAX>::max() == INT_MAX); +static_assert(std::counting_semaphore<INT_MAX-1>::max() == INT_MAX-1); +static_assert(std::counting_semaphore<PTRDIFF_MAX>::max() == PTRDIFF_MAX); +static_assert(std::counting_semaphore<PTRDIFF_MAX-3>::max() == PTRDIFF_MAX-3); diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/3.cc b/libstdc++-v3/testsuite/30_threads/semaphore/3.cc new file mode 100644 index 000000000000..51b9fbb0e4bd --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/semaphore/3.cc @@ -0,0 +1,19 @@ +// { dg-do compile { target *-*-*linux* } } +// { dg-require-effective-target c++20 } +// { dg-require-effective-target hosted } + +#include <semaphore> +#include <limits.h> + +// on Linux these specializations all use a futex: +static_assert(sizeof(std::counting_semaphore<0>) == sizeof(int)); +static_assert(sizeof(std::counting_semaphore<1>) == sizeof(int)); +static_assert(sizeof(std::counting_semaphore<INT_MAX>) == sizeof(int)); +static_assert(sizeof(std::counting_semaphore<>) == sizeof(int)); + +// This will use a futex iff ptrdiff_t has 32 bits: +static_assert(sizeof(std::counting_semaphore<PTRDIFF_MAX>) == sizeof(std::ptrdiff_t)); + +#if PTRDIFF_MAX > INT_MAX +static_assert(sizeof(std::counting_semaphore<INT_MAX+1LL>) == sizeof(std::ptrdiff_t)); +#endif diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/cons_neg.cc b/libstdc++-v3/testsuite/30_threads/semaphore/cons_neg.cc new file mode 100644 index 000000000000..56e27d7ddc4c --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/semaphore/cons_neg.cc @@ -0,0 +1,12 @@ +// { dg-options "-D_GLIBCXX_ASSERTIONS" } +// { dg-do run { target c++20 xfail *-*-* } } +// { dg-require-effective-target hosted } +// { dg-add-options libatomic } + +#include <semaphore> + +int main() +{ + // Preconditions: desired >= 0 is true, and desired <= max() is true. + std::binary_semaphore b(2); +}