/contrib/bind9/lib/isc/pthreads/condition.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 74 lines · 38 code · 11 blank · 25 comment · 13 complexity · 1b6cd046f8f4bef1902df949e580e3fd MD5 · raw file

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