You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

libev_helper.h 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2024 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef RSPAMD_LIBEV_HELPER_H
  17. #define RSPAMD_LIBEV_HELPER_H
  18. #include "config.h"
  19. #include "contrib/libev/ev.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /*
  24. * This module is a little helper to simplify libevent->libev transition
  25. * It allows to create timed IO watchers utilising both
  26. */
  27. typedef void (*rspamd_ev_cb)(int fd, short what, void *ud);
  28. struct rspamd_io_ev {
  29. ev_io io;
  30. ev_timer tm;
  31. rspamd_ev_cb cb;
  32. void *ud;
  33. ev_tstamp timeout;
  34. };
  35. /**
  36. * Initialize watcher similar to event_init
  37. * @param ev
  38. * @param fd
  39. * @param what
  40. * @param cb
  41. * @param ud
  42. */
  43. void rspamd_ev_watcher_init(struct rspamd_io_ev *ev,
  44. int fd, short what, rspamd_ev_cb cb, void *ud);
  45. /**
  46. * Start watcher with the specific timeout
  47. * @param loop
  48. * @param ev
  49. * @param timeout
  50. */
  51. void rspamd_ev_watcher_start(struct ev_loop *loop,
  52. struct rspamd_io_ev *ev,
  53. ev_tstamp timeout);
  54. /**
  55. * Stops watcher and clean it up
  56. * @param loop
  57. * @param ev
  58. */
  59. void rspamd_ev_watcher_stop(struct ev_loop *loop,
  60. struct rspamd_io_ev *ev);
  61. /**
  62. * Convenience function to reschedule watcher with different events
  63. * @param loop
  64. * @param ev
  65. * @param what
  66. */
  67. void rspamd_ev_watcher_reschedule(struct ev_loop *loop,
  68. struct rspamd_io_ev *ev,
  69. short what);
  70. /**
  71. * Convenience function to reschedule watcher with different events and different timeout
  72. * @param loop
  73. * @param ev
  74. * @param what
  75. */
  76. void rspamd_ev_watcher_reschedule_at(struct ev_loop *loop,
  77. struct rspamd_io_ev *ev,
  78. short what,
  79. ev_tstamp at);
  80. #ifdef __cplusplus
  81. }
  82. #endif
  83. #endif