t_join.c revision 1.5 1 /* $NetBSD: t_join.c,v 1.5 2010/11/30 18:38:54 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.5 2010/11/30 18:38:54 joerg Exp $");
33
34 #include <errno.h>
35 #include <pthread.h>
36
37 #include <atf-c.h>
38
39 #include "h_common.h"
40
41 #ifdef CHECK_STACK_ALIGNMENT
42 extern int check_stack_alignment(void);
43 #endif
44
45 static bool error;
46
47 static void *threadfunc1(void *);
48 static void *threadfunc2(void *);
49
50 ATF_TC(pthread_join);
51 ATF_TC_HEAD(pthread_join, tc)
52 {
53
54 atf_tc_set_md_var(tc, "descr",
55 "Checks basic error conditions in pthread_join(3)");
56 }
57
58 ATF_TC_BODY(pthread_join, tc)
59 {
60 pthread_t thread;
61
62 PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc1, NULL));
63 PTHREAD_REQUIRE(pthread_join(thread, NULL));
64 }
65
66 static void *
67 threadfunc1(void *arg)
68 {
69 pthread_t thread[25];
70 pthread_t caller;
71 void *val = NULL;
72 uintptr_t i;
73 int rv;
74
75 caller = pthread_self();
76
77 #ifdef CHECK_STACK_ALIGNMENT
78 /*
79 * Check alignment of thread stack, if supported.
80 */
81 ATF_REQUIRE(check_stack_alignment());
82 #endif
83
84 /*
85 * The behavior is undefined, but should error
86 * out, if we try to join the calling thread.
87 */
88 rv = pthread_join(caller, NULL);
89
90 /*
91 * The specification recommends EDEADLK.
92 */
93 ATF_REQUIRE(rv != 0);
94 ATF_REQUIRE_EQ(rv, EDEADLK);
95
96 for (i = 0; i < __arraycount(thread); i++) {
97
98 error = true;
99
100 rv = pthread_create(&thread[i], NULL, threadfunc2, (void *)i);
101
102 ATF_REQUIRE_EQ(rv, 0);
103
104 /*
105 * Try to join an invalid thread.
106 */
107 rv = pthread_join(thread[i + 1], NULL);
108
109 ATF_REQUIRE_EQ(rv, ESRCH);
110
111 /*
112 * Check join and exit condition.
113 */
114 PTHREAD_REQUIRE(pthread_join(thread[i], &val));
115
116 ATF_REQUIRE_EQ(error, false);
117
118 ATF_REQUIRE(val != NULL);
119 ATF_REQUIRE(val == (void *)(i + 1));
120
121 /*
122 * Once the thread has returned, ESRCH should
123 * again follow if we try to join it again.
124 */
125 rv = pthread_join(thread[i], NULL);
126
127 ATF_REQUIRE_EQ(rv, ESRCH);
128
129 /*
130 * Try to detach the exited thread.
131 */
132 rv = pthread_detach(thread[i]);
133
134 ATF_REQUIRE(rv != 0);
135 }
136
137 pthread_exit(NULL);
138
139 return NULL;
140 }
141
142 static void *
143 threadfunc2(void *arg)
144 {
145 static uintptr_t i = 0;
146 uintptr_t j;
147
148 j = (uintptr_t)arg;
149
150 if (i++ == j)
151 error = false;
152
153 pthread_exit((void *)i);
154
155 return NULL;
156 }
157
158 ATF_TP_ADD_TCS(tp)
159 {
160
161 ATF_TP_ADD_TC(tp, pthread_join);
162
163 return atf_no_error();
164 }
165