condition.c revision 258945
115103Sphk/*
215103Sphk * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
315103Sphk * Copyright (C) 1998-2001  Internet Software Consortium.
415103Sphk *
515103Sphk * Permission to use, copy, modify, and/or distribute this software for any
615103Sphk * purpose with or without fee is hereby granted, provided that the above
715103Sphk * copyright notice and this permission notice appear in all copies.
815103Sphk *
915103Sphk * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
1015103Sphk * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1115103Sphk * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
1215103Sphk * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1315103Sphk * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
1415103Sphk * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1515103Sphk * PERFORMANCE OF THIS SOFTWARE.
1615103Sphk */
1715103Sphk
1815103Sphk/* $Id: condition.c,v 1.36 2007/06/19 23:47:18 tbox Exp $ */
1915103Sphk
2015103Sphk/*! \file */
2115103Sphk
2215103Sphk#include <config.h>
2315103Sphk
2415103Sphk#include <errno.h>
2515103Sphk
2615103Sphk#include <isc/condition.h>
2715103Sphk#include <isc/msgs.h>
2815103Sphk#include <isc/strerror.h>
2915103Sphk#include <isc/string.h>
3015103Sphk#include <isc/time.h>
3115103Sphk#include <isc/util.h>
3215103Sphk
3315103Sphkisc_result_t
3415103Sphkisc_condition_waituntil(isc_condition_t *c, isc_mutex_t *m, isc_time_t *t) {
3515103Sphk	int presult;
3615103Sphk	isc_result_t result;
3715103Sphk	struct timespec ts;
38116182Sobrien	char strbuf[ISC_STRERRORSIZE];
39116182Sobrien
40116182Sobrien	REQUIRE(c != NULL && m != NULL && t != NULL);
41175417Sjhb
4286190Srwatson	/*
43169604Swkoszek	 * POSIX defines a timespec's tv_sec as time_t.
4484611Srwatson	 */
4515103Sphk	result = isc_time_secondsastimet(t, &ts.tv_sec);
4615103Sphk	if (result != ISC_R_SUCCESS)
47169507Swkoszek		return (result);
4815103Sphk
4915103Sphk	/*!
5015103Sphk	 * POSIX defines a timespec's tv_nsec as long.  isc_time_nanoseconds
5187275Srwatson	 * ensures its return value is < 1 billion, which will fit in a long.
5287275Srwatson	 */
5346155Sphk	ts.tv_nsec = (long)isc_time_nanoseconds(t);
5476078Sjhb
55192895Sjamie	do {
56105046Smike#if ISC_MUTEX_PROFILE
5728918Skato		presult = pthread_cond_timedwait(c, &m->mutex, &ts);
5815103Sphk#else
5915103Sphk		presult = pthread_cond_timedwait(c, m, &ts);
6015103Sphk#endif
6115103Sphk		if (presult == 0)
6215103Sphk			return (ISC_R_SUCCESS);
6315103Sphk		if (presult == ETIMEDOUT)
6423382Sbde			return (ISC_R_TIMEDOUT);
6515103Sphk	} while (presult == EINTR);
6615103Sphk
6715103Sphk	isc__strerror(presult, strbuf, sizeof(strbuf));
6815103Sphk	UNEXPECTED_ERROR(__FILE__, __LINE__,
6915103Sphk			 "pthread_cond_timedwait() %s %s",
7048891Sphk			 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
7148891Sphk					ISC_MSG_RETURNED, "returned"),
7215103Sphk			 strbuf);
7315103Sphk	return (ISC_R_UNEXPECTED);
7415103Sphk}
7515103Sphk