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.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*-
  2. * Copyright 2019 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. #include "libev_helper.h"
  17. static void
  18. rspamd_ev_watcher_io_cb (EV_P_ struct ev_io *w, int revents)
  19. {
  20. struct rspamd_io_ev *ev = (struct rspamd_io_ev *)w->data;
  21. ev->last_activity = ev_now (EV_A);
  22. ev->cb (ev->io.fd, revents, ev->ud);
  23. }
  24. static void
  25. rspamd_ev_watcher_timer_cb (EV_P_ struct ev_timer *w, int revents)
  26. {
  27. struct rspamd_io_ev *ev = (struct rspamd_io_ev *)w->data;
  28. ev_tstamp after = ev->last_activity - ev_now (EV_A) + ev->timeout;
  29. if (after < 0.) {
  30. /* Real timeout */
  31. ev->cb (ev->io.fd, EV_TIMER, ev->ud);
  32. }
  33. else {
  34. /* Start another cycle as there was some activity */
  35. w->repeat = after;
  36. ev_timer_again (EV_A_ w);
  37. }
  38. }
  39. void
  40. rspamd_ev_watcher_init (struct rspamd_io_ev *ev,
  41. int fd,
  42. short what,
  43. rspamd_ev_cb cb,
  44. void *ud)
  45. {
  46. ev_io_init (&ev->io, rspamd_ev_watcher_io_cb, fd, what);
  47. ev->io.data = ev;
  48. ev_init (&ev->tm, rspamd_ev_watcher_timer_cb);
  49. ev->tm.data = ev;
  50. ev->ud = ud;
  51. ev->cb = cb;
  52. }
  53. void
  54. rspamd_ev_watcher_start (struct ev_loop *loop,
  55. struct rspamd_io_ev *ev,
  56. ev_tstamp timeout)
  57. {
  58. g_assert (ev->cb != NULL);
  59. ev->last_activity = ev_now (EV_A);
  60. ev_io_start (EV_A_ &ev->io);
  61. if (timeout > 0) {
  62. /* Update timestamp to avoid timers running early */
  63. ev_now_update (loop);
  64. ev->timeout = timeout;
  65. ev_timer_set (&ev->tm, timeout, 0.0);
  66. ev_timer_start (EV_A_ &ev->tm);
  67. }
  68. }
  69. void
  70. rspamd_ev_watcher_stop (struct ev_loop *loop,
  71. struct rspamd_io_ev *ev)
  72. {
  73. if (ev_can_stop (&ev->io)) {
  74. ev_io_stop (EV_A_ &ev->io);
  75. }
  76. if (ev->timeout > 0) {
  77. ev_timer_stop (EV_A_ &ev->tm);
  78. }
  79. }
  80. void
  81. rspamd_ev_watcher_reschedule (struct ev_loop *loop,
  82. struct rspamd_io_ev *ev,
  83. short what)
  84. {
  85. g_assert (ev->cb != NULL);
  86. if (ev_can_stop (&ev->io)) {
  87. ev_io_stop (EV_A_ &ev->io);
  88. ev_io_set (&ev->io, ev->io.fd, what);
  89. ev_io_start (EV_A_ &ev->io);
  90. }
  91. else {
  92. ev->io.data = ev;
  93. ev_io_init (&ev->io, rspamd_ev_watcher_io_cb, ev->io.fd, what);
  94. ev_io_start (EV_A_ &ev->io);
  95. }
  96. if (ev->timeout > 0) {
  97. if (!(ev_can_stop (&ev->tm))) {
  98. /* Update timestamp to avoid timers running early */
  99. ev_now_update (loop);
  100. ev->tm.data = ev;
  101. ev_timer_init (&ev->tm, rspamd_ev_watcher_timer_cb, ev->timeout, 0.0);
  102. ev_timer_start (EV_A_ &ev->tm);
  103. }
  104. }
  105. ev->last_activity = ev_now (EV_A);
  106. }