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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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->cb (ev->io.fd, revents, ev->ud);
  22. }
  23. static void
  24. rspamd_ev_watcher_timer_cb (EV_P_ struct ev_timer *w, int revents)
  25. {
  26. struct rspamd_io_ev *ev = (struct rspamd_io_ev *)w->data;
  27. /*
  28. * We now call timeout callback in all the cases, as we assume that all
  29. * timeouts are final
  30. */
  31. ev->cb (ev->io.fd, EV_TIMER, ev->ud);
  32. }
  33. void
  34. rspamd_ev_watcher_init (struct rspamd_io_ev *ev,
  35. int fd,
  36. short what,
  37. rspamd_ev_cb cb,
  38. void *ud)
  39. {
  40. ev_io_init (&ev->io, rspamd_ev_watcher_io_cb, fd, what);
  41. ev->io.data = ev;
  42. ev_init (&ev->tm, rspamd_ev_watcher_timer_cb);
  43. ev->tm.data = ev;
  44. ev->ud = ud;
  45. ev->cb = cb;
  46. }
  47. void
  48. rspamd_ev_watcher_start (struct ev_loop *loop,
  49. struct rspamd_io_ev *ev,
  50. ev_tstamp timeout)
  51. {
  52. g_assert (ev->cb != NULL);
  53. ev_io_start (EV_A_ &ev->io);
  54. if (timeout > 0) {
  55. /* Update timestamp to avoid timers running early */
  56. ev_now_update_if_cheap (loop);
  57. ev->timeout = timeout;
  58. ev_timer_set (&ev->tm, timeout, 0.0);
  59. ev_timer_start (EV_A_ &ev->tm);
  60. }
  61. }
  62. void
  63. rspamd_ev_watcher_stop (struct ev_loop *loop,
  64. struct rspamd_io_ev *ev)
  65. {
  66. if (ev_can_stop (&ev->io)) {
  67. ev_io_stop (EV_A_ &ev->io);
  68. }
  69. if (ev->timeout > 0) {
  70. ev_timer_stop (EV_A_ &ev->tm);
  71. }
  72. }
  73. void
  74. rspamd_ev_watcher_reschedule (struct ev_loop *loop,
  75. struct rspamd_io_ev *ev,
  76. short what)
  77. {
  78. g_assert (ev->cb != NULL);
  79. if (ev_can_stop (&ev->io)) {
  80. ev_io_stop (EV_A_ &ev->io);
  81. ev_io_set (&ev->io, ev->io.fd, what);
  82. ev_io_start (EV_A_ &ev->io);
  83. }
  84. else {
  85. ev->io.data = ev;
  86. ev_io_init (&ev->io, rspamd_ev_watcher_io_cb, ev->io.fd, what);
  87. ev_io_start (EV_A_ &ev->io);
  88. }
  89. if (ev->timeout > 0) {
  90. if (!(ev_can_stop (&ev->tm))) {
  91. /* Update timestamp to avoid timers running early */
  92. ev_now_update_if_cheap (loop);
  93. ev->tm.data = ev;
  94. ev_timer_init (&ev->tm, rspamd_ev_watcher_timer_cb, ev->timeout, 0.0);
  95. ev_timer_start (EV_A_ &ev->tm);
  96. }
  97. }
  98. }