1
0

Timeout.c 3.9 KB

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