xref: /freebsd/contrib/ntp/libntp/lib/isc/pthreads/condition.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert  * Copyright (C) 1998-2001  Internet Software Consortium.
4*a466cc55SCy Schubert  *
5*a466cc55SCy Schubert  * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert  * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert  * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert  *
9*a466cc55SCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert  * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert  */
17*a466cc55SCy Schubert 
18*a466cc55SCy Schubert /* $Id: condition.c,v 1.36 2007/06/19 23:47:18 tbox Exp $ */
19*a466cc55SCy Schubert 
20*a466cc55SCy Schubert /*! \file */
21*a466cc55SCy Schubert 
22*a466cc55SCy Schubert #include <config.h>
23*a466cc55SCy Schubert 
24*a466cc55SCy Schubert #include <errno.h>
25*a466cc55SCy Schubert 
26*a466cc55SCy Schubert #include <isc/condition.h>
27*a466cc55SCy Schubert #include <isc/msgs.h>
28*a466cc55SCy Schubert #include <isc/strerror.h>
29*a466cc55SCy Schubert #include <isc/string.h>
30*a466cc55SCy Schubert #include <isc/time.h>
31*a466cc55SCy Schubert #include <isc/util.h>
32*a466cc55SCy Schubert 
33*a466cc55SCy Schubert isc_result_t
isc_condition_waituntil(isc_condition_t * c,isc_mutex_t * m,isc_time_t * t)34*a466cc55SCy Schubert isc_condition_waituntil(isc_condition_t *c, isc_mutex_t *m, isc_time_t *t) {
35*a466cc55SCy Schubert 	int presult;
36*a466cc55SCy Schubert 	isc_result_t result;
37*a466cc55SCy Schubert 	struct timespec ts;
38*a466cc55SCy Schubert 	char strbuf[ISC_STRERRORSIZE];
39*a466cc55SCy Schubert 
40*a466cc55SCy Schubert 	REQUIRE(c != NULL && m != NULL && t != NULL);
41*a466cc55SCy Schubert 
42*a466cc55SCy Schubert 	/*
43*a466cc55SCy Schubert 	 * POSIX defines a timespec's tv_sec as time_t.
44*a466cc55SCy Schubert 	 */
45*a466cc55SCy Schubert 	result = isc_time_secondsastimet(t, &ts.tv_sec);
46*a466cc55SCy Schubert 	if (result != ISC_R_SUCCESS)
47*a466cc55SCy Schubert 		return (result);
48*a466cc55SCy Schubert 
49*a466cc55SCy Schubert 	/*!
50*a466cc55SCy Schubert 	 * POSIX defines a timespec's tv_nsec as long.  isc_time_nanoseconds
51*a466cc55SCy Schubert 	 * ensures its return value is < 1 billion, which will fit in a long.
52*a466cc55SCy Schubert 	 */
53*a466cc55SCy Schubert 	ts.tv_nsec = (long)isc_time_nanoseconds(t);
54*a466cc55SCy Schubert 
55*a466cc55SCy Schubert 	do {
56*a466cc55SCy Schubert #if ISC_MUTEX_PROFILE
57*a466cc55SCy Schubert 		presult = pthread_cond_timedwait(c, &m->mutex, &ts);
58*a466cc55SCy Schubert #else
59*a466cc55SCy Schubert 		presult = pthread_cond_timedwait(c, m, &ts);
60*a466cc55SCy Schubert #endif
61*a466cc55SCy Schubert 		if (presult == 0)
62*a466cc55SCy Schubert 			return (ISC_R_SUCCESS);
63*a466cc55SCy Schubert 		if (presult == ETIMEDOUT)
64*a466cc55SCy Schubert 			return (ISC_R_TIMEDOUT);
65*a466cc55SCy Schubert 	} while (presult == EINTR);
66*a466cc55SCy Schubert 
67*a466cc55SCy Schubert 	isc__strerror(presult, strbuf, sizeof(strbuf));
68*a466cc55SCy Schubert 	UNEXPECTED_ERROR(__FILE__, __LINE__,
69*a466cc55SCy Schubert 			 "pthread_cond_timedwait() %s %s",
70*a466cc55SCy Schubert 			 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
71*a466cc55SCy Schubert 					ISC_MSG_RETURNED, "returned"),
72*a466cc55SCy Schubert 			 strbuf);
73*a466cc55SCy Schubert 	return (ISC_R_UNEXPECTED);
74*a466cc55SCy Schubert }
75