1 1.10 kre /* $NetBSD: t_condwait.c,v 1.10 2022/12/11 10:02:53 kre Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 27 1.1 christos */ 28 1.1 christos #include <sys/cdefs.h> 29 1.10 kre __RCSID("$NetBSD: t_condwait.c,v 1.10 2022/12/11 10:02:53 kre Exp $"); 30 1.1 christos 31 1.5 christos #include <sys/time.h> 32 1.1 christos #include <errno.h> 33 1.1 christos #include <pthread.h> 34 1.1 christos #include <stdio.h> 35 1.1 christos #include <stdlib.h> 36 1.1 christos #include <string.h> 37 1.1 christos #include <time.h> 38 1.1 christos #include <unistd.h> 39 1.1 christos 40 1.1 christos #include <atf-c.h> 41 1.1 christos 42 1.4 christos #include "isqemu.h" 43 1.4 christos 44 1.5 christos #include "h_common.h" 45 1.5 christos 46 1.9 andvar #define WAITTIME 2 /* Timeout wait in seconds */ 47 1.1 christos 48 1.1 christos static const int debug = 1; 49 1.1 christos 50 1.1 christos static void * 51 1.1 christos run(void *param) 52 1.1 christos { 53 1.10 kre struct timespec ts, to, te; 54 1.1 christos clockid_t clck; 55 1.1 christos pthread_condattr_t attr; 56 1.1 christos pthread_cond_t cond; 57 1.1 christos pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; 58 1.1 christos int ret = 0; 59 1.1 christos 60 1.1 christos 61 1.1 christos clck = *(clockid_t *)param; 62 1.5 christos PTHREAD_REQUIRE(pthread_condattr_init(&attr)); 63 1.5 christos PTHREAD_REQUIRE(pthread_condattr_setclock(&attr, clck)); 64 1.1 christos pthread_cond_init(&cond, &attr); 65 1.1 christos 66 1.1 christos ATF_REQUIRE_EQ((ret = pthread_mutex_lock(&m)), 0); 67 1.1 christos 68 1.1 christos ATF_REQUIRE_EQ(clock_gettime(clck, &ts), 0); 69 1.1 christos to = ts; 70 1.1 christos 71 1.1 christos if (debug) 72 1.2 christos printf("started: %lld.%09ld sec\n", (long long)to.tv_sec, 73 1.2 christos to.tv_nsec); 74 1.1 christos 75 1.1 christos ts.tv_sec += WAITTIME; /* Timeout wait */ 76 1.1 christos 77 1.1 christos switch (ret = pthread_cond_timedwait(&cond, &m, &ts)) { 78 1.1 christos case ETIMEDOUT: 79 1.1 christos /* Timeout */ 80 1.1 christos ATF_REQUIRE_EQ(clock_gettime(clck, &te), 0); 81 1.1 christos if (debug) { 82 1.2 christos printf("timeout: %lld.%09ld sec\n", 83 1.2 christos (long long)te.tv_sec, te.tv_nsec); 84 1.10 kre timespecsub(&te, &to, &to); 85 1.2 christos printf("elapsed: %lld.%09ld sec\n", 86 1.2 christos (long long)to.tv_sec, to.tv_nsec); 87 1.1 christos } 88 1.10 kre if (clck == CLOCK_MONOTONIC) 89 1.10 kre ATF_REQUIRE(timespeccmp(&te, &ts, >=)); 90 1.10 kre break; 91 1.10 kre case 0: 92 1.10 kre atf_tc_fail("pthread_cond_timedwait returned before timeout"); 93 1.1 christos break; 94 1.1 christos default: 95 1.1 christos ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret)); 96 1.1 christos } 97 1.1 christos 98 1.1 christos ATF_REQUIRE_MSG(!(ret = pthread_mutex_unlock(&m)), 99 1.1 christos "pthread_mutex_unlock: %s", strerror(ret)); 100 1.1 christos pthread_exit(&ret); 101 1.1 christos } 102 1.1 christos 103 1.1 christos static void 104 1.1 christos cond_wait(clockid_t clck, const char *msg) { 105 1.1 christos pthread_t child; 106 1.1 christos 107 1.1 christos if (debug) 108 1.1 christos printf( "**** %s clock wait starting\n", msg); 109 1.1 christos ATF_REQUIRE_EQ(pthread_create(&child, NULL, run, &clck), 0); 110 1.1 christos ATF_REQUIRE_EQ(pthread_join(child, NULL), 0); /* wait for terminate */ 111 1.1 christos if (debug) 112 1.1 christos printf( "**** %s clock wait ended\n", msg); 113 1.1 christos } 114 1.1 christos 115 1.1 christos ATF_TC(cond_wait_real); 116 1.1 christos ATF_TC_HEAD(cond_wait_real, tc) 117 1.1 christos { 118 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait " 119 1.1 christos "with CLOCK_REALTIME"); 120 1.1 christos } 121 1.1 christos 122 1.1 christos ATF_TC_BODY(cond_wait_real, tc) { 123 1.1 christos cond_wait(CLOCK_REALTIME, "CLOCK_REALTIME"); 124 1.1 christos } 125 1.1 christos 126 1.1 christos ATF_TC(cond_wait_mono); 127 1.1 christos ATF_TC_HEAD(cond_wait_mono, tc) 128 1.1 christos { 129 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait " 130 1.1 christos "with CLOCK_MONOTONIC"); 131 1.1 christos } 132 1.1 christos 133 1.1 christos ATF_TC_BODY(cond_wait_mono, tc) { 134 1.1 christos cond_wait(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); 135 1.1 christos } 136 1.1 christos 137 1.1 christos ATF_TP_ADD_TCS(tp) 138 1.1 christos { 139 1.1 christos ATF_TP_ADD_TC(tp, cond_wait_real); 140 1.1 christos ATF_TP_ADD_TC(tp, cond_wait_mono); 141 1.6 maya return atf_no_error(); 142 1.1 christos } 143