Timeout.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "rust/cjdns_sys/Rffi.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. Rffi_TimerTx* timer;
  23. struct Allocator* alloc;
  24. struct EventBase_pvt* base;
  25. Identity
  26. };
  27. /**
  28. * Create a timeout event.
  29. * The timeout event will be triggered after the given number of milliseconds.
  30. *
  31. * @param callback the functiont to call.
  32. * @param callbackContext a pointer to pass to the called function.
  33. * @param milliseconds the number of milliseconds to wait before the event happens.
  34. * @param interval if non-zero, this event will repeat instead of triggering once.
  35. * @param eventBase the libevent event base.
  36. * @param allocator the memory allocator to use for allocating the event.
  37. * if this is freed, the event will be safely deleted.
  38. * @return a timeout struct which can be used to clear the timeout.
  39. */
  40. static struct Timeout* setTimeout(void (* const callback)(void* callbackContext),
  41. void* const callbackContext,
  42. const uint64_t milliseconds,
  43. const uint32_t interval,
  44. struct EventBase* eventBase,
  45. struct Allocator* allocator,
  46. char* file,
  47. int line)
  48. {
  49. struct EventBase_pvt* base = EventBase_privatize(eventBase);
  50. struct Allocator* alloc = Allocator__child(allocator, file, line);
  51. struct Timeout* timeout = Allocator_calloc(alloc, sizeof(struct Timeout), 1);
  52. timeout->alloc = alloc;
  53. timeout->base = base;
  54. Identity_set(timeout);
  55. Rffi_setTimeout(&timeout->timer, callback, callbackContext,
  56. milliseconds, (interval) ? 1 : 0, base->rffi_loop, alloc);
  57. return timeout;
  58. }
  59. /** See: Timeout.h */
  60. struct Timeout* Timeout__setTimeout(void (* const callback)(void* callbackContext),
  61. void* const callbackContext,
  62. const uint64_t milliseconds,
  63. struct EventBase* eventBase,
  64. struct Allocator* allocator,
  65. char* file,
  66. int line)
  67. {
  68. return setTimeout(callback, callbackContext, milliseconds, 0, eventBase, allocator, file, line);
  69. }
  70. /** See: Timeout.h */
  71. struct Timeout* Timeout__setInterval(void (* const callback)(void* callbackContext),
  72. void* const callbackContext,
  73. const uint64_t milliseconds,
  74. struct EventBase* eventBase,
  75. struct Allocator* allocator,
  76. char* file,
  77. int line)
  78. {
  79. return setTimeout(callback, callbackContext, milliseconds, 1, eventBase, allocator, file, line);
  80. }
  81. /** See: Timeout.h */
  82. void Timeout_resetTimeout(struct Timeout* timeout,
  83. const uint64_t milliseconds)
  84. {
  85. Rffi_resetTimeout(timeout->timer, milliseconds);
  86. }
  87. /** See: Timeout.h */
  88. void Timeout_clearTimeout(struct Timeout* timeout)
  89. {
  90. Rffi_clearTimeout(timeout->timer);
  91. }
  92. void Timeout_clearAll(struct EventBase* eventBase)
  93. {
  94. struct EventBase_pvt* base = EventBase_privatize(eventBase);
  95. Rffi_clearAllTimeouts(base->rffi_loop);
  96. }
  97. int Timeout_isActive(struct Timeout* timeout)
  98. {
  99. return Rffi_isTimeoutActive(timeout->timer);
  100. }