1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright 2025 The FreeBSD Foundation
5 *
6 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7 * under sponsorship from the FreeBSD Foundation.
8 */
9
10 #include <atf-c.h>
11 #include <err.h>
12 #include <errno.h>
13 #include <pthread.h>
14 #include <pthread_np.h>
15 #include <stdatomic.h>
16 #include <stdio.h>
17 #include <unistd.h>
18
19 static atomic_int finish;
20
21 static void *
thr_fun(void * arg)22 thr_fun(void *arg)
23 {
24 while (atomic_load_explicit(&finish, memory_order_relaxed) != 1)
25 sleep(1);
26 atomic_store_explicit(&finish, 2, memory_order_relaxed);
27 return (arg);
28 }
29
30 ATF_TC(pthread_tryjoin);
ATF_TC_HEAD(pthread_tryjoin,tc)31 ATF_TC_HEAD(pthread_tryjoin, tc)
32 {
33 atf_tc_set_md_var(tc, "descr",
34 "Checks pthread_tryjoin(3)");
35 }
36
ATF_TC_BODY(pthread_tryjoin,tc)37 ATF_TC_BODY(pthread_tryjoin, tc)
38 {
39 pthread_t thr;
40 void *retval;
41 int error, x;
42
43 error = pthread_create(&thr, NULL, thr_fun, &x);
44 ATF_REQUIRE_EQ(error, 0);
45
46 error = pthread_tryjoin_np(thr, &retval);
47 ATF_REQUIRE_EQ(error, EBUSY);
48
49 atomic_store_explicit(&finish, 1, memory_order_relaxed);
50 while (atomic_load_explicit(&finish, memory_order_relaxed) != 2)
51 sleep(1);
52
53 error = pthread_tryjoin_np(thr, &retval);
54 ATF_REQUIRE_EQ(error, 0);
55 ATF_REQUIRE_EQ(retval, &x);
56 }
57
ATF_TP_ADD_TCS(tp)58 ATF_TP_ADD_TCS(tp)
59 {
60 ATF_TP_ADD_TC(tp, pthread_tryjoin);
61 return (atf_no_error());
62 }
63