summaryrefslogtreecommitdiff
path: root/lib/libthr/tests/pthread_tryjoin_test.c
blob: ba88ac2ed6f50dbada824c7c4cc18fa95f1b6a95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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());
}