]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/netbsd-tests/lib/libpthread/t_join.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / netbsd-tests / lib / libpthread / t_join.c
1 /* $NetBSD: t_join.c,v 1.8 2012/03/12 20:17:16 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.8 2012/03/12 20:17:16 joerg Exp $");
33
34 #include <errno.h>
35 #include <pthread.h>
36 #include <stdint.h>
37
38 #include <atf-c.h>
39
40 #ifdef __FreeBSD__
41 #include <pthread_np.h>
42 #endif
43
44 #include "h_common.h"
45
46 #ifdef CHECK_STACK_ALIGNMENT
47 extern int check_stack_alignment(void);
48 #endif
49
50 #define STACKSIZE       65536
51
52 static bool error;
53
54 static void *threadfunc1(void *);
55 static void *threadfunc2(void *);
56
57 ATF_TC(pthread_join);
58 ATF_TC_HEAD(pthread_join, tc)
59 {
60
61         atf_tc_set_md_var(tc, "descr",
62             "Checks basic error conditions in pthread_join(3)");
63 }
64
65 ATF_TC_BODY(pthread_join, tc)
66 {
67         pthread_t thread;
68
69         PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc1, NULL));
70         PTHREAD_REQUIRE(pthread_join(thread, NULL));
71 }
72
73 static void *
74 threadfunc1(void *arg)
75 {
76         pthread_t thread[25];
77         pthread_t caller;
78         void *val = NULL;
79         uintptr_t i;
80         int rv;
81         pthread_attr_t attr;
82
83         caller = pthread_self();
84
85 #ifdef CHECK_STACK_ALIGNMENT
86         /*
87          * Check alignment of thread stack, if supported.
88          */
89         ATF_REQUIRE(check_stack_alignment());
90 #endif
91
92         /*
93          * The behavior is undefined, but should error
94          * out, if we try to join the calling thread.
95          */
96         rv = pthread_join(caller, NULL);
97
98         /*
99          * The specification recommends EDEADLK.
100          */
101         ATF_REQUIRE(rv != 0);
102         ATF_REQUIRE_EQ(rv, EDEADLK);
103
104         ATF_REQUIRE(pthread_attr_init(&attr) == 0);
105
106         for (i = 0; i < __arraycount(thread); i++) {
107
108                 error = true;
109
110                 ATF_REQUIRE(pthread_attr_setstacksize(&attr, STACKSIZE * (i + 1)) == 0);
111
112                 rv = pthread_create(&thread[i], &attr, threadfunc2, (void *)i);
113
114                 ATF_REQUIRE_EQ(rv, 0);
115
116                 /*
117                  * Check join and exit condition.
118                  */
119                 PTHREAD_REQUIRE(pthread_join(thread[i], &val));
120
121                 ATF_REQUIRE_EQ(error, false);
122
123                 ATF_REQUIRE(val != NULL);
124                 ATF_REQUIRE(val == (void *)(i + 1));
125
126                 /*
127                  * Once the thread has returned, ESRCH should
128                  * again follow if we try to join it again.
129                  */
130                 rv = pthread_join(thread[i], NULL);
131
132                 ATF_REQUIRE_EQ(rv, ESRCH);
133
134                 /*
135                  * Try to detach the exited thread.
136                  */
137                 rv = pthread_detach(thread[i]);
138
139                 ATF_REQUIRE(rv != 0);
140         }
141
142         ATF_REQUIRE(pthread_attr_destroy(&attr) == 0);
143
144         pthread_exit(NULL);
145
146         return NULL;
147 }
148
149 static void *
150 threadfunc2(void *arg)
151 {
152         static uintptr_t i = 0;
153         uintptr_t j;
154         pthread_attr_t attr;
155         size_t stacksize;
156
157         j = (uintptr_t)arg;
158
159 #ifdef __FreeBSD__
160         pthread_attr_init(&attr);
161 #endif
162         ATF_REQUIRE(pthread_attr_get_np(pthread_self(), &attr) == 0);
163         ATF_REQUIRE(pthread_attr_getstacksize(&attr, &stacksize) == 0);
164         ATF_REQUIRE(stacksize == STACKSIZE * (j + 1));
165         ATF_REQUIRE(pthread_attr_destroy(&attr) == 0);
166
167         if (i++ == j)
168                 error = false;
169
170         pthread_exit((void *)i);
171
172         return NULL;
173 }
174
175 ATF_TP_ADD_TCS(tp)
176 {
177
178         ATF_TP_ADD_TC(tp, pthread_join);
179
180         return atf_no_error();
181 }