Index: head/contrib/netbsd-tests/lib/libpthread/t_condwait.c =================================================================== --- head/contrib/netbsd-tests/lib/libpthread/t_condwait.c (revision 274576) +++ head/contrib/netbsd-tests/lib/libpthread/t_condwait.c (revision 274577) @@ -1,142 +1,146 @@ /* $NetBSD: t_condwait.c,v 1.4 2013/04/12 17:18:11 christos Exp $ */ /* * Copyright (c) 2013 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 __RCSID("$NetBSD: t_condwait.c,v 1.4 2013/04/12 17:18:11 christos Exp $"); #include #include #include #include #include #include #include #include #include "isqemu.h" +#ifdef __FreeBSD__ +#include +#endif + #define WAITTIME 2 /* Timeout wait secound */ static const int debug = 1; static void * run(void *param) { struct timespec ts, to, te; clockid_t clck; pthread_condattr_t attr; pthread_cond_t cond; pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; int ret = 0; clck = *(clockid_t *)param; pthread_condattr_init(&attr); pthread_condattr_setclock(&attr, clck); /* MONOTONIC or MONOTONIC */ pthread_cond_init(&cond, &attr); ATF_REQUIRE_EQ((ret = pthread_mutex_lock(&m)), 0); ATF_REQUIRE_EQ(clock_gettime(clck, &ts), 0); to = ts; if (debug) printf("started: %lld.%09ld sec\n", (long long)to.tv_sec, to.tv_nsec); ts.tv_sec += WAITTIME; /* Timeout wait */ switch (ret = pthread_cond_timedwait(&cond, &m, &ts)) { case ETIMEDOUT: /* Timeout */ ATF_REQUIRE_EQ(clock_gettime(clck, &te), 0); timespecsub(&te, &to, &to); if (debug) { printf("timeout: %lld.%09ld sec\n", (long long)te.tv_sec, te.tv_nsec); printf("elapsed: %lld.%09ld sec\n", (long long)to.tv_sec, to.tv_nsec); } if (isQEMU()) { double to_seconds = to.tv_sec + 1e-9 * to.tv_nsec; ATF_REQUIRE(to_seconds >= WAITTIME * 0.9); /* Loose upper limit because of qemu timing bugs */ ATF_REQUIRE(to_seconds < WAITTIME * 2.5); } else { ATF_REQUIRE_EQ(to.tv_sec, WAITTIME); } break; default: ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret)); } ATF_REQUIRE_MSG(!(ret = pthread_mutex_unlock(&m)), "pthread_mutex_unlock: %s", strerror(ret)); pthread_exit(&ret); } static void cond_wait(clockid_t clck, const char *msg) { pthread_t child; if (debug) printf( "**** %s clock wait starting\n", msg); ATF_REQUIRE_EQ(pthread_create(&child, NULL, run, &clck), 0); ATF_REQUIRE_EQ(pthread_join(child, NULL), 0); /* wait for terminate */ if (debug) printf( "**** %s clock wait ended\n", msg); } ATF_TC(cond_wait_real); ATF_TC_HEAD(cond_wait_real, tc) { atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait " "with CLOCK_REALTIME"); } ATF_TC_BODY(cond_wait_real, tc) { cond_wait(CLOCK_REALTIME, "CLOCK_REALTIME"); } ATF_TC(cond_wait_mono); ATF_TC_HEAD(cond_wait_mono, tc) { atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait " "with CLOCK_MONOTONIC"); } ATF_TC_BODY(cond_wait_mono, tc) { cond_wait(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, cond_wait_real); ATF_TP_ADD_TC(tp, cond_wait_mono); return 0; } Index: head/contrib/netbsd-tests/lib/libpthread/t_once.c =================================================================== --- head/contrib/netbsd-tests/lib/libpthread/t_once.c (revision 274576) +++ head/contrib/netbsd-tests/lib/libpthread/t_once.c (revision 274577) @@ -1,196 +1,200 @@ /* $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 $"); #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: head/contrib/netbsd-tests/lib/libpthread/t_sem.c =================================================================== --- head/contrib/netbsd-tests/lib/libpthread/t_sem.c (revision 274576) +++ head/contrib/netbsd-tests/lib/libpthread/t_sem.c (revision 274577) @@ -1,306 +1,310 @@ /* $NetBSD: t_sem.c,v 1.8 2014/11/04 00:20:19 justin 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.8 2014/11/04 00:20:19 justin Exp $"); #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(); }