summaryrefslogtreecommitdiff
path: root/lib/libthr/tests
diff options
context:
space:
mode:
authorKonstantin Belousov <kib@FreeBSD.org>2026-01-18 14:47:40 +0200
committerKonstantin Belousov <kib@FreeBSD.org>2026-01-19 18:50:08 +0200
commit902e3057cd5c7a076b78dd559b7b264610af59aa (patch)
treecba6e4a06532f3929f4583c4f8a0d3349041e4d9 /lib/libthr/tests
parent7f026a58691db1763203ab8c7c8f34f738bfd9d5 (diff)
lib/libthr: add pthread_tryjoin(3) testHEADmain
Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D54766
Diffstat (limited to 'lib/libthr/tests')
-rw-r--r--lib/libthr/tests/Makefile1
-rw-r--r--lib/libthr/tests/pthread_tryjoin_test.c62
2 files changed, 63 insertions, 0 deletions
diff --git a/lib/libthr/tests/Makefile b/lib/libthr/tests/Makefile
index 017b740157dc..5ffcd7b046c8 100644
--- a/lib/libthr/tests/Makefile
+++ b/lib/libthr/tests/Makefile
@@ -36,6 +36,7 @@ NETBSD_ATF_TESTS_SH+= resolv_test
ATF_TESTS_C+= atfork_test
ATF_TESTS_C+= umtx_op_test
ATF_TESTS_C+= pthread_sigqueue_test
+ATF_TESTS_C+= pthread_tryjoin_test
LIBADD+= pthread
LIBADD.fpu_test+= m
diff --git a/lib/libthr/tests/pthread_tryjoin_test.c b/lib/libthr/tests/pthread_tryjoin_test.c
new file mode 100644
index 000000000000..ba88ac2ed6f5
--- /dev/null
+++ b/lib/libthr/tests/pthread_tryjoin_test.c
@@ -0,0 +1,62 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2025 The FreeBSD Foundation
+ *
+ * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
+ * under sponsorship from the FreeBSD Foundation.
+ */
+
+#include <atf-c.h>
+#include <err.h>
+#include <errno.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <unistd.h>
+
+static atomic_int finish;
+
+static void *
+thr_fun(void *arg)
+{
+ while (atomic_load_explicit(&finish, memory_order_relaxed) != 1)
+ sleep(1);
+ atomic_store_explicit(&finish, 2, memory_order_relaxed);
+ return (arg);
+}
+
+ATF_TC(pthread_tryjoin);
+ATF_TC_HEAD(pthread_tryjoin, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks pthread_tryjoin(3)");
+}
+
+ATF_TC_BODY(pthread_tryjoin, tc)
+{
+ pthread_t thr;
+ void *retval;
+ int error, x;
+
+ error = pthread_create(&thr, NULL, thr_fun, &x);
+ ATF_REQUIRE_EQ(error, 0);
+
+ error = pthread_tryjoin_np(thr, &retval);
+ ATF_REQUIRE_EQ(error, EBUSY);
+
+ atomic_store_explicit(&finish, 1, memory_order_relaxed);
+ while (atomic_load_explicit(&finish, memory_order_relaxed) != 2)
+ sleep(1);
+
+ error = pthread_tryjoin_np(thr, &retval);
+ ATF_REQUIRE_EQ(error, 0);
+ ATF_REQUIRE_EQ(retval, &x);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, pthread_tryjoin);
+ return (atf_no_error());
+}