1 /* $NetBSD: t_condwait.c,v 1.4 2013/04/12 17:18:11 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2013 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 #include <sys/cdefs.h> 29 __RCSID("$NetBSD: t_condwait.c,v 1.4 2013/04/12 17:18:11 christos Exp $"); 30 31 #include <errno.h> 32 #include <pthread.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <time.h> 37 #include <unistd.h> 38 39 #include <atf-c.h> 40 41 #include "isqemu.h" 42 43 #ifdef __FreeBSD__ 44 #include <sys/time.h> 45 #endif 46 47 #define WAITTIME 2 /* Timeout wait secound */ 48 49 static const int debug = 1; 50 51 static void * 52 run(void *param) 53 { 54 struct timespec ts, to, te; 55 clockid_t clck; 56 pthread_condattr_t attr; 57 pthread_cond_t cond; 58 pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; 59 int ret = 0; 60 61 62 clck = *(clockid_t *)param; 63 pthread_condattr_init(&attr); 64 pthread_condattr_setclock(&attr, clck); /* MONOTONIC or MONOTONIC */ 65 pthread_cond_init(&cond, &attr); 66 67 ATF_REQUIRE_EQ((ret = pthread_mutex_lock(&m)), 0); 68 69 ATF_REQUIRE_EQ(clock_gettime(clck, &ts), 0); 70 to = ts; 71 72 if (debug) 73 printf("started: %lld.%09ld sec\n", (long long)to.tv_sec, 74 to.tv_nsec); 75 76 ts.tv_sec += WAITTIME; /* Timeout wait */ 77 78 switch (ret = pthread_cond_timedwait(&cond, &m, &ts)) { 79 case ETIMEDOUT: 80 /* Timeout */ 81 ATF_REQUIRE_EQ(clock_gettime(clck, &te), 0); 82 timespecsub(&te, &to, &to); 83 if (debug) { 84 printf("timeout: %lld.%09ld sec\n", 85 (long long)te.tv_sec, te.tv_nsec); 86 printf("elapsed: %lld.%09ld sec\n", 87 (long long)to.tv_sec, to.tv_nsec); 88 } 89 if (isQEMU()) { 90 double to_seconds = to.tv_sec + 1e-9 * to.tv_nsec; 91 ATF_REQUIRE(to_seconds >= WAITTIME * 0.9); 92 /* Loose upper limit because of qemu timing bugs */ 93 ATF_REQUIRE(to_seconds < WAITTIME * 2.5); 94 } else { 95 ATF_REQUIRE_EQ(to.tv_sec, WAITTIME); 96 } 97 break; 98 default: 99 ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret)); 100 } 101 102 ATF_REQUIRE_MSG(!(ret = pthread_mutex_unlock(&m)), 103 "pthread_mutex_unlock: %s", strerror(ret)); 104 pthread_exit(&ret); 105 } 106 107 static void 108 cond_wait(clockid_t clck, const char *msg) { 109 pthread_t child; 110 111 if (debug) 112 printf( "**** %s clock wait starting\n", msg); 113 ATF_REQUIRE_EQ(pthread_create(&child, NULL, run, &clck), 0); 114 ATF_REQUIRE_EQ(pthread_join(child, NULL), 0); /* wait for terminate */ 115 if (debug) 116 printf( "**** %s clock wait ended\n", msg); 117 } 118 119 ATF_TC(cond_wait_real); 120 ATF_TC_HEAD(cond_wait_real, tc) 121 { 122 atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait " 123 "with CLOCK_REALTIME"); 124 } 125 126 ATF_TC_BODY(cond_wait_real, tc) { 127 cond_wait(CLOCK_REALTIME, "CLOCK_REALTIME"); 128 } 129 130 ATF_TC(cond_wait_mono); 131 ATF_TC_HEAD(cond_wait_mono, tc) 132 { 133 atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait " 134 "with CLOCK_MONOTONIC"); 135 } 136 137 ATF_TC_BODY(cond_wait_mono, tc) { 138 cond_wait(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); 139 } 140 141 ATF_TP_ADD_TCS(tp) 142 { 143 ATF_TP_ADD_TC(tp, cond_wait_real); 144 ATF_TP_ADD_TC(tp, cond_wait_mono); 145 return 0; 146 } 147