From c983e776c4e7e2ea6e1990123d215e639deb353b Mon Sep 17 00:00:00 2001 From: Evgeny Vereshchagin Date: Sun, 22 Nov 2015 06:41:31 +0000 Subject: sd-event: use prioq_ensure_allocated where possible --- src/libsystemd/sd-event/sd-event.c | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) (limited to 'src/libsystemd') diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index e8e3d7306f..3191b458d1 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -416,11 +416,9 @@ _public_ int sd_event_new(sd_event** ret) { e->original_pid = getpid(); e->perturb = USEC_INFINITY; - e->pending = prioq_new(pending_prioq_compare); - if (!e->pending) { - r = -ENOMEM; + r = prioq_ensure_allocated(&e->pending, pending_prioq_compare); + if (r < 0) goto fail; - } e->epoll_fd = epoll_create1(EPOLL_CLOEXEC); if (e->epoll_fd < 0) { @@ -1052,17 +1050,13 @@ _public_ int sd_event_add_time( d = event_get_clock_data(e, type); assert(d); - if (!d->earliest) { - d->earliest = prioq_new(earliest_time_prioq_compare); - if (!d->earliest) - return -ENOMEM; - } + r = prioq_ensure_allocated(&d->earliest, earliest_time_prioq_compare); + if (r < 0) + return r; - if (!d->latest) { - d->latest = prioq_new(latest_time_prioq_compare); - if (!d->latest) - return -ENOMEM; - } + r = prioq_ensure_allocated(&d->latest, latest_time_prioq_compare); + if (r < 0) + return r; if (d->fd < 0) { r = event_setup_timer_fd(e, d, clock); @@ -1313,11 +1307,9 @@ _public_ int sd_event_add_exit( assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(!event_pid_changed(e), -ECHILD); - if (!e->exit) { - e->exit = prioq_new(exit_prioq_compare); - if (!e->exit) - return -ENOMEM; - } + r = prioq_ensure_allocated(&e->exit, exit_prioq_compare); + if (r < 0) + return r; s = source_new(e, !ret, SOURCE_EXIT); if (!s) -- cgit v1.2.3-54-g00ecf From 5657c75f91ae7c2a9b130d8bf256ba6fda07975f Mon Sep 17 00:00:00 2001 From: Evgeny Vereshchagin Date: Sun, 22 Nov 2015 08:23:59 +0000 Subject: tests: check exit handler invocation --- src/libsystemd/sd-event/test-event.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/libsystemd') diff --git a/src/libsystemd/sd-event/test-event.c b/src/libsystemd/sd-event/test-event.c index c1a3b49483..e09fa915eb 100644 --- a/src/libsystemd/sd-event/test-event.c +++ b/src/libsystemd/sd-event/test-event.c @@ -234,6 +234,7 @@ static void test_basic(void) { assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0); assert_se(sd_event_loop(e) >= 0); + assert_se(got_exit); sd_event_source_unref(z); sd_event_source_unref(q); -- cgit v1.2.3-54-g00ecf From 509a07ad6d6aed85a63ca04661b9993867b43b11 Mon Sep 17 00:00:00 2001 From: Evgeny Vereshchagin Date: Sun, 22 Nov 2015 09:11:19 +0000 Subject: tests: add missing sd_event_add_post to test_basic --- src/libsystemd/sd-event/test-event.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/libsystemd') diff --git a/src/libsystemd/sd-event/test-event.c b/src/libsystemd/sd-event/test-event.c index e09fa915eb..4a87159151 100644 --- a/src/libsystemd/sd-event/test-event.c +++ b/src/libsystemd/sd-event/test-event.c @@ -158,6 +158,16 @@ static int exit_handler(sd_event_source *s, void *userdata) { return 3; } +static bool got_post = false; + +static int post_handler(sd_event_source *s, void *userdata) { + log_info("got post handler"); + + got_post = true; + + return 2; +} + static void test_basic(void) { sd_event *e = NULL; sd_event_source *w = NULL, *x = NULL, *y = NULL, *z = NULL, *q = NULL, *t = NULL; @@ -230,10 +240,12 @@ static void test_basic(void) { sd_event_source_unref(y); do_quit = true; + assert_se(sd_event_add_post(e, NULL, post_handler, NULL) >= 0); assert_se(sd_event_source_set_time(z, now(CLOCK_MONOTONIC) + 200 * USEC_PER_MSEC) >= 0); assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0); assert_se(sd_event_loop(e) >= 0); + assert_se(got_post); assert_se(got_exit); sd_event_source_unref(z); -- cgit v1.2.3-54-g00ecf