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
This commit is contained in:
ShengYi Hung committed 2026-10-30 04:44:15 +11:00
1 parent 8e3a6d7f5a
commit 56a490f9c5
2 files changed
+25 -5

No files matched your search

+1
View File
@@ -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}
+24 -5
View File
@@ -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);