Timeout.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #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. /**
  39. * Create a new interval event.
  40. * This event will continue to be fired periodicly until it is cleared or its allocator is freed.
  41. *
  42. * @param callback the function to call after this event expires.
  43. * @param callbackContext a pointer to be passed to the callback when it is called.
  44. * @param milliseconds the of milliseconds of the interval.
  45. * @param eventBase the libevent event base to use.
  46. * @param allocator the memory allocator to create the timeout with,
  47. * freeing this allocator will make the event be removed safely.
  48. */
  49. struct Timeout* Timeout_setInterval(void (* const callback)(void* callbackContext),
  50. void* const callbackContext,
  51. const uint64_t milliseconds,
  52. struct EventBase* eventBase,
  53. struct Allocator* allocator);
  54. /**
  55. * Change an existing event to trigger after a new timeout.
  56. * This will keep the callback and context but will delete and recreate the underlying event.
  57. *
  58. * @param timeout the timeout or interval to change,
  59. * if an interval then the interval period will be changed.
  60. * @param milliseconds the new timeout time or interval period.
  61. */
  62. void Timeout_resetTimeout(struct Timeout* timeout,
  63. const uint64_t milliseconds);
  64. /**
  65. * Disable a timeout or interval.
  66. * The allocator still needs to be freed.
  67. *
  68. * @param timeout the timeout or interval event to stop.
  69. */
  70. void Timeout_clearTimeout(struct Timeout* timeout);
  71. #endif