| @@ -1,304 +1,307 @@ | | | @@ -1,304 +1,307 @@ |
1 | /* $NetBSD: t_sem.c,v 1.6 2012/03/07 23:31:44 joerg Exp $ */ | | 1 | /* $NetBSD: t_sem.c,v 1.7 2012/03/09 19:46:37 joerg Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
15 | * | | 15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 16 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
17 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 17 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | * POSSIBILITY OF SUCH DAMAGE. | | 26 | * POSSIBILITY OF SUCH DAMAGE. |
27 | */ | | 27 | */ |
28 | | | 28 | |
29 | /*- | | 29 | /*- |
30 | * Copyright (c)2004 YAMAMOTO Takashi, | | 30 | * Copyright (c)2004 YAMAMOTO Takashi, |
31 | * All rights reserved. | | 31 | * All rights reserved. |
32 | * | | 32 | * |
33 | * Redistribution and use in source and binary forms, with or without | | 33 | * Redistribution and use in source and binary forms, with or without |
34 | * modification, are permitted provided that the following conditions | | 34 | * modification, are permitted provided that the following conditions |
35 | * are met: | | 35 | * are met: |
36 | * 1. Redistributions of source code must retain the above copyright | | 36 | * 1. Redistributions of source code must retain the above copyright |
37 | * notice, this list of conditions and the following disclaimer. | | 37 | * notice, this list of conditions and the following disclaimer. |
38 | * 2. Redistributions in binary form must reproduce the above copyright | | 38 | * 2. Redistributions in binary form must reproduce the above copyright |
39 | * notice, this list of conditions and the following disclaimer in the | | 39 | * notice, this list of conditions and the following disclaimer in the |
40 | * documentation and/or other materials provided with the distribution. | | 40 | * documentation and/or other materials provided with the distribution. |
41 | * | | 41 | * |
42 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | | 42 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | 43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | | 45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
52 | * SUCH DAMAGE. | | 52 | * SUCH DAMAGE. |
53 | */ | | 53 | */ |
54 | | | 54 | |
55 | /**************************************************************************** | | 55 | /**************************************************************************** |
56 | * | | 56 | * |
57 | * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>. | | 57 | * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>. |
58 | * All rights reserved. | | 58 | * All rights reserved. |
59 | * | | 59 | * |
60 | * Redistribution and use in source and binary forms, with or without | | 60 | * Redistribution and use in source and binary forms, with or without |
61 | * modification, are permitted provided that the following conditions | | 61 | * modification, are permitted provided that the following conditions |
62 | * are met: | | 62 | * are met: |
63 | * 1. Redistributions of source code must retain the above copyright | | 63 | * 1. Redistributions of source code must retain the above copyright |
64 | * notice(s), this list of conditions and the following disclaimer as | | 64 | * notice(s), this list of conditions and the following disclaimer as |
65 | * the first lines of this file unmodified other than the possible | | 65 | * the first lines of this file unmodified other than the possible |
66 | * addition of one or more copyright notices. | | 66 | * addition of one or more copyright notices. |
67 | * 2. Redistributions in binary form must reproduce the above copyright | | 67 | * 2. Redistributions in binary form must reproduce the above copyright |
68 | * notice(s), this list of conditions and the following disclaimer in | | 68 | * notice(s), this list of conditions and the following disclaimer in |
69 | * the documentation and/or other materials provided with the | | 69 | * the documentation and/or other materials provided with the |
70 | * distribution. | | 70 | * distribution. |
71 | * | | 71 | * |
72 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY | | 72 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY |
73 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | 73 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
74 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 74 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
75 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE | | 75 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE |
76 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 76 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
77 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 77 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
78 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | | 78 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
79 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | | 79 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
80 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | | 80 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
81 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | | 81 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
82 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | 82 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
83 | * | | 83 | * |
84 | ****************************************************************************/ | | 84 | ****************************************************************************/ |
85 | | | 85 | |
86 | #include <sys/cdefs.h> | | 86 | #include <sys/cdefs.h> |
87 | __COPYRIGHT("@(#) Copyright (c) 2008, 2010\ | | 87 | __COPYRIGHT("@(#) Copyright (c) 2008, 2010\ |
88 | The NetBSD Foundation, inc. All rights reserved."); | | 88 | The NetBSD Foundation, inc. All rights reserved."); |
89 | __RCSID("$NetBSD: t_sem.c,v 1.6 2012/03/07 23:31:44 joerg Exp $"); | | 89 | __RCSID("$NetBSD: t_sem.c,v 1.7 2012/03/09 19:46:37 joerg Exp $"); |
90 | | | 90 | |
91 | #include <errno.h> | | 91 | #include <errno.h> |
92 | #include <fcntl.h> | | 92 | #include <fcntl.h> |
93 | #include <pthread.h> | | 93 | #include <pthread.h> |
94 | #include <semaphore.h> | | 94 | #include <semaphore.h> |
95 | #include <signal.h> | | 95 | #include <signal.h> |
96 | #include <stdio.h> | | 96 | #include <stdio.h> |
97 | #include <stdlib.h> | | 97 | #include <stdlib.h> |
98 | #include <string.h> | | 98 | #include <string.h> |
99 | #include <unistd.h> | | 99 | #include <unistd.h> |
100 | | | 100 | |
101 | #include <atf-c.h> | | 101 | #include <atf-c.h> |
102 | #include <atf-c/config.h> | | 102 | #include <atf-c/config.h> |
103 | | | 103 | |
104 | #include "h_common.h" | | 104 | #include "h_common.h" |
105 | | | 105 | |
106 | #define NTHREADS 10 | | 106 | #define NTHREADS 10 |
107 | | | 107 | |
108 | #define _LIBC_R_ | | 108 | #define _LIBC_R_ |
109 | | | 109 | |
110 | #define SEM_REQUIRE(x) \ | | 110 | #define SEM_REQUIRE(x) \ |
111 | ATF_REQUIRE_EQ_MSG(x, 0, "%s", strerror(errno)) | | 111 | ATF_REQUIRE_EQ_MSG(x, 0, "%s", strerror(errno)) |
112 | | | 112 | |
113 | static sem_t sem; | | 113 | static sem_t sem; |
114 | | | 114 | |
115 | ATF_TC(named); | | 115 | ATF_TC(named); |
116 | ATF_TC_HEAD(named, tc) | | 116 | ATF_TC_HEAD(named, tc) |
117 | { | | 117 | { |
118 | atf_tc_set_md_var(tc, "descr", "Checks named semaphores"); | | 118 | atf_tc_set_md_var(tc, "descr", "Checks named semaphores"); |
119 | } | | 119 | } |
120 | ATF_TC_BODY(named, tc) | | 120 | ATF_TC_BODY(named, tc) |
121 | { | | 121 | { |
122 | sem_t *semp; | | 122 | sem_t *semp; |
123 | | | 123 | |
124 | ATF_REQUIRE_MSG(-1 != sysconf(_SC_SEMAPHORES), "%s", strerror(errno)); | | 124 | ATF_REQUIRE_MSG(-1 != sysconf(_SC_SEMAPHORES), "%s", strerror(errno)); |
125 | | | 125 | |
126 | printf("Test begin\n"); | | 126 | printf("Test begin\n"); |
127 | | | 127 | |
128 | (void) sem_unlink("/foo"); | | 128 | (void) sem_unlink("/foo"); |
129 | semp = sem_open("/foo", O_CREAT | O_EXCL, 0644, 0); | | 129 | semp = sem_open("/foo", O_CREAT | O_EXCL, 0644, 0); |
130 | ATF_REQUIRE_MSG(semp != SEM_FAILED, "%s", strerror(errno)); | | 130 | ATF_REQUIRE_MSG(semp != SEM_FAILED, "%s", strerror(errno)); |
131 | SEM_REQUIRE(sem_close(semp)); | | 131 | SEM_REQUIRE(sem_close(semp)); |
132 | SEM_REQUIRE(sem_unlink("/foo")); | | 132 | SEM_REQUIRE(sem_unlink("/foo")); |
133 | | | 133 | |
134 | printf("Test end\n"); | | 134 | printf("Test end\n"); |
135 | } | | 135 | } |
136 | | | 136 | |
137 | ATF_TC(unnamed); | | 137 | ATF_TC(unnamed); |
138 | ATF_TC_HEAD(unnamed, tc) | | 138 | ATF_TC_HEAD(unnamed, tc) |
139 | { | | 139 | { |
140 | atf_tc_set_md_var(tc, "descr", "Checks unnamed semaphores"); | | 140 | atf_tc_set_md_var(tc, "descr", "Checks unnamed semaphores"); |
141 | } | | 141 | } |
142 | | | 142 | |
143 | static void * | | 143 | static void * |
144 | entry(void * a_arg) | | 144 | entry(void * a_arg) |
145 | { | | 145 | { |
146 | pthread_t self = pthread_self(); | | 146 | pthread_t self = pthread_self(); |
147 | sem_t *semp = (sem_t *) a_arg; | | 147 | sem_t *semp = (sem_t *) a_arg; |
148 | | | 148 | |
149 | printf("Thread %p waiting for semaphore...\n", self); | | 149 | printf("Thread %p waiting for semaphore...\n", self); |
150 | sem_wait(semp); | | 150 | sem_wait(semp); |
151 | printf("Thread %p got semaphore\n", self); | | 151 | printf("Thread %p got semaphore\n", self); |
152 | | | 152 | |
153 | return NULL; | | 153 | return NULL; |
154 | } | | 154 | } |
155 | | | 155 | |
156 | ATF_TC_BODY(unnamed, tc) | | 156 | ATF_TC_BODY(unnamed, tc) |
157 | { | | 157 | { |
158 | sem_t sem_a, sem_b; | | 158 | sem_t sem_a, sem_b; |
159 | pthread_t threads[NTHREADS]; | | 159 | pthread_t threads[NTHREADS]; |
160 | unsigned i, j; | | 160 | unsigned i, j; |
161 | int val; | | 161 | int val; |
162 | | | 162 | |
163 | ATF_REQUIRE_MSG(-1 != sysconf(_SC_SEMAPHORES), "%s", strerror(errno)); | | 163 | ATF_REQUIRE_MSG(-1 != sysconf(_SC_SEMAPHORES), "%s", strerror(errno)); |
164 | | | 164 | |
165 | printf("Test begin\n"); | | 165 | printf("Test begin\n"); |
166 | | | 166 | |
167 | SEM_REQUIRE(sem_init(&sem_b, 0, 0)); | | 167 | SEM_REQUIRE(sem_init(&sem_b, 0, 0)); |
168 | SEM_REQUIRE(sem_getvalue(&sem_b, &val)); | | 168 | SEM_REQUIRE(sem_getvalue(&sem_b, &val)); |
169 | ATF_REQUIRE_EQ(0, val); | | 169 | ATF_REQUIRE_EQ(0, val); |
170 | | | 170 | |
171 | SEM_REQUIRE(sem_post(&sem_b)); | | 171 | SEM_REQUIRE(sem_post(&sem_b)); |
172 | SEM_REQUIRE(sem_getvalue(&sem_b, &val)); | | 172 | SEM_REQUIRE(sem_getvalue(&sem_b, &val)); |
173 | ATF_REQUIRE_EQ(1, val); | | 173 | ATF_REQUIRE_EQ(1, val); |
174 | | | 174 | |
175 | SEM_REQUIRE(sem_wait(&sem_b)); | | 175 | SEM_REQUIRE(sem_wait(&sem_b)); |
176 | ATF_REQUIRE_EQ(sem_trywait(&sem_b), -1); | | 176 | ATF_REQUIRE_EQ(sem_trywait(&sem_b), -1); |
177 | ATF_REQUIRE_EQ(errno, EAGAIN); | | 177 | ATF_REQUIRE_EQ(errno, EAGAIN); |
178 | SEM_REQUIRE(sem_post(&sem_b)); | | 178 | SEM_REQUIRE(sem_post(&sem_b)); |
179 | SEM_REQUIRE(sem_trywait(&sem_b)); | | 179 | SEM_REQUIRE(sem_trywait(&sem_b)); |
180 | SEM_REQUIRE(sem_post(&sem_b)); | | 180 | SEM_REQUIRE(sem_post(&sem_b)); |
181 | SEM_REQUIRE(sem_wait(&sem_b)); | | 181 | SEM_REQUIRE(sem_wait(&sem_b)); |
182 | SEM_REQUIRE(sem_post(&sem_b)); | | 182 | SEM_REQUIRE(sem_post(&sem_b)); |
183 | | | 183 | |
184 | SEM_REQUIRE(sem_destroy(&sem_b)); | | 184 | SEM_REQUIRE(sem_destroy(&sem_b)); |
185 | | | 185 | |
186 | SEM_REQUIRE(sem_init(&sem_a, 0, 0)); | | 186 | SEM_REQUIRE(sem_init(&sem_a, 0, 0)); |
187 | | | 187 | |
188 | for (j = 0; j < 2; j++) { | | 188 | for (j = 0; j < 2; j++) { |
189 | for (i = 0; i < NTHREADS; i++) { | | 189 | for (i = 0; i < NTHREADS; i++) { |
190 | PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, | | 190 | PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, |
191 | entry, (void *) &sem_a)); | | 191 | entry, (void *) &sem_a)); |
192 | } | | 192 | } |
193 | | | 193 | |
194 | for (i = 0; i < NTHREADS; i++) { | | 194 | for (i = 0; i < NTHREADS; i++) { |
195 | usleep(10000); | | 195 | usleep(10000); |
196 | printf("main loop %u: posting...\n", j+1); | | 196 | printf("main loop %u: posting...\n", j+1); |
197 | SEM_REQUIRE(sem_post(&sem_a)); | | 197 | SEM_REQUIRE(sem_post(&sem_a)); |
198 | } | | 198 | } |
199 | | | 199 | |
200 | for (i = 0; i < NTHREADS; i++) { | | 200 | for (i = 0; i < NTHREADS; i++) { |
201 | PTHREAD_REQUIRE(pthread_join(threads[i], NULL)); | | 201 | PTHREAD_REQUIRE(pthread_join(threads[i], NULL)); |
202 | } | | 202 | } |
203 | } | | 203 | } |
204 | | | 204 | |
205 | SEM_REQUIRE(sem_destroy(&sem_a)); | | 205 | SEM_REQUIRE(sem_destroy(&sem_a)); |
206 | | | 206 | |
207 | printf("Test end\n"); | | 207 | printf("Test end\n"); |
208 | } | | 208 | } |
209 | | | 209 | |
210 | static void | | 210 | static void |
211 | sighandler(int signo) | | 211 | sighandler(int signo) |
212 | { | | 212 | { |
213 | /* printf("signal %d\n", signo); */ | | 213 | /* printf("signal %d\n", signo); */ |
214 | | | 214 | |
215 | ATF_REQUIRE_EQ_MSG(signo, SIGALRM, "unexpected signal"); | | 215 | ATF_REQUIRE_EQ_MSG(signo, SIGALRM, "unexpected signal"); |
216 | SEM_REQUIRE(sem_post(&sem)); | | 216 | SEM_REQUIRE(sem_post(&sem)); |
217 | } | | 217 | } |
218 | | | 218 | |
219 | static void | | 219 | static void |
220 | alarm_ms(const int ms) | | 220 | alarm_ms(const int ms) |
221 | { | | 221 | { |
222 | struct itimerval timer; | | 222 | struct itimerval timer; |
223 | timer.it_interval.tv_sec = 0; | | 223 | timer.it_interval.tv_sec = 0; |
224 | timer.it_interval.tv_usec = 0; | | 224 | timer.it_interval.tv_usec = 0; |
225 | timer.it_value.tv_sec = 0; | | 225 | timer.it_value.tv_sec = 0; |
226 | timer.it_value.tv_usec = ms * 1000; | | 226 | timer.it_value.tv_usec = ms * 1000; |
227 | ATF_REQUIRE(setitimer(ITIMER_REAL, &timer, NULL) == 0); | | 227 | ATF_REQUIRE(setitimer(ITIMER_REAL, &timer, NULL) == 0); |
228 | } | | 228 | } |
229 | | | 229 | |
230 | static void * | | 230 | static void * |
231 | threadfunc(void *arg) | | 231 | threadfunc(void *arg) |
232 | { | | 232 | { |
233 | int i; | | 233 | int i, ret; |
234 | | | 234 | |
235 | printf("Entering loop\n"); | | 235 | printf("Entering loop\n"); |
236 | for (i = 0; i < 500; ) { | | 236 | for (i = 0; i < 500; ) { |
237 | if ((i & 1) != 0) { | | 237 | if ((i & 1) != 0) { |
238 | ATF_REQUIRE(sem_wait(&sem) != -1); | | 238 | do { |
| | | 239 | ret = sem_wait(&sem); |
| | | 240 | } while (ret == -1 && errno == EINTR); |
| | | 241 | ATF_REQUIRE(ret == 0); |
239 | } else { | | 242 | } else { |
240 | const int ret = sem_trywait(&sem); | | 243 | ret = sem_trywait(&sem); |
241 | if (ret == -1) { | | 244 | if (ret == -1) { |
242 | ATF_REQUIRE(errno == EAGAIN); | | 245 | ATF_REQUIRE(errno == EAGAIN); |
243 | continue; | | 246 | continue; |
244 | } | | 247 | } |
245 | } | | 248 | } |
246 | printf("%s: %d\n", __func__, i); | | 249 | printf("%s: %d\n", __func__, i); |
247 | alarm_ms(5); | | 250 | alarm_ms(5); |
248 | i++; | | 251 | i++; |
249 | } | | 252 | } |
250 | | | 253 | |
251 | return NULL; | | 254 | return NULL; |
252 | } | | 255 | } |
253 | | | 256 | |
254 | static void | | 257 | static void |
255 | before_start_test(const bool use_pthread) | | 258 | before_start_test(const bool use_pthread) |
256 | { | | 259 | { |
257 | pthread_t t; | | 260 | pthread_t t; |
258 | | | 261 | |
259 | SEM_REQUIRE(sem_init(&sem, 0, 0)); | | 262 | SEM_REQUIRE(sem_init(&sem, 0, 0)); |
260 | ATF_REQUIRE(SIG_ERR != signal(SIGALRM, sighandler)); | | 263 | ATF_REQUIRE(SIG_ERR != signal(SIGALRM, sighandler)); |
261 | | | 264 | |
262 | alarm_ms(5); | | 265 | alarm_ms(5); |
263 | | | 266 | |
264 | if (use_pthread) { | | 267 | if (use_pthread) { |
265 | PTHREAD_REQUIRE(pthread_create(&t, NULL, threadfunc, NULL)); | | 268 | PTHREAD_REQUIRE(pthread_create(&t, NULL, threadfunc, NULL)); |
266 | PTHREAD_REQUIRE(pthread_join(t, NULL)); | | 269 | PTHREAD_REQUIRE(pthread_join(t, NULL)); |
267 | } else { | | 270 | } else { |
268 | threadfunc(NULL); | | 271 | threadfunc(NULL); |
269 | } | | 272 | } |
270 | } | | 273 | } |
271 | | | 274 | |
272 | ATF_TC(before_start_no_threads); | | 275 | ATF_TC(before_start_no_threads); |
273 | ATF_TC_HEAD(before_start_no_threads, tc) | | 276 | ATF_TC_HEAD(before_start_no_threads, tc) |
274 | { | | 277 | { |
275 | atf_tc_set_md_var(tc, "descr", "Checks using semaphores without any " | | 278 | atf_tc_set_md_var(tc, "descr", "Checks using semaphores without any " |
276 | "thread running"); | | 279 | "thread running"); |
277 | atf_tc_set_md_var(tc, "timeout", "40"); | | 280 | atf_tc_set_md_var(tc, "timeout", "40"); |
278 | } | | 281 | } |
279 | ATF_TC_BODY(before_start_no_threads, tc) | | 282 | ATF_TC_BODY(before_start_no_threads, tc) |
280 | { | | 283 | { |
281 | before_start_test(false); | | 284 | before_start_test(false); |
282 | } | | 285 | } |
283 | | | 286 | |
284 | ATF_TC(before_start_one_thread); | | 287 | ATF_TC(before_start_one_thread); |
285 | ATF_TC_HEAD(before_start_one_thread, tc) | | 288 | ATF_TC_HEAD(before_start_one_thread, tc) |
286 | { | | 289 | { |
287 | atf_tc_set_md_var(tc, "descr", "Checks using semaphores before " | | 290 | atf_tc_set_md_var(tc, "descr", "Checks using semaphores before " |
288 | "starting one thread"); | | 291 | "starting one thread"); |
289 | atf_tc_set_md_var(tc, "timeout", "40"); | | 292 | atf_tc_set_md_var(tc, "timeout", "40"); |
290 | } | | 293 | } |
291 | ATF_TC_BODY(before_start_one_thread, tc) | | 294 | ATF_TC_BODY(before_start_one_thread, tc) |
292 | { | | 295 | { |
293 | before_start_test(true); | | 296 | before_start_test(true); |
294 | } | | 297 | } |
295 | | | 298 | |
296 | ATF_TP_ADD_TCS(tp) | | 299 | ATF_TP_ADD_TCS(tp) |
297 | { | | 300 | { |
298 | ATF_TP_ADD_TC(tp, named); | | 301 | ATF_TP_ADD_TC(tp, named); |
299 | ATF_TP_ADD_TC(tp, unnamed); | | 302 | ATF_TP_ADD_TC(tp, unnamed); |
300 | ATF_TP_ADD_TC(tp, before_start_no_threads); | | 303 | ATF_TP_ADD_TC(tp, before_start_no_threads); |
301 | ATF_TP_ADD_TC(tp, before_start_one_thread); | | 304 | ATF_TP_ADD_TC(tp, before_start_one_thread); |
302 | | | 305 | |
303 | return atf_no_error(); | | 306 | return atf_no_error(); |
304 | } | | 307 | } |