git: 56a490f9c5e6 - main - libthr: Robust tryjoin test

From: ShengYi Hung <aokblast_at_FreeBSD.org>
Date: Thu, 29 Oct 2026 17:44:41 UTC
The branch main has been updated by aokblast:

URL: https://cgit.FreeBSD.org/src/commit/?id=56a490f9c5e64f2ee346fc93332c7ab78f9237c4

commit 56a490f9c5e64f2ee346fc93332c7ab78f9237c4
Author:     ShengYi Hung <aokblast@FreeBSD.org>
AuthorDate: 2026-10-18 02:15:18 +0000
Commit:     ShengYi Hung <aokblast@FreeBSD.org>
CommitDate: 2026-10-29 17:44:15 +0000

    libthr: Robust tryjoin test
    
    We cannot actually rely on a status (finish) before thr_exit. The
    thread needs the kernel signal status=1 to userspace. Before that,
    any tryjoin fails. The previous one works as we sleep 1 and should be
    enough for normal case.
    
    Reviewed by:    kib, markj
    MFC after:      2 weeks
    Differential Revision: https://reviews.freebsd.org/D60832
---
 lib/libthr/tests/Makefile               |  1 +
 lib/libthr/tests/pthread_tryjoin_test.c | 29 ++++++++++++++++++++++++-----
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/lib/libthr/tests/Makefile b/lib/libthr/tests/Makefile
index 5ffcd7b046c8..f738c3268c56 100644
--- a/lib/libthr/tests/Makefile
+++ b/lib/libthr/tests/Makefile
@@ -40,6 +40,7 @@ ATF_TESTS_C+=		pthread_tryjoin_test
 
 LIBADD+=		pthread
 LIBADD.fpu_test+=	m
+LIBADD.pthread_tryjoin_test+=	kvm
 LIBADD.sem_test+=	rt
 
 BINDIR=		${TESTSDIR}
diff --git a/lib/libthr/tests/pthread_tryjoin_test.c b/lib/libthr/tests/pthread_tryjoin_test.c
index ba88ac2ed6f5..ca92f8e25228 100644
--- a/lib/libthr/tests/pthread_tryjoin_test.c
+++ b/lib/libthr/tests/pthread_tryjoin_test.c
@@ -10,6 +10,8 @@
 #include <atf-c.h>
 #include <err.h>
 #include <errno.h>
+#include <fcntl.h>
+#include <kvm.h>
 #include <pthread.h>
 #include <pthread_np.h>
 #include <stdatomic.h>
@@ -22,11 +24,22 @@ 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);
+		usleep(1000);
 	return (arg);
 }
 
+static int
+thread_count(kvm_t *kd, pid_t pid)
+{
+	struct kinfo_proc *kip;
+	int count;
+
+	kip = kvm_getprocs(kd, KERN_PROC_PID | KERN_PROC_INC_THREAD, pid,
+	    &count);
+	ATF_REQUIRE(kip != NULL);
+	return (count);
+}
+
 ATF_TC(pthread_tryjoin);
 ATF_TC_HEAD(pthread_tryjoin, tc)
 {
@@ -37,8 +50,13 @@ ATF_TC_HEAD(pthread_tryjoin, tc)
 ATF_TC_BODY(pthread_tryjoin, tc)
 {
 	pthread_t thr;
+	kvm_t *kd;
 	void *retval;
-	int error, x;
+	int error, nthr, x;
+
+	kd = kvm_openfiles(NULL, "/dev/null", NULL, O_RDONLY, NULL);
+	ATF_REQUIRE(kd != NULL);
+	nthr = thread_count(kd, getpid());
 
 	error = pthread_create(&thr, NULL, thr_fun, &x);
 	ATF_REQUIRE_EQ(error, 0);
@@ -47,8 +65,9 @@ ATF_TC_BODY(pthread_tryjoin, tc)
 	ATF_REQUIRE_EQ(error, EBUSY);
 
 	atomic_store_explicit(&finish, 1, memory_order_relaxed);
-	while (atomic_load_explicit(&finish, memory_order_relaxed) != 2)
-		sleep(1);
+	while (thread_count(kd, getpid()) != nthr)
+		usleep(1000);
+	kvm_close(kd);
 
 	error = pthread_tryjoin_np(thr, &retval);
 	ATF_REQUIRE_EQ(error, 0);