condition.c revision 1.2
1/* $NetBSD: condition.c,v 1.2 2024/02/21 22:52:28 christos Exp $ */ 2 3/* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * SPDX-License-Identifier: MPL-2.0 7 * 8 * This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 * 12 * See the COPYRIGHT file distributed with this work for additional 13 * information regarding copyright ownership. 14 */ 15 16/*! \file */ 17 18#include <errno.h> 19 20#include <isc/condition.h> 21#include <isc/string.h> 22#include <isc/time.h> 23#include <isc/util.h> 24 25isc_result_t 26isc_condition_waituntil(isc_condition_t *c, isc_mutex_t *m, isc_time_t *t) { 27 int presult; 28 isc_result_t result; 29 struct timespec ts; 30 31 REQUIRE(c != NULL && m != NULL && t != NULL); 32 33 /* 34 * POSIX defines a timespec's tv_sec as time_t. 35 */ 36 result = isc_time_secondsastimet(t, &ts.tv_sec); 37 38 /* 39 * If we have a range error ts.tv_sec is most probably a signed 40 * 32 bit value. Set ts.tv_sec to INT_MAX. This is a kludge. 41 */ 42 if (result == ISC_R_RANGE) { 43 ts.tv_sec = INT_MAX; 44 } else if (result != ISC_R_SUCCESS) { 45 return (result); 46 } 47 48 /*! 49 * POSIX defines a timespec's tv_nsec as long. isc_time_nanoseconds 50 * ensures its return value is < 1 billion, which will fit in a long. 51 */ 52 ts.tv_nsec = (long)isc_time_nanoseconds(t); 53 54 do { 55 presult = pthread_cond_timedwait(c, m, &ts); 56 if (presult == 0) { 57 return (ISC_R_SUCCESS); 58 } 59 if (presult == ETIMEDOUT) { 60 return (ISC_R_TIMEDOUT); 61 } 62 } while (presult == EINTR); 63 64 UNEXPECTED_SYSERROR(presult, "pthread_cond_timedwait()"); 65 return (ISC_R_UNEXPECTED); 66} 67