Timeout.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #ifndef Timeout_H
  16. #define Timeout_H
  17. #include "util/events/EventBase.h"
  18. #include "util/Linker.h"
  19. Linker_require("util/events/libuv/Timeout.c")
  20. #include <stdint.h>
  21. /** An event which will happen after a given number of milliseconds. */
  22. struct Timeout;
  23. /**
  24. * Create a new timeout event.
  25. *
  26. * @param callback the function to call after this event expires.
  27. * @param callbackContext a pointer to be passed to the callback when it is called.
  28. * @param milliseconds the number of milliseconds after which to call the callback.
  29. * @param eventBase the libevent event base to use.
  30. * @param allocator the memory allocator to create the timeout with,
  31. * freeing this allocator will make the event be removed safely.
  32. */
  33. struct Timeout* Timeout__setTimeout(void (* const callback)(void* callbackContext),
  34. void* const callbackContext,
  35. const uint64_t milliseconds,
  36. struct EventBase* eventBase,
  37. struct Allocator* allocator,
  38. char* file,
  39. int line);
  40. #define Timeout_setTimeout(cb, cbc, ms, eb, alloc) \
  41. Timeout__setTimeout((cb), (cbc), (ms), (eb), (alloc), Gcc_SHORT_FILE, Gcc_LINE)
  42. /**
  43. * Create a new interval event.
  44. * This event will continue to be fired periodically until it is cleared or its allocator is freed.
  45. *
  46. * @param callback the function to call after this event expires.
  47. * @param callbackContext a pointer to be passed to the callback when it is called.
  48. * @param milliseconds the of milliseconds of the interval.
  49. * @param eventBase the libevent event base to use.
  50. * @param allocator the memory allocator to create the timeout with,
  51. * freeing this allocator will make the event be removed safely.
  52. */
  53. struct Timeout* Timeout__setInterval(void (* const callback)(void* callbackContext),
  54. void* const callbackContext,
  55. const uint64_t milliseconds,
  56. struct EventBase* eventBase,
  57. struct Allocator* allocator,
  58. char* file,
  59. int line);
  60. #define Timeout_setInterval(cb, cbc, ms, eb, alloc) \
  61. Timeout__setInterval((cb), (cbc), (ms), (eb), (alloc), Gcc_SHORT_FILE, Gcc_LINE)
  62. /**
  63. * Change an existing event to trigger after a new timeout.
  64. * This will keep the callback and context but will delete and recreate the underlying event.
  65. *
  66. * @param timeout the timeout or interval to change,
  67. * if an interval then the interval period will be changed.
  68. * @param milliseconds the new timeout time or interval period.
  69. */
  70. void Timeout_resetTimeout(struct Timeout* timeout,
  71. const uint64_t milliseconds);
  72. /**
  73. * Disable a timeout or interval.
  74. * The allocator still needs to be freed.
  75. *
  76. * @param timeout the timeout or interval event to stop.
  77. */
  78. void Timeout_clearTimeout(struct Timeout* timeout);
  79. void Timeout_clearAll(struct EventBase* eventBase);
  80. int Timeout_isActive(struct Timeout* timeout);
  81. #endif