xref: /netbsd-src/tests/lib/libpthread/t_join.c (revision ead2c0eee3abe6bcf08c63bfc78eb8a93a579b2b)
1 /* $NetBSD: t_join.c,v 1.7 2011/12/12 23:54:18 joerg Exp $ */
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_join.c,v 1.7 2011/12/12 23:54:18 joerg Exp $");
33 
34 #include <errno.h>
35 #include <pthread.h>
36 #include <stdint.h>
37 
38 #include <atf-c.h>
39 
40 #include "h_common.h"
41 
42 #ifdef CHECK_STACK_ALIGNMENT
43 extern int check_stack_alignment(void);
44 #endif
45 
46 static bool error;
47 
48 static void *threadfunc1(void *);
49 static void *threadfunc2(void *);
50 
51 ATF_TC(pthread_join);
52 ATF_TC_HEAD(pthread_join, tc)
53 {
54 
55 	atf_tc_set_md_var(tc, "descr",
56 	    "Checks basic error conditions in pthread_join(3)");
57 }
58 
59 ATF_TC_BODY(pthread_join, tc)
60 {
61 	pthread_t thread;
62 
63 	PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc1, NULL));
64 	PTHREAD_REQUIRE(pthread_join(thread, NULL));
65 }
66 
67 static void *
68 threadfunc1(void *arg)
69 {
70 	pthread_t thread[25];
71 	pthread_t caller;
72 	void *val = NULL;
73 	uintptr_t i;
74 	int rv;
75 
76 	caller = pthread_self();
77 
78 #ifdef CHECK_STACK_ALIGNMENT
79 	/*
80 	 * Check alignment of thread stack, if supported.
81 	 */
82 	ATF_REQUIRE(check_stack_alignment());
83 #endif
84 
85 	/*
86 	 * The behavior is undefined, but should error
87 	 * out, if we try to join the calling thread.
88 	 */
89 	rv = pthread_join(caller, NULL);
90 
91 	/*
92 	 * The specification recommends EDEADLK.
93 	 */
94 	ATF_REQUIRE(rv != 0);
95 	ATF_REQUIRE_EQ(rv, EDEADLK);
96 
97 	for (i = 0; i < __arraycount(thread); i++) {
98 
99 		error = true;
100 
101 		rv = pthread_create(&thread[i], NULL, threadfunc2, (void *)i);
102 
103 		ATF_REQUIRE_EQ(rv, 0);
104 
105 		/*
106 		 * Check join and exit condition.
107 		 */
108 		PTHREAD_REQUIRE(pthread_join(thread[i], &val));
109 
110 		ATF_REQUIRE_EQ(error, false);
111 
112 		ATF_REQUIRE(val != NULL);
113 		ATF_REQUIRE(val == (void *)(i + 1));
114 
115 		/*
116 		 * Once the thread has returned, ESRCH should
117 		 * again follow if we try to join it again.
118 		 */
119 		rv = pthread_join(thread[i], NULL);
120 
121 		ATF_REQUIRE_EQ(rv, ESRCH);
122 
123 		/*
124 		 * Try to detach the exited thread.
125 		 */
126 		rv = pthread_detach(thread[i]);
127 
128 		ATF_REQUIRE(rv != 0);
129 	}
130 
131 	pthread_exit(NULL);
132 
133 	return NULL;
134 }
135 
136 static void *
137 threadfunc2(void *arg)
138 {
139 	static uintptr_t i = 0;
140 	uintptr_t j;
141 
142 	j = (uintptr_t)arg;
143 
144 	if (i++ == j)
145 		error = false;
146 
147 	pthread_exit((void *)i);
148 
149 	return NULL;
150 }
151 
152 ATF_TP_ADD_TCS(tp)
153 {
154 
155 	ATF_TP_ADD_TC(tp, pthread_join);
156 
157 	return atf_no_error();
158 }
159