condition.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2012  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1998-2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: condition.c,v 1.36 2007/06/19 23:47:18 tbox Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #include <errno.h>
00025 
00026 #include <isc/condition.h>
00027 #include <isc/msgs.h>
00028 #include <isc/strerror.h>
00029 #include <isc/string.h>
00030 #include <isc/time.h>
00031 #include <isc/util.h>
00032 
00033 isc_result_t
00034 isc_condition_waituntil(isc_condition_t *c, isc_mutex_t *m, isc_time_t *t) {
00035         int presult;
00036         isc_result_t result;
00037         struct timespec ts;
00038         char strbuf[ISC_STRERRORSIZE];
00039 
00040         REQUIRE(c != NULL && m != NULL && t != NULL);
00041 
00042         /*
00043          * POSIX defines a timespec's tv_sec as time_t.
00044          */
00045         result = isc_time_secondsastimet(t, &ts.tv_sec);
00046 
00047         /*
00048          * If we have a range error ts.tv_sec is most probably a signed
00049          * 32 bit value.  Set ts.tv_sec to INT_MAX.  This is a kludge.
00050          */
00051         if (result == ISC_R_RANGE)
00052                 ts.tv_sec = INT_MAX;
00053         else if (result != ISC_R_SUCCESS)
00054                 return (result);
00055 
00056         /*!
00057          * POSIX defines a timespec's tv_nsec as long.  isc_time_nanoseconds
00058          * ensures its return value is < 1 billion, which will fit in a long.
00059          */
00060         ts.tv_nsec = (long)isc_time_nanoseconds(t);
00061 
00062         do {
00063 #if ISC_MUTEX_PROFILE
00064                 presult = pthread_cond_timedwait(c, &m->mutex, &ts);
00065 #else
00066                 presult = pthread_cond_timedwait(c, m, &ts);
00067 #endif
00068                 if (presult == 0)
00069                         return (ISC_R_SUCCESS);
00070                 if (presult == ETIMEDOUT)
00071                         return (ISC_R_TIMEDOUT);
00072         } while (presult == EINTR);
00073 
00074         isc__strerror(presult, strbuf, sizeof(strbuf));
00075         UNEXPECTED_ERROR(__FILE__, __LINE__,
00076                          "pthread_cond_timedwait() %s %s",
00077                          isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
00078                                         ISC_MSG_RETURNED, "returned"),
00079                          strbuf);
00080         return (ISC_R_UNEXPECTED);
00081 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha