A regress test to test concurrent exception handling in threads
authorotto <otto@openbsd.org>
Sat, 20 Feb 2021 19:05:28 +0000 (19:05 +0000)
committerotto <otto@openbsd.org>
Sat, 20 Feb 2021 19:05:28 +0000 (19:05 +0000)
regress/misc/exceptions/threads/Makefile [new file with mode: 0644]
regress/misc/exceptions/threads/exceptions.cc [new file with mode: 0644]

diff --git a/regress/misc/exceptions/threads/Makefile b/regress/misc/exceptions/threads/Makefile
new file mode 100644 (file)
index 0000000..722725b
--- /dev/null
@@ -0,0 +1,11 @@
+#      $OpenBSD: Makefile,v 1.1 2021/02/20 19:05:28 otto Exp $
+
+PROG=  exceptions
+SRCS=  exceptions.cc
+
+REGRESS_TARGETS=runs
+
+runs: exceptions
+       for i in $$(jot 100); do exceptions; done
+
+.include <bsd.regress.mk>
diff --git a/regress/misc/exceptions/threads/exceptions.cc b/regress/misc/exceptions/threads/exceptions.cc
new file mode 100644 (file)
index 0000000..a29ac88
--- /dev/null
@@ -0,0 +1,52 @@
+/*     $OpenBSD: exceptions.cc,v 1.1 2021/02/20 19:05:28 otto Exp $    */
+/*
+ *     Written by Otto Moerbeek <otto@drijf.net> 2021 Public Domain
+ */
+
+#include <string>
+#include <iostream>
+#include <err.h>
+#include <pthread.h>
+
+void
+a()
+{
+       try {
+               throw std::string("foo");
+        }
+       catch (const std::string& ex) {
+               if (ex != "foo")
+                       errx(1, "foo");
+       }
+}
+
+void
+b()
+{
+       a();
+}
+
+void *
+c(void *)
+{
+       b();
+       return nullptr;
+}
+
+#define N 100
+
+int
+main()
+{
+       int i;
+       pthread_t p[N];
+
+       for (i = 0; i < N; i++)
+               if (pthread_create(&p[i], nullptr, c, nullptr) != 0)
+                       err(1, nullptr);
+       for (i = 0; i < N; i++)
+               if (pthread_join(p[i], nullptr) != 0)
+                       err(1, nullptr);
+       std::cout << ".";
+       return 0;
+}