From 77b772bce846db28dc447420fd380a51eadcde15 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 23 Nov 2020 11:40:24 +0100 Subject: [PATCH 09/20] sd-event: split clock data allocation out of sd_event_add_time() Just some simple refactoring, that will make things easier for us later. But it looks better this way even without the later function reuse. (cherry picked from commit 41c63f36c3352af8bebf03b6181f5d866431d0af) Related: #1819868 [commit 6cc0022115afbac9ac66c456b140601d90271687 from https://github.com/systemd-rhel/rhel-8/] Signed-off-by: Li Zhou --- src/libsystemd/sd-event/sd-event.c | 34 ++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index 7074520..8e6536f 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -1065,6 +1065,28 @@ static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata)); } +static int setup_clock_data(sd_event *e, struct clock_data *d, clockid_t clock) { + int r; + + assert(d); + + if (d->fd < 0) { + r = event_setup_timer_fd(e, d, clock); + if (r < 0) + return r; + } + + r = prioq_ensure_allocated(&d->earliest, earliest_time_prioq_compare); + if (r < 0) + return r; + + r = prioq_ensure_allocated(&d->latest, latest_time_prioq_compare); + if (r < 0) + return r; + + return 0; +} + _public_ int sd_event_add_time( sd_event *e, sd_event_source **ret, @@ -1094,20 +1116,10 @@ _public_ int sd_event_add_time( d = event_get_clock_data(e, type); assert(d); - r = prioq_ensure_allocated(&d->earliest, earliest_time_prioq_compare); - if (r < 0) - return r; - - r = prioq_ensure_allocated(&d->latest, latest_time_prioq_compare); + r = setup_clock_data(e, d, clock); if (r < 0) return r; - if (d->fd < 0) { - r = event_setup_timer_fd(e, d, clock); - if (r < 0) - return r; - } - s = source_new(e, !ret, type); if (!s) return -ENOMEM; -- 2.17.1