From: Jonathan Wakely Date: Fri, 6 Jun 2025 16:18:40 +0000 (+0100) Subject: libstdc++: Add more tests for semaphores X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=37fe7e7069c2bb999c95a9807a8f895f38b9cacf;p=thirdparty%2Fgcc.git 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 --git a/libstdc++-v3/testsuite/30_threads/semaphore/1.cc b/libstdc++-v3/testsuite/30_threads/semaphore/1.cc index cc6befcc1a5..9472def4cba 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 " #endif + +static_assert(std::is_same_v, + std::binary_semaphore>); + +static_assert(! std::is_same_v, + std::binary_semaphore>); + +static_assert(! std::is_same_v, + 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 + +static_assert(std::counting_semaphore::max() == INT_MAX); +static_assert(std::counting_semaphore::max() == INT_MAX-1); +static_assert(std::counting_semaphore::max() == PTRDIFF_MAX); +static_assert(std::counting_semaphore::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 00000000000..51b9fbb0e4b --- /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 +#include + +// 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) == 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) == sizeof(std::ptrdiff_t)); + +#if PTRDIFF_MAX > INT_MAX +static_assert(sizeof(std::counting_semaphore) == 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 00000000000..56e27d7ddc4 --- /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 + +int main() +{ + // Preconditions: desired >= 0 is true, and desired <= max() is true. + std::binary_semaphore b(2); +}