Browse Source

[Minor] Add function to reschedule events at specific timer

pull/4939/head
Vsevolod Stakhov 1 week ago
parent
commit
917cdf87bf
No account linked to committer's email address
2 changed files with 44 additions and 3 deletions
  1. 30
    0
      src/libutil/libev_helper.c
  2. 14
    3
      src/libutil/libev_helper.h

+ 30
- 0
src/libutil/libev_helper.c View File

@@ -108,4 +108,34 @@ void rspamd_ev_watcher_reschedule(struct ev_loop *loop,
ev_timer_start(EV_A, &ev->tm);
}
}
}

void rspamd_ev_watcher_reschedule_at(struct ev_loop *loop,
struct rspamd_io_ev *ev,
short what,
ev_tstamp at)
{
g_assert(ev->cb != NULL);

if (ev_can_stop(&ev->io)) {
ev_io_stop(EV_A, &ev->io);
ev_io_set(&ev->io, ev->io.fd, what);
ev_io_start(EV_A, &ev->io);
}
else {
ev->io.data = ev;
ev_io_init(&ev->io, rspamd_ev_watcher_io_cb, ev->io.fd, what);
ev_io_start(EV_A, &ev->io);
}

if (at > 0) {
if (!(ev_can_stop(&ev->tm))) {
/* Update timestamp to avoid timers running early */
ev_now_update_if_cheap(loop);

ev->tm.data = ev;
ev_timer_init(&ev->tm, rspamd_ev_watcher_timer_cb, at, 0.0);
ev_timer_start(EV_A, &ev->tm);
}
}
}

+ 14
- 3
src/libutil/libev_helper.h View File

@@ -1,11 +1,11 @@
/*-
* Copyright 2019 Vsevolod Stakhov
/*
* Copyright 2024 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -79,6 +79,17 @@ void rspamd_ev_watcher_reschedule(struct ev_loop *loop,
struct rspamd_io_ev *ev,
short what);

/**
* Convenience function to reschedule watcher with different events and different timeout
* @param loop
* @param ev
* @param what
*/
void rspamd_ev_watcher_reschedule_at(struct ev_loop *loop,
struct rspamd_io_ev *ev,
short what,
ev_tstamp at);

#ifdef __cplusplus
}
#endif

Loading…
Cancel
Save