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