Timeout.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. if (!timeout->isInterval) {
  37. Timeout_clearTimeout(timeout);
  38. }
  39. timeout->callback(timeout->callbackContext);
  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. char* file,
  72. int line)
  73. {
  74. struct EventBase_pvt* base = EventBase_privatize(eventBase);
  75. struct Allocator* alloc = Allocator__child(allocator, file, line);
  76. struct Timeout* timeout = Allocator_calloc(alloc, sizeof(struct Timeout), 1);
  77. timeout->callback = callback;
  78. timeout->callbackContext = callbackContext;
  79. timeout->milliseconds = milliseconds;
  80. timeout->alloc = alloc;
  81. timeout->isInterval = interval;
  82. Identity_set(timeout);
  83. uv_timer_init(base->loop, &timeout->timer);
  84. uv_timer_start(&timeout->timer, handleEvent, milliseconds, (interval) ? milliseconds : 0);
  85. timeout->timer.data = timeout;
  86. Allocator_onFree(alloc, onFree, timeout);
  87. return timeout;
  88. }
  89. /** See: Timeout.h */
  90. struct Timeout* Timeout__setTimeout(void (* const callback)(void* callbackContext),
  91. void* const callbackContext,
  92. const uint64_t milliseconds,
  93. struct EventBase* eventBase,
  94. struct Allocator* allocator,
  95. char* file,
  96. int line)
  97. {
  98. return setTimeout(callback, callbackContext, milliseconds, 0, eventBase, allocator, file, line);
  99. }
  100. /** See: Timeout.h */
  101. struct Timeout* Timeout__setInterval(void (* const callback)(void* callbackContext),
  102. void* const callbackContext,
  103. const uint64_t milliseconds,
  104. struct EventBase* eventBase,
  105. struct Allocator* allocator,
  106. char* file,
  107. int line)
  108. {
  109. return setTimeout(callback, callbackContext, milliseconds, 1, eventBase, allocator, file, line);
  110. }
  111. /** See: Timeout.h */
  112. void Timeout_resetTimeout(struct Timeout* timeout,
  113. const uint64_t milliseconds)
  114. {
  115. Timeout_clearTimeout(timeout);
  116. uv_timer_start(&timeout->timer, handleEvent, milliseconds, 0);
  117. }
  118. /** See: Timeout.h */
  119. void Timeout_clearTimeout(struct Timeout* timeout)
  120. {
  121. if (!uv_is_closing((uv_handle_t*) &timeout->timer)) {
  122. uv_timer_stop(&timeout->timer);
  123. }
  124. }