From 917cdf87bf1447839522adb5e81e12b611b69a26 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 23 Apr 2024 15:03:25 +0100 Subject: [PATCH] [Minor] Add function to reschedule events at specific timer --- src/libutil/libev_helper.c | 30 ++++++++++++++++++++++++++++++ src/libutil/libev_helper.h | 17 ++++++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/libutil/libev_helper.c b/src/libutil/libev_helper.c index 3b880aaa2..203e1ed73 100644 --- a/src/libutil/libev_helper.c +++ b/src/libutil/libev_helper.c @@ -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); + } + } } \ No newline at end of file diff --git a/src/libutil/libev_helper.h b/src/libutil/libev_helper.h index 44d1604b0..d68f17951 100644 --- a/src/libutil/libev_helper.h +++ b/src/libutil/libev_helper.h @@ -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 -- 2.39.5