Timeout.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. 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 periodicly 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. #endif