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