Timeout.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "util/events/libuv/UvWrapper.h"
  16. #include "memory/Allocator.h"
  17. #include "util/events/libuv/EventBase_pvt.h"
  18. #include "util/events/Timeout.h"
  19. #include "util/Identity.h"
  20. struct Timeout
  21. {
  22. uv_timer_t timer;
  23. void (* callback)(void* callbackContext);
  24. void* callbackContext;
  25. uint64_t milliseconds;
  26. int isInterval;
  27. struct Allocator* alloc;
  28. Identity
  29. };
  30. /**
  31. * The callback to be called by libuv.
  32. */
  33. static void handleEvent(uv_timer_t* handle, int status)
  34. {
  35. struct Timeout* timeout = Identity_check((struct Timeout*) handle);
  36. timeout->callback(timeout->callbackContext);
  37. if (!timeout->isInterval) {
  38. Allocator_free(timeout->alloc);
  39. }
  40. }
  41. static void onFree2(uv_handle_t* timer)
  42. {
  43. Allocator_onFreeComplete(timer->data);
  44. }
  45. static int onFree(struct Allocator_OnFreeJob* job)
  46. {
  47. struct Timeout* t = Identity_check((struct Timeout*) job->userData);
  48. t->timer.data = job;
  49. uv_close((uv_handle_t*) &t->timer, onFree2);
  50. return Allocator_ONFREE_ASYNC;
  51. }
  52. /**
  53. * Create a timeout event.
  54. * The timeout event will be triggered after the given number of milliseconds.
  55. *
  56. * @param callback the functiont to call.
  57. * @param callbackContext a pointer to pass to the called function.
  58. * @param milliseconds the number of milliseconds to wait before the event happens.
  59. * @param interval if non-zero, this event will repeat instead of triggering once.
  60. * @param eventBase the libevent event base.
  61. * @param allocator the memory allocator to use for allocating the event.
  62. * if this is freed, the event will be safely deleted.
  63. * @return a timeout struct which can be used to clear the timeout.
  64. */
  65. static struct Timeout* setTimeout(void (* const callback)(void* callbackContext),
  66. void* const callbackContext,
  67. const uint64_t milliseconds,
  68. const uint32_t interval,
  69. struct EventBase* eventBase,
  70. struct Allocator* allocator)
  71. {
  72. struct EventBase_pvt* base = EventBase_privatize(eventBase);
  73. struct Allocator* alloc = Allocator_child(allocator);
  74. struct Timeout* timeout = Allocator_calloc(alloc, sizeof(struct Timeout), 1);
  75. timeout->callback = callback;
  76. timeout->callbackContext = callbackContext;
  77. timeout->milliseconds = milliseconds;
  78. timeout->alloc = alloc;
  79. timeout->isInterval = interval;
  80. Identity_set(timeout);
  81. uv_timer_init(base->loop, &timeout->timer);
  82. uv_timer_start(&timeout->timer, handleEvent, milliseconds, (interval) ? milliseconds : 0);
  83. timeout->timer.data = timeout;
  84. Allocator_onFree(alloc, onFree, timeout);
  85. return timeout;
  86. }
  87. /** See: Timeout.h */
  88. struct Timeout* Timeout_setTimeout(void (* const callback)(void* callbackContext),
  89. void* const callbackContext,
  90. const uint64_t milliseconds,
  91. struct EventBase* eventBase,
  92. struct Allocator* allocator)
  93. {
  94. return setTimeout(callback, callbackContext, milliseconds, 0, eventBase, allocator);
  95. }
  96. /** See: Timeout.h */
  97. struct Timeout* Timeout_setInterval(void (* const callback)(void* callbackContext),
  98. void* const callbackContext,
  99. const uint64_t milliseconds,
  100. struct EventBase* eventBase,
  101. struct Allocator* allocator)
  102. {
  103. return setTimeout(callback, callbackContext, milliseconds, 1, eventBase, allocator);
  104. }
  105. /** See: Timeout.h */
  106. void Timeout_resetTimeout(struct Timeout* timeout,
  107. const uint64_t milliseconds)
  108. {
  109. Timeout_clearTimeout(timeout);
  110. uv_timer_start(&timeout->timer, handleEvent, milliseconds, 0);
  111. }
  112. /** See: Timeout.h */
  113. void Timeout_clearTimeout(struct Timeout* timeout)
  114. {
  115. if (!uv_is_closing((uv_handle_t*) &timeout->timer)) {
  116. uv_timer_stop(&timeout->timer);
  117. }
  118. }