1 /* $NetBSD: t_sem.c,v 1.9 2017/01/16 16:22:22 christos 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@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.9 2017/01/16 16:22:22 christos Exp $"); 90 91 #include <sys/time.h> 92 #include <errno.h> 93 #include <fcntl.h> 94 #include <pthread.h> 95 #include <semaphore.h> 96 #include <signal.h> 97 #include <stdio.h> 98 #include <stdlib.h> 99 #include <string.h> 100 #include <unistd.h> 101 102 #include <atf-c.h> 103 104 #include "h_common.h" 105 106 #define NTHREADS 10 107 108 #define _LIBC_R_ 109 110 #define SEM_REQUIRE(x) \ 111 ATF_REQUIRE_EQ_MSG(x, 0, "%s", strerror(errno)) 112 113 static sem_t sem; 114 115 #ifdef __FreeBSD__ 116 #include <sys/time.h> 117 #endif 118 119 ATF_TC(named); 120 ATF_TC_HEAD(named, tc) 121 { 122 atf_tc_set_md_var(tc, "descr", "Checks named semaphores"); 123 } 124 ATF_TC_BODY(named, tc) 125 { 126 sem_t *semp; 127 128 ATF_REQUIRE_MSG(-1 != sysconf(_SC_SEMAPHORES), "%s", strerror(errno)); 129 130 printf("Test begin\n"); 131 132 (void) sem_unlink("/foo"); 133 semp = sem_open("/foo", O_CREAT | O_EXCL, 0644, 0); 134 ATF_REQUIRE_MSG(semp != SEM_FAILED, "%s", strerror(errno)); 135 SEM_REQUIRE(sem_close(semp)); 136 SEM_REQUIRE(sem_unlink("/foo")); 137 138 printf("Test end\n"); 139 } 140 141 ATF_TC(unnamed); 142 ATF_TC_HEAD(unnamed, tc) 143 { 144 atf_tc_set_md_var(tc, "descr", "Checks unnamed semaphores"); 145 } 146 147 static void * 148 entry(void * a_arg) 149 { 150 pthread_t self = pthread_self(); 151 sem_t *semp = (sem_t *) a_arg; 152 153 printf("Thread %p waiting for semaphore...\n", self); 154 sem_wait(semp); 155 printf("Thread %p got semaphore\n", self); 156 157 return NULL; 158 } 159 160 ATF_TC_BODY(unnamed, tc) 161 { 162 sem_t sem_a, sem_b; 163 pthread_t threads[NTHREADS]; 164 unsigned i, j; 165 int val; 166 167 ATF_REQUIRE_MSG(-1 != sysconf(_SC_SEMAPHORES), "%s", strerror(errno)); 168 169 printf("Test begin\n"); 170 171 SEM_REQUIRE(sem_init(&sem_b, 0, 0)); 172 SEM_REQUIRE(sem_getvalue(&sem_b, &val)); 173 ATF_REQUIRE_EQ(0, val); 174 175 SEM_REQUIRE(sem_post(&sem_b)); 176 SEM_REQUIRE(sem_getvalue(&sem_b, &val)); 177 ATF_REQUIRE_EQ(1, val); 178 179 SEM_REQUIRE(sem_wait(&sem_b)); 180 ATF_REQUIRE_EQ(sem_trywait(&sem_b), -1); 181 ATF_REQUIRE_EQ(errno, EAGAIN); 182 SEM_REQUIRE(sem_post(&sem_b)); 183 SEM_REQUIRE(sem_trywait(&sem_b)); 184 SEM_REQUIRE(sem_post(&sem_b)); 185 SEM_REQUIRE(sem_wait(&sem_b)); 186 SEM_REQUIRE(sem_post(&sem_b)); 187 188 SEM_REQUIRE(sem_destroy(&sem_b)); 189 190 SEM_REQUIRE(sem_init(&sem_a, 0, 0)); 191 192 for (j = 0; j < 2; j++) { 193 for (i = 0; i < NTHREADS; i++) { 194 PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, 195 entry, (void *) &sem_a)); 196 } 197 198 for (i = 0; i < NTHREADS; i++) { 199 usleep(10000); 200 printf("main loop %u: posting...\n", j+1); 201 SEM_REQUIRE(sem_post(&sem_a)); 202 } 203 204 for (i = 0; i < NTHREADS; i++) { 205 PTHREAD_REQUIRE(pthread_join(threads[i], NULL)); 206 } 207 } 208 209 SEM_REQUIRE(sem_destroy(&sem_a)); 210 211 printf("Test end\n"); 212 } 213 214 static void 215 sighandler(int signo) 216 { 217 /* printf("signal %d\n", signo); */ 218 219 ATF_REQUIRE_EQ_MSG(signo, SIGALRM, "unexpected signal"); 220 SEM_REQUIRE(sem_post(&sem)); 221 } 222 223 static void 224 alarm_ms(const int ms) 225 { 226 struct itimerval timer; 227 timer.it_interval.tv_sec = 0; 228 timer.it_interval.tv_usec = 0; 229 timer.it_value.tv_sec = 0; 230 timer.it_value.tv_usec = ms * 1000; 231 ATF_REQUIRE(setitimer(ITIMER_REAL, &timer, NULL) == 0); 232 } 233 234 static void * 235 threadfunc(void *arg) 236 { 237 int i, ret; 238 239 printf("Entering loop\n"); 240 for (i = 0; i < 500; ) { 241 if ((i & 1) != 0) { 242 do { 243 ret = sem_wait(&sem); 244 } while (ret == -1 && errno == EINTR); 245 ATF_REQUIRE(ret == 0); 246 } else { 247 ret = sem_trywait(&sem); 248 if (ret == -1) { 249 ATF_REQUIRE(errno == EAGAIN); 250 continue; 251 } 252 } 253 printf("%s: %d\n", __func__, i); 254 alarm_ms(5); 255 i++; 256 } 257 258 return NULL; 259 } 260 261 static void 262 before_start_test(const bool use_pthread) 263 { 264 pthread_t t; 265 266 SEM_REQUIRE(sem_init(&sem, 0, 0)); 267 ATF_REQUIRE(SIG_ERR != signal(SIGALRM, sighandler)); 268 269 alarm_ms(5); 270 271 if (use_pthread) { 272 PTHREAD_REQUIRE(pthread_create(&t, NULL, threadfunc, NULL)); 273 PTHREAD_REQUIRE(pthread_join(t, NULL)); 274 } else { 275 threadfunc(NULL); 276 } 277 } 278 279 ATF_TC(before_start_no_threads); 280 ATF_TC_HEAD(before_start_no_threads, tc) 281 { 282 atf_tc_set_md_var(tc, "descr", "Checks using semaphores without any " 283 "thread running"); 284 atf_tc_set_md_var(tc, "timeout", "40"); 285 } 286 ATF_TC_BODY(before_start_no_threads, tc) 287 { 288 before_start_test(false); 289 } 290 291 ATF_TC(before_start_one_thread); 292 ATF_TC_HEAD(before_start_one_thread, tc) 293 { 294 atf_tc_set_md_var(tc, "descr", "Checks using semaphores before " 295 "starting one thread"); 296 atf_tc_set_md_var(tc, "timeout", "40"); 297 } 298 ATF_TC_BODY(before_start_one_thread, tc) 299 { 300 before_start_test(true); 301 } 302 303 ATF_TP_ADD_TCS(tp) 304 { 305 ATF_TP_ADD_TC(tp, named); 306 ATF_TP_ADD_TC(tp, unnamed); 307 ATF_TP_ADD_TC(tp, before_start_no_threads); 308 ATF_TP_ADD_TC(tp, before_start_one_thread); 309 310 return atf_no_error(); 311 } 312