aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2025-06-06 17:18:40 +0100
committerJonathan Wakely <redi@gcc.gnu.org>2025-06-06 20:15:07 +0100
commit37fe7e7069c2bb999c95a9807a8f895f38b9cacf (patch)
tree7fa5e40168d48ebbf7479d6536a803fc66ebd31d
parent7407891a3d7c177212c6027c1dee08bed096666b (diff)
downloadgcc-37fe7e7069c2bb999c95a9807a8f895f38b9cacf.zip
gcc-37fe7e7069c2bb999c95a9807a8f895f38b9cacf.tar.gz
gcc-37fe7e7069c2bb999c95a9807a8f895f38b9cacf.tar.bz2
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.
-rw-r--r--libstdc++-v3/testsuite/30_threads/semaphore/1.cc22
-rw-r--r--libstdc++-v3/testsuite/30_threads/semaphore/3.cc19
-rw-r--r--libstdc++-v3/testsuite/30_threads/semaphore/cons_neg.cc12
3 files changed, 53 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/1.cc b/libstdc++-v3/testsuite/30_threads/semaphore/1.cc
index cc6befc..9472def 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 0000000..51b9fbb
--- /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 0000000..56e27d7
--- /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);
+}