]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add more tests for semaphores
authorJonathan Wakely <jwakely@redhat.com>
Fri, 6 Jun 2025 16:18:40 +0000 (17:18 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 6 Jun 2025 19:15:07 +0000 (20:15 +0100)
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.

libstdc++-v3/testsuite/30_threads/semaphore/1.cc
libstdc++-v3/testsuite/30_threads/semaphore/3.cc [new file with mode: 0644]
libstdc++-v3/testsuite/30_threads/semaphore/cons_neg.cc [new file with mode: 0644]

index cc6befcc1a50a790881c8a9921645137d6915faf..9472def4cba1a24a6251af579a36a2c749067d89 100644 (file)
 #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 (file)
index 0000000..51b9fbb
--- /dev/null
@@ -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 (file)
index 0000000..56e27d7
--- /dev/null
@@ -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);
+}