Timeout.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <https://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. uint16_t isInterval;
  27. uint16_t isArmed;
  28. struct Allocator* alloc;
  29. struct Timeout* next;
  30. struct Timeout** selfPtr;
  31. struct EventBase_pvt* base;
  32. Identity
  33. };
  34. static void linkTo(struct Timeout* timeout)
  35. {
  36. timeout->next = (struct Timeout*) timeout->base->timeouts;
  37. if (timeout->next) {
  38. timeout->next->selfPtr = &timeout->next;
  39. }
  40. timeout->base->timeouts = timeout;
  41. timeout->selfPtr = (struct Timeout**) &timeout->base->timeouts;
  42. timeout->isArmed = 1;
  43. }
  44. static void unlinkTo(struct Timeout* timeout)
  45. {
  46. if (timeout->selfPtr) {
  47. *timeout->selfPtr = timeout->next;
  48. if (timeout->next) {
  49. Assert_true(&timeout->next == timeout->next->selfPtr);
  50. timeout->next->selfPtr = timeout->selfPtr;
  51. timeout->next = NULL;
  52. }
  53. timeout->selfPtr = NULL;
  54. }
  55. timeout->isArmed = 0;
  56. }
  57. /**
  58. * The callback to be called by libuv.
  59. */
  60. static void handleEvent(uv_timer_t* handle)
  61. {
  62. struct Timeout* timeout = Identity_check((struct Timeout*) handle);
  63. if (!timeout->isArmed) { return; }
  64. if (!timeout->isInterval) {
  65. Timeout_clearTimeout(timeout);
  66. }
  67. timeout->callback(timeout->callbackContext);
  68. }
  69. static void onFree2(uv_handle_t* timer)
  70. {
  71. Allocator_onFreeComplete(timer->data);
  72. }
  73. static int onFree(struct Allocator_OnFreeJob* job)
  74. {
  75. struct Timeout* t = Identity_check((struct Timeout*) job->userData);
  76. unlinkTo(t);
  77. t->timer.data = job;
  78. uv_close((uv_handle_t*) &t->timer, onFree2);
  79. return Allocator_ONFREE_ASYNC;
  80. }
  81. /**
  82. * Create a timeout event.
  83. * The timeout event will be triggered after the given number of milliseconds.
  84. *
  85. * @param callback the functiont to call.
  86. * @param callbackContext a pointer to pass to the called function.
  87. * @param milliseconds the number of milliseconds to wait before the event happens.
  88. * @param interval if non-zero, this event will repeat instead of triggering once.
  89. * @param eventBase the libevent event base.
  90. * @param allocator the memory allocator to use for allocating the event.
  91. * if this is freed, the event will be safely deleted.
  92. * @return a timeout struct which can be used to clear the timeout.
  93. */
  94. static struct Timeout* setTimeout(void (* const callback)(void* callbackContext),
  95. void* const callbackContext,
  96. const uint64_t milliseconds,
  97. const uint32_t interval,
  98. struct EventBase* eventBase,
  99. struct Allocator* allocator,
  100. char* file,
  101. int line)
  102. {
  103. struct EventBase_pvt* base = EventBase_privatize(eventBase);
  104. struct Allocator* alloc = Allocator__child(allocator, file, line);
  105. struct Timeout* timeout = Allocator_calloc(alloc, sizeof(struct Timeout), 1);
  106. timeout->callback = callback;
  107. timeout->callbackContext = callbackContext;
  108. timeout->milliseconds = milliseconds;
  109. timeout->alloc = alloc;
  110. timeout->isInterval = interval;
  111. timeout->base = base;
  112. Identity_set(timeout);
  113. uv_timer_init(base->loop, &timeout->timer);
  114. uv_timer_start(&timeout->timer, handleEvent, milliseconds, (interval) ? milliseconds : 0);
  115. timeout->timer.data = timeout;
  116. Allocator_onFree(alloc, onFree, timeout);
  117. linkTo(timeout);
  118. return timeout;
  119. }
  120. /** See: Timeout.h */
  121. struct Timeout* Timeout__setTimeout(void (* const callback)(void* callbackContext),
  122. void* const callbackContext,
  123. const uint64_t milliseconds,
  124. struct EventBase* eventBase,
  125. struct Allocator* allocator,
  126. char* file,
  127. int line)
  128. {
  129. return setTimeout(callback, callbackContext, milliseconds, 0, eventBase, allocator, file, line);
  130. }
  131. /** See: Timeout.h */
  132. struct Timeout* Timeout__setInterval(void (* const callback)(void* callbackContext),
  133. void* const callbackContext,
  134. const uint64_t milliseconds,
  135. struct EventBase* eventBase,
  136. struct Allocator* allocator,
  137. char* file,
  138. int line)
  139. {
  140. return setTimeout(callback, callbackContext, milliseconds, 1, eventBase, allocator, file, line);
  141. }
  142. /** See: Timeout.h */
  143. void Timeout_resetTimeout(struct Timeout* timeout,
  144. const uint64_t milliseconds)
  145. {
  146. Timeout_clearTimeout(timeout);
  147. linkTo(timeout);
  148. uv_timer_start(&timeout->timer, handleEvent, milliseconds, 0);
  149. }
  150. /** See: Timeout.h */
  151. void Timeout_clearTimeout(struct Timeout* timeout)
  152. {
  153. unlinkTo(timeout);
  154. if (!uv_is_closing((uv_handle_t*) &timeout->timer)) {
  155. uv_timer_stop(&timeout->timer);
  156. }
  157. }
  158. void Timeout_clearAll(struct EventBase* eventBase)
  159. {
  160. struct EventBase_pvt* base = EventBase_privatize(eventBase);
  161. struct Timeout* to = base->timeouts;
  162. if (!to) { return; }
  163. while (to) {
  164. struct Timeout* next = to->next;
  165. Timeout_clearTimeout(to);
  166. to = next;
  167. }
  168. Assert_true(!base->timeouts);
  169. }
  170. int Timeout_isActive(struct Timeout* timeout)
  171. {
  172. return (timeout && timeout->selfPtr);
  173. }