/contrib/bind9/lib/isc/timer_api.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 144 lines · 92 code · 36 blank · 16 comment · 18 complexity · a430b90614df272b756c6cd2d97c9fca MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* $Id: timer_api.c,v 1.4 2009/09/02 23:48:02 tbox Exp $ */
  17. #include <config.h>
  18. #include <unistd.h>
  19. #include <isc/app.h>
  20. #include <isc/magic.h>
  21. #include <isc/mutex.h>
  22. #include <isc/once.h>
  23. #include <isc/timer.h>
  24. #include <isc/util.h>
  25. static isc_mutex_t createlock;
  26. static isc_once_t once = ISC_ONCE_INIT;
  27. static isc_timermgrcreatefunc_t timermgr_createfunc = NULL;
  28. static void
  29. initialize(void) {
  30. RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
  31. }
  32. isc_result_t
  33. isc_timer_register(isc_timermgrcreatefunc_t createfunc) {
  34. isc_result_t result = ISC_R_SUCCESS;
  35. RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
  36. LOCK(&createlock);
  37. if (timermgr_createfunc == NULL)
  38. timermgr_createfunc = createfunc;
  39. else
  40. result = ISC_R_EXISTS;
  41. UNLOCK(&createlock);
  42. return (result);
  43. }
  44. isc_result_t
  45. isc_timermgr_createinctx(isc_mem_t *mctx, isc_appctx_t *actx,
  46. isc_timermgr_t **managerp)
  47. {
  48. isc_result_t result;
  49. LOCK(&createlock);
  50. REQUIRE(timermgr_createfunc != NULL);
  51. result = (*timermgr_createfunc)(mctx, managerp);
  52. UNLOCK(&createlock);
  53. if (result == ISC_R_SUCCESS)
  54. isc_appctx_settimermgr(actx, *managerp);
  55. return (result);
  56. }
  57. isc_result_t
  58. isc_timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp) {
  59. isc_result_t result;
  60. LOCK(&createlock);
  61. REQUIRE(timermgr_createfunc != NULL);
  62. result = (*timermgr_createfunc)(mctx, managerp);
  63. UNLOCK(&createlock);
  64. return (result);
  65. }
  66. void
  67. isc_timermgr_destroy(isc_timermgr_t **managerp) {
  68. REQUIRE(*managerp != NULL && ISCAPI_TIMERMGR_VALID(*managerp));
  69. (*managerp)->methods->destroy(managerp);
  70. ENSURE(*managerp == NULL);
  71. }
  72. isc_result_t
  73. isc_timer_create(isc_timermgr_t *manager, isc_timertype_t type,
  74. isc_time_t *expires, isc_interval_t *interval,
  75. isc_task_t *task, isc_taskaction_t action, const void *arg,
  76. isc_timer_t **timerp)
  77. {
  78. REQUIRE(ISCAPI_TIMERMGR_VALID(manager));
  79. return (manager->methods->timercreate(manager, type, expires,
  80. interval, task, action, arg,
  81. timerp));
  82. }
  83. void
  84. isc_timer_attach(isc_timer_t *timer, isc_timer_t **timerp) {
  85. REQUIRE(ISCAPI_TIMER_VALID(timer));
  86. REQUIRE(timerp != NULL && *timerp == NULL);
  87. timer->methods->attach(timer, timerp);
  88. ENSURE(*timerp == timer);
  89. }
  90. void
  91. isc_timer_detach(isc_timer_t **timerp) {
  92. REQUIRE(timerp != NULL && ISCAPI_TIMER_VALID(*timerp));
  93. (*timerp)->methods->detach(timerp);
  94. ENSURE(*timerp == NULL);
  95. }
  96. isc_result_t
  97. isc_timer_reset(isc_timer_t *timer, isc_timertype_t type,
  98. isc_time_t *expires, isc_interval_t *interval,
  99. isc_boolean_t purge)
  100. {
  101. REQUIRE(ISCAPI_TIMER_VALID(timer));
  102. return (timer->methods->reset(timer, type, expires, interval, purge));
  103. }
  104. isc_result_t
  105. isc_timer_touch(isc_timer_t *timer) {
  106. REQUIRE(ISCAPI_TIMER_VALID(timer));
  107. return (timer->methods->touch(timer));
  108. }