Index: projects/netbsd-tests-upstream-01-2017/contrib/netbsd-tests/lib/libpthread/t_detach.c =================================================================== --- projects/netbsd-tests-upstream-01-2017/contrib/netbsd-tests/lib/libpthread/t_detach.c (revision 312301) +++ projects/netbsd-tests-upstream-01-2017/contrib/netbsd-tests/lib/libpthread/t_detach.c (revision 312302) @@ -1,100 +1,96 @@ /* $NetBSD: t_detach.c,v 1.2 2017/01/16 16:29:54 christos Exp $ */ /*- * Copyright (c) 2011 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Jukka Ruohonen. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include __RCSID("$NetBSD: t_detach.c,v 1.2 2017/01/16 16:29:54 christos Exp $"); #include #include #include #include #include "h_common.h" -#ifdef __FreeBSD__ -#include -#endif - static void *func(void *); static void * func(void *arg) { sleep(2); return NULL; } ATF_TC(pthread_detach); ATF_TC_HEAD(pthread_detach, tc) { atf_tc_set_md_var(tc, "descr", "A test of pthread_detach(3)"); } ATF_TC_BODY(pthread_detach, tc) { const int state = PTHREAD_CREATE_JOINABLE; pthread_attr_t attr; pthread_t t; int rv; /* * Create a joinable thread. */ PTHREAD_REQUIRE(pthread_attr_init(&attr)); PTHREAD_REQUIRE(pthread_attr_setdetachstate(&attr, state)); PTHREAD_REQUIRE(pthread_create(&t, &attr, func, NULL)); /* * Detach the thread and try to * join it; EINVAL should follow. */ PTHREAD_REQUIRE(pthread_detach(t)); sleep(1); rv = pthread_join(t, NULL); ATF_REQUIRE(rv == EINVAL); sleep(3); /* * As usual, ESRCH should follow if * we try to detach an invalid thread. */ rv = pthread_cancel(t); ATF_REQUIRE(rv == ESRCH); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, pthread_detach); return atf_no_error(); } Index: projects/netbsd-tests-upstream-01-2017/contrib/netbsd-tests/lib/libpthread/t_fpu.c =================================================================== --- projects/netbsd-tests-upstream-01-2017/contrib/netbsd-tests/lib/libpthread/t_fpu.c (revision 312301) +++ projects/netbsd-tests-upstream-01-2017/contrib/netbsd-tests/lib/libpthread/t_fpu.c (revision 312302) @@ -1,157 +1,152 @@ /* $NetBSD: t_fpu.c,v 1.3 2017/01/16 16:27:43 christos Exp $ */ /* * Copyright (c) 2008 The NetBSD Foundation, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include __COPYRIGHT("@(#) Copyright (c) 2008\ The NetBSD Foundation, inc. All rights reserved."); __RCSID("$NetBSD: t_fpu.c,v 1.3 2017/01/16 16:27:43 christos Exp $"); /* * This is adapted from part of csw/cstest of the MPD implementation by * the University of Arizona CS department (http://www.cs.arizona.edu/sr/) * which is in the public domain: * * "The MPD system is in the public domain and you may use and distribute it * as you wish. We ask that you retain credits referencing the University * of Arizona and that you identify any changes you make. * * We can't provide a warranty with MPD; it's up to you to determine its * suitability and reliability for your needs. We would like to hear of * any problems you encounter but we cannot promise a timely correction." * * It was changed to use pthread_create() and sched_yield() instead of * the internal MPD context switching primitives by Ignatios Souvatzis * . */ #include #include #include #include #include #include #include #include #include -#ifdef __FreeBSD__ -#include -#include -#endif - #include "h_common.h" #define N_RECURSE 10 static void recurse(void); int recursion_depth = 0; pthread_mutex_t recursion_depth_lock; static void * stir(void *p) { double *q = (double *)p; double x = *q++; double y = *q++; double z = *q++; for (;;) { x = sin ((y = cos (x + y + .4)) - (z = cos (x + z + .6))); ATF_REQUIRE_MSG(sched_yield() == 0, "sched_yield failed: %s", strerror(errno)); } } static double mul3(double x, double y, double z) { ATF_REQUIRE_MSG(sched_yield() == 0, "sched_yield failed: %s", strerror(errno)); return x * y * z; } static void * bar(void *p) { double d; int rc; d = mul3(mul3(2., 3., 5.), mul3(7., 11., 13.), mul3(17., 19., 23.)); ATF_REQUIRE_EQ(d, 223092870.); PTHREAD_REQUIRE(pthread_mutex_lock(&recursion_depth_lock)); rc = recursion_depth++; PTHREAD_REQUIRE(pthread_mutex_unlock(&recursion_depth_lock)); if (rc < N_RECURSE) recurse(); else atf_tc_pass(); /* NOTREACHED */ return NULL; } static void recurse(void) { pthread_t s2; PTHREAD_REQUIRE(pthread_create(&s2, 0, bar, 0)); sleep(20); /* XXX must be long enough for our slowest machine */ } ATF_TC(fpu); ATF_TC_HEAD(fpu, tc) { atf_tc_set_md_var(tc, "descr", "Checks that thread context switches will leave the " "floating point computations unharmed"); } ATF_TC_BODY(fpu, tc) { double stirseed[] = { 1.7, 3.2, 2.4 }; pthread_t s5; printf("Testing threaded floating point computations...\n"); PTHREAD_REQUIRE(pthread_mutex_init(&recursion_depth_lock, 0)); PTHREAD_REQUIRE(pthread_create(&s5, 0, stir, stirseed)); recurse(); atf_tc_fail("exiting from main"); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, fpu); return atf_no_error(); } Index: projects/netbsd-tests-upstream-01-2017/contrib/netbsd-tests/lib/libpthread/t_once.c =================================================================== --- projects/netbsd-tests-upstream-01-2017/contrib/netbsd-tests/lib/libpthread/t_once.c (revision 312301) +++ projects/netbsd-tests-upstream-01-2017/contrib/netbsd-tests/lib/libpthread/t_once.c (revision 312302) @@ -1,200 +1,199 @@ /* $NetBSD: t_once.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ /* * Copyright (c) 2008 The NetBSD Foundation, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include __COPYRIGHT("@(#) Copyright (c) 2008\ The NetBSD Foundation, inc. All rights reserved."); __RCSID("$NetBSD: t_once.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); +#ifdef __FreeBSD__ +#include /* For itimer*, etc. */ +#endif #include #include #include #include #include #include "h_common.h" static pthread_once_t once = PTHREAD_ONCE_INIT; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static int x; #define NTHREADS 25 - -#ifdef __FreeBSD__ -#include -#endif static void ofunc(void) { printf("Variable x has value %d\n", x); x++; } ATF_TC(once1); ATF_TC_HEAD(once1, tc) { atf_tc_set_md_var(tc, "descr", "Checks pthread_once()"); } ATF_TC_BODY(once1, tc) { printf("1: Test 1 of pthread_once()\n"); PTHREAD_REQUIRE(pthread_once(&once, ofunc)); PTHREAD_REQUIRE(pthread_once(&once, ofunc)); printf("1: X has value %d\n",x ); ATF_REQUIRE_EQ(x, 1); } static void once2_ofunc(void) { x++; printf("ofunc: Variable x has value %d\n", x); x++; } static void * once2_threadfunc(void *arg) { int num; PTHREAD_REQUIRE(pthread_once(&once, once2_ofunc)); num = *(int *)arg; printf("Thread %d sees x with value %d\n", num, x); ATF_REQUIRE_EQ(x, 2); return NULL; } ATF_TC(once2); ATF_TC_HEAD(once2, tc) { atf_tc_set_md_var(tc, "descr", "Checks pthread_once()"); } ATF_TC_BODY(once2, tc) { pthread_t threads[NTHREADS]; int id[NTHREADS]; int i; printf("1: Test 2 of pthread_once()\n"); for (i=0; i < NTHREADS; i++) { id[i] = i; PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, once2_threadfunc, &id[i])); } for (i=0; i < NTHREADS; i++) PTHREAD_REQUIRE(pthread_join(threads[i], NULL)); printf("1: X has value %d\n",x ); ATF_REQUIRE_EQ(x, 2); } static void once3_cleanup(void *m) { pthread_mutex_t *mu = m; PTHREAD_REQUIRE(pthread_mutex_unlock(mu)); } static void once3_ofunc(void) { pthread_testcancel(); } static void * once3_threadfunc(void *arg) { PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); pthread_cleanup_push(once3_cleanup, &mutex); PTHREAD_REQUIRE(pthread_once(&once, once3_ofunc)); pthread_cleanup_pop(1); return NULL; } static void handler(int sig, siginfo_t *info, void *ctx) { atf_tc_fail("Signal handler was called; " "main thread deadlocked in pthread_once()"); } ATF_TC(once3); ATF_TC_HEAD(once3, tc) { atf_tc_set_md_var(tc, "descr", "Checks pthread_once()"); } ATF_TC_BODY(once3, tc) { pthread_t thread; struct sigaction act; struct itimerval it; printf("Test 3 of pthread_once() (test versus cancellation)\n"); act.sa_sigaction = handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_SIGINFO; sigaction(SIGALRM, &act, NULL); timerclear(&it.it_value); it.it_value.tv_usec = 500000; timerclear(&it.it_interval); setitimer(ITIMER_REAL, &it, NULL); PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); PTHREAD_REQUIRE(pthread_create(&thread, NULL, once3_threadfunc, NULL)); PTHREAD_REQUIRE(pthread_cancel(thread)); PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); PTHREAD_REQUIRE(pthread_join(thread, NULL)); PTHREAD_REQUIRE(pthread_once(&once, ofunc)); /* Cancel timer */ timerclear(&it.it_value); setitimer(ITIMER_REAL, &it, NULL); printf("Test succeeded\n"); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, once1); ATF_TP_ADD_TC(tp, once2); ATF_TP_ADD_TC(tp, once3); return atf_no_error(); } Index: projects/netbsd-tests-upstream-01-2017/contrib/netbsd-tests/lib/libpthread/t_sem.c =================================================================== --- projects/netbsd-tests-upstream-01-2017/contrib/netbsd-tests/lib/libpthread/t_sem.c (revision 312301) +++ projects/netbsd-tests-upstream-01-2017/contrib/netbsd-tests/lib/libpthread/t_sem.c (revision 312302) @@ -1,311 +1,307 @@ /* $NetBSD: t_sem.c,v 1.9 2017/01/16 16:22:22 christos Exp $ */ /* * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /*- * Copyright (c)2004 YAMAMOTO Takashi, * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /**************************************************************************** * * Copyright (C) 2000 Jason Evans . * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice(s), this list of conditions and the following disclaimer as * the first lines of this file unmodified other than the possible * addition of one or more copyright notices. * 2. Redistributions in binary form must reproduce the above copyright * notice(s), this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************/ #include __COPYRIGHT("@(#) Copyright (c) 2008, 2010\ The NetBSD Foundation, inc. All rights reserved."); __RCSID("$NetBSD: t_sem.c,v 1.9 2017/01/16 16:22:22 christos Exp $"); #include #include #include #include #include #include #include #include #include #include #include #include "h_common.h" #define NTHREADS 10 #define _LIBC_R_ #define SEM_REQUIRE(x) \ ATF_REQUIRE_EQ_MSG(x, 0, "%s", strerror(errno)) static sem_t sem; -#ifdef __FreeBSD__ -#include -#endif - ATF_TC(named); ATF_TC_HEAD(named, tc) { atf_tc_set_md_var(tc, "descr", "Checks named semaphores"); } ATF_TC_BODY(named, tc) { sem_t *semp; ATF_REQUIRE_MSG(-1 != sysconf(_SC_SEMAPHORES), "%s", strerror(errno)); printf("Test begin\n"); (void) sem_unlink("/foo"); semp = sem_open("/foo", O_CREAT | O_EXCL, 0644, 0); ATF_REQUIRE_MSG(semp != SEM_FAILED, "%s", strerror(errno)); SEM_REQUIRE(sem_close(semp)); SEM_REQUIRE(sem_unlink("/foo")); printf("Test end\n"); } ATF_TC(unnamed); ATF_TC_HEAD(unnamed, tc) { atf_tc_set_md_var(tc, "descr", "Checks unnamed semaphores"); } static void * entry(void * a_arg) { pthread_t self = pthread_self(); sem_t *semp = (sem_t *) a_arg; printf("Thread %p waiting for semaphore...\n", self); sem_wait(semp); printf("Thread %p got semaphore\n", self); return NULL; } ATF_TC_BODY(unnamed, tc) { sem_t sem_a, sem_b; pthread_t threads[NTHREADS]; unsigned i, j; int val; ATF_REQUIRE_MSG(-1 != sysconf(_SC_SEMAPHORES), "%s", strerror(errno)); printf("Test begin\n"); SEM_REQUIRE(sem_init(&sem_b, 0, 0)); SEM_REQUIRE(sem_getvalue(&sem_b, &val)); ATF_REQUIRE_EQ(0, val); SEM_REQUIRE(sem_post(&sem_b)); SEM_REQUIRE(sem_getvalue(&sem_b, &val)); ATF_REQUIRE_EQ(1, val); SEM_REQUIRE(sem_wait(&sem_b)); ATF_REQUIRE_EQ(sem_trywait(&sem_b), -1); ATF_REQUIRE_EQ(errno, EAGAIN); SEM_REQUIRE(sem_post(&sem_b)); SEM_REQUIRE(sem_trywait(&sem_b)); SEM_REQUIRE(sem_post(&sem_b)); SEM_REQUIRE(sem_wait(&sem_b)); SEM_REQUIRE(sem_post(&sem_b)); SEM_REQUIRE(sem_destroy(&sem_b)); SEM_REQUIRE(sem_init(&sem_a, 0, 0)); for (j = 0; j < 2; j++) { for (i = 0; i < NTHREADS; i++) { PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, entry, (void *) &sem_a)); } for (i = 0; i < NTHREADS; i++) { usleep(10000); printf("main loop %u: posting...\n", j+1); SEM_REQUIRE(sem_post(&sem_a)); } for (i = 0; i < NTHREADS; i++) { PTHREAD_REQUIRE(pthread_join(threads[i], NULL)); } } SEM_REQUIRE(sem_destroy(&sem_a)); printf("Test end\n"); } static void sighandler(int signo) { /* printf("signal %d\n", signo); */ ATF_REQUIRE_EQ_MSG(signo, SIGALRM, "unexpected signal"); SEM_REQUIRE(sem_post(&sem)); } static void alarm_ms(const int ms) { struct itimerval timer; timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = 0; timer.it_value.tv_sec = 0; timer.it_value.tv_usec = ms * 1000; ATF_REQUIRE(setitimer(ITIMER_REAL, &timer, NULL) == 0); } static void * threadfunc(void *arg) { int i, ret; printf("Entering loop\n"); for (i = 0; i < 500; ) { if ((i & 1) != 0) { do { ret = sem_wait(&sem); } while (ret == -1 && errno == EINTR); ATF_REQUIRE(ret == 0); } else { ret = sem_trywait(&sem); if (ret == -1) { ATF_REQUIRE(errno == EAGAIN); continue; } } printf("%s: %d\n", __func__, i); alarm_ms(5); i++; } return NULL; } static void before_start_test(const bool use_pthread) { pthread_t t; SEM_REQUIRE(sem_init(&sem, 0, 0)); ATF_REQUIRE(SIG_ERR != signal(SIGALRM, sighandler)); alarm_ms(5); if (use_pthread) { PTHREAD_REQUIRE(pthread_create(&t, NULL, threadfunc, NULL)); PTHREAD_REQUIRE(pthread_join(t, NULL)); } else { threadfunc(NULL); } } ATF_TC(before_start_no_threads); ATF_TC_HEAD(before_start_no_threads, tc) { atf_tc_set_md_var(tc, "descr", "Checks using semaphores without any " "thread running"); atf_tc_set_md_var(tc, "timeout", "40"); } ATF_TC_BODY(before_start_no_threads, tc) { before_start_test(false); } ATF_TC(before_start_one_thread); ATF_TC_HEAD(before_start_one_thread, tc) { atf_tc_set_md_var(tc, "descr", "Checks using semaphores before " "starting one thread"); atf_tc_set_md_var(tc, "timeout", "40"); } ATF_TC_BODY(before_start_one_thread, tc) { before_start_test(true); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, named); ATF_TP_ADD_TC(tp, unnamed); ATF_TP_ADD_TC(tp, before_start_no_threads); ATF_TP_ADD_TC(tp, before_start_one_thread); return atf_no_error(); }